Skip to content

Commit ca24ced

Browse files
committed
Parse port_search_count as an int
1 parent af4b9e6 commit ca24ced

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

pytest_postgresql/config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,18 @@ class PostgresqlConfigDict(TypedDict):
2828
def get_config(request: FixtureRequest) -> PostgresqlConfigDict:
2929
"""Return a dictionary with config options."""
3030

31-
def get_postgresql_option(option: str) -> Any:
31+
def get_postgresql_option(option: str, default: Any = None) -> Any:
3232
name = "postgresql_" + option
33-
return request.config.getoption(name) or request.config.getini(name)
33+
return request.config.getoption(name) or request.config.getini(name) or default
3434

3535
load_paths = detect_paths(get_postgresql_option("load"))
3636

3737
return PostgresqlConfigDict(
3838
exec=get_postgresql_option("exec"),
3939
host=get_postgresql_option("host"),
4040
port=get_postgresql_option("port"),
41-
port_search_count=get_postgresql_option("port_search_count"),
41+
# Parse as int, because if it's defined in an INI file then it'll always be a string
42+
port_search_count=int(get_postgresql_option("port_search_count", default=5)),
4243
user=get_postgresql_option("user"),
4344
password=get_postgresql_option("password"),
4445
options=get_postgresql_option("options"),

pytest_postgresql/plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.
1818
"""Plugin module of pytest-postgresql."""
19+
1920
from tempfile import gettempdir
2021

2122
from _pytest.config.argparsing import Parser
@@ -92,7 +93,6 @@ def pytest_addoption(parser: Parser) -> None:
9293
action="store",
9394
dest="postgresql_port_search_count",
9495
help=_help_port_search_count,
95-
default=5,
9696
)
9797

9898
parser.addoption("--postgresql-user", action="store", dest="postgresql_user", help=_help_user)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Asserts that port_search_count is 10.
2+
3+
That shows that it is not the default (5), and that we parsed it as an integer.
4+
"""
5+
6+
from pytest import FixtureRequest
7+
8+
from pytest_postgresql.config import get_config
9+
10+
11+
def test_assert_port_search_count_is_ten(request: FixtureRequest) -> None:
12+
"""Asserts that port_search_count is 10."""
13+
config = get_config(request)
14+
assert config["port_search_count"] == 10

tests/test_postgres_options_plugin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ def test_postgres_loader_in_ini(pointed_pytester: Pytester) -> None:
5757
ret.assert_outcomes(passed=1)
5858

5959

60+
def test_postgres_port_search_count_in_cli_is_int(pointed_pytester: Pytester) -> None:
61+
"""Check that the --postgresql-port-search-count command line argument is parsed as an int."""
62+
pointed_pytester.copy_example("test_assert_port_search_count_is_ten.py")
63+
ret = pointed_pytester.runpytest(
64+
"--postgresql-port-search-count", "test_assert_port_search_count_is_ten.py"
65+
)
66+
ret.assert_outcomes(passed=1)
67+
68+
69+
def test_postgres_port_search_count_in_ini_is_int(pointed_pytester: Pytester) -> None:
70+
"""Check that pytest.ini arguments are honored for load."""
71+
pointed_pytester.copy_example("test_assert_port_search_count_is_ten.py")
72+
pointed_pytester.makefile(".ini", pytest="[pytest]\npostgresql_port_search_count = 10\n")
73+
ret = pointed_pytester.runpytest("test_assert_port_search_count_is_ten.py")
74+
ret.assert_outcomes(passed=1)
75+
76+
6077
postgresql_proc_to_override = postgresql_proc()
6178

6279

0 commit comments

Comments
 (0)