Skip to content

Commit cf247ef

Browse files
committed
Fix and test init load option check
1 parent 49b9b48 commit cf247ef

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

newsfragments/+3f45d2d2.bugfix.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed an issue, where defining load paths in ini/toml configuration would fail
2+
due to pytest's LocalPath not having an endswith method.

pytest_postgresql/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from typing import Any, List, Optional, TypedDict, Union
55

6+
from _pytest._py.path import LocalPath
67
from pytest import FixtureRequest
78

89

@@ -50,10 +51,12 @@ def get_postgresql_option(option: str) -> Any:
5051
)
5152

5253

53-
def detect_paths(load_paths: List[str]) -> List[Union[Path, str]]:
54+
def detect_paths(load_paths: List[Union[LocalPath, str]]) -> List[Union[Path, str]]:
5455
"""Convert path to sql files to Path instances."""
5556
converted_load_paths: List[Union[Path, str]] = []
5657
for path in load_paths:
58+
if isinstance(path, LocalPath):
59+
path = str(path)
5760
if path.endswith(".sql"):
5861
converted_load_paths.append(Path(path))
5962
else:

tests/test_config.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""Config tests."""
2+
3+
from pathlib import Path
4+
from typing import Union
5+
6+
import pytest
7+
from _pytest._py.path import LocalPath
8+
9+
from pytest_postgresql.config import detect_paths
10+
11+
12+
@pytest.mark.parametrize(
13+
"path, want",
14+
(
15+
("test.sql", Path("test.sql")),
16+
("load.function", "load.function"),
17+
(LocalPath("test.sql"), Path("test.sql").absolute()), # type: ignore[no-untyped-call]
18+
),
19+
)
20+
def test_detect_paths(path: Union[str, LocalPath], want: Path | str) -> None:
21+
"""Check the correctness of detect_paths function."""
22+
assert detect_paths([path]) == [want]

tests/test_postgres_options_plugin.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ def test_postgres_loader_in_cli(pointed_pytester: Pytester) -> None:
4848
ret.assert_outcomes(passed=1)
4949

5050

51+
def test_postgres_loader_in_ini(pointed_pytester: Pytester) -> None:
52+
"""Check that pytest.ini arguments are honored for load."""
53+
pointed_pytester.copy_example("test_load.py")
54+
test_sql_path = pointed_pytester.copy_example("test.sql")
55+
pointed_pytester.makefile(".ini", pytest=f"[pytest]\npostgresql_load = {test_sql_path}\n")
56+
ret = pointed_pytester.runpytest("test_load.py")
57+
ret.assert_outcomes(passed=1)
58+
59+
5160
postgresql_proc_to_override = postgresql_proc()
5261

5362

0 commit comments

Comments
 (0)