Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions pytest_postgresql/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ class PostgresqlConfigDict(TypedDict):
def get_config(request: FixtureRequest) -> PostgresqlConfigDict:
"""Return a dictionary with config options."""

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

load_paths = detect_paths(get_postgresql_option("load"))

return PostgresqlConfigDict(
exec=get_postgresql_option("exec"),
host=get_postgresql_option("host"),
port=get_postgresql_option("port"),
port_search_count=get_postgresql_option("port_search_count"),
# Parse as int, because if it's defined in an INI file then it'll always be a string
port_search_count=int(get_postgresql_option("port_search_count", default=5)),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd defer to keeping all defaults in single place either defaults for ini or here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! I'll keep them in INI. This should also fix the bug where my return request.config.getoption(name) or request.config.getini(name) or default code was returning None for any falsey config setting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I could change it to be more careful about only coalescing when request.config.getoption(name) is None)

user=get_postgresql_option("user"),
password=get_postgresql_option("password"),
options=get_postgresql_option("options"),
Expand Down
2 changes: 1 addition & 1 deletion pytest_postgresql/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with pytest-postgresql. If not, see <http://www.gnu.org/licenses/>.
"""Plugin module of pytest-postgresql."""

from tempfile import gettempdir

from _pytest.config.argparsing import Parser
Expand Down Expand Up @@ -92,7 +93,6 @@ def pytest_addoption(parser: Parser) -> None:
action="store",
dest="postgresql_port_search_count",
help=_help_port_search_count,
default=5,
)

parser.addoption("--postgresql-user", action="store", dest="postgresql_user", help=_help_user)
Expand Down
14 changes: 14 additions & 0 deletions tests/examples/test_assert_port_search_count_is_ten.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Asserts that port_search_count is 10.

That shows that it is not the default (5), and that we parsed it as an integer.
"""

from pytest import FixtureRequest

from pytest_postgresql.config import get_config


def test_assert_port_search_count_is_ten(request: FixtureRequest) -> None:
"""Asserts that port_search_count is 10."""
config = get_config(request)
assert config["port_search_count"] == 10
17 changes: 17 additions & 0 deletions tests/test_postgres_options_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ def test_postgres_loader_in_ini(pointed_pytester: Pytester) -> None:
ret.assert_outcomes(passed=1)


def test_postgres_port_search_count_in_cli_is_int(pointed_pytester: Pytester) -> None:
"""Check that the --postgresql-port-search-count command line argument is parsed as an int."""
pointed_pytester.copy_example("test_assert_port_search_count_is_ten.py")
ret = pointed_pytester.runpytest(
"--postgresql-port-search-count", "test_assert_port_search_count_is_ten.py"
)
ret.assert_outcomes(passed=1)


def test_postgres_port_search_count_in_ini_is_int(pointed_pytester: Pytester) -> None:
"""Check that pytest.ini arguments are honored for load."""
pointed_pytester.copy_example("test_assert_port_search_count_is_ten.py")
pointed_pytester.makefile(".ini", pytest="[pytest]\npostgresql_port_search_count = 10\n")
ret = pointed_pytester.runpytest("test_assert_port_search_count_is_ten.py")
ret.assert_outcomes(passed=1)


postgresql_proc_to_override = postgresql_proc()


Expand Down