Skip to content

Commit f96ecbc

Browse files
committed
feat: Expose postgres fixture factory functions for controlling container scope.
1 parent f1196b3 commit f96ecbc

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pytest-mock-resources"
3-
version = "2.12.0"
3+
version = "2.12.1"
44
description = "A pytest plugin for easily instantiating reproducible mock resources."
55
authors = [
66
"Omar Khan <oakhan3@gmail.com>",

src/pytest_mock_resources/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
create_mongo_fixture,
1313
create_moto_fixture,
1414
create_mysql_fixture,
15+
create_postgres_config_fixture,
16+
create_postgres_container_fixture,
1517
create_postgres_fixture,
1618
create_redis_fixture,
1719
create_redshift_fixture,
@@ -56,6 +58,8 @@
5658
"create_moto_fixture",
5759
"create_mysql_fixture",
5860
"create_postgres_fixture",
61+
"create_postgres_config_fixture",
62+
"create_postgres_container_fixture",
5963
"create_redis_fixture",
6064
"create_redshift_fixture",
6165
"create_sqlite_fixture",

src/pytest_mock_resources/fixture/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
pmr_mysql_container,
1717
)
1818
from pytest_mock_resources.fixture.postgresql import (
19+
create_postgres_config_fixture,
20+
create_postgres_container_fixture,
1921
create_postgres_fixture,
2022
pmr_postgres_config,
2123
pmr_postgres_container,
@@ -39,6 +41,8 @@
3941
"create_moto_fixture",
4042
"create_mysql_fixture",
4143
"create_postgres_fixture",
44+
"create_postgres_config_fixture",
45+
"create_postgres_container_fixture",
4246
"create_redis_fixture",
4347
"create_redshift_fixture",
4448
"create_sqlite_fixture",

src/pytest_mock_resources/fixture/postgresql.py

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,23 @@
1616
get_sqlalchemy_engine,
1717
PostgresConfig,
1818
)
19-
from pytest_mock_resources.fixture.base import asyncio_fixture, generate_fixture_id
19+
from pytest_mock_resources.fixture.base import asyncio_fixture, generate_fixture_id, Scope
2020
from pytest_mock_resources.sqlalchemy import (
2121
bifurcate_actions,
2222
EngineManager,
2323
normalize_actions,
2424
)
2525

26+
__all__ = [
27+
"DatabaseExistsError",
28+
"PostgresConfig",
29+
"create_postgres_config_fixture",
30+
"create_postgres_container_fixture",
31+
"create_postgres_fixture",
32+
"pmr_postgres_config",
33+
"pmr_postgres_container",
34+
]
35+
2636
log = logging.getLogger(__name__)
2737

2838

@@ -35,26 +45,68 @@ class DatabaseExistsError(RuntimeError):
3545
"""
3646

3747

38-
@pytest.fixture(scope="session")
39-
def pmr_postgres_config():
40-
"""Override this fixture with a :class:`PostgresConfig` instance to specify different defaults.
48+
def create_postgres_config_fixture(
49+
config: PostgresConfig | None = None, *, scope: Scope = "session"
50+
):
51+
"""Create a fixture for a :class:`PostgresConfig` instance.
52+
53+
Arguments:
54+
config: A postgres configuration instance to use as the default.
55+
scope: The scope of the fixture. Defaults to "session".
56+
57+
>>> pmr_postgres_config = create_postgres_config_fixture(PostgresConfig(), scope="session")
58+
59+
is exactly equivalent to
60+
61+
>>> @pytest.fixture(scope="session")
62+
... def pmr_postgres_config():
63+
... return PostgresConfig()
64+
"""
65+
if config is None:
66+
config = PostgresConfig()
67+
68+
@pytest.fixture(scope=scope)
69+
def fixture():
70+
"""Override this fixture with a :class:`PostgresConfig` instance to specify different defaults.
71+
72+
Examples:
73+
>>> @pytest.fixture(scope='session')
74+
... def pmr_postgres_config():
75+
... return PostgresConfig(image="postgres:9.6.10", root_database="foo")
76+
"""
77+
return config
78+
79+
return fixture
80+
81+
82+
def create_postgres_container_fixture(
83+
*, config: PostgresConfig | None = None, scope: Scope = "session"
84+
):
85+
"""Create a fixture for the underlying postgres container.
86+
87+
This fixture factory is primarily useful as a mechanism to adjust the container's fixture scope,
88+
which would only otherwise be possible by inlining the default fixture's source implementation.
4189
4290
Examples:
43-
>>> @pytest.fixture(scope='session')
44-
... def pmr_postgres_config():
45-
... return PostgresConfig(image="postgres:9.6.10", root_database="foo")
91+
>>> pmr_postgres_container = create_postgres_container_fixture(scope="function")
92+
93+
Arguments:
94+
config: A postgres configuration instance to use as the default. When left unspecified, defaults
95+
to the `pmr_postgres_config` fixture.
96+
scope: The scope of the fixture. Defaults to "session".
4697
"""
47-
return PostgresConfig()
4898

99+
@pytest.fixture(scope=scope)
100+
def fixture(pytestconfig, pmr_postgres_config: PostgresConfig):
101+
postgres_config = config or pmr_postgres_config
102+
yield from get_container(pytestconfig, postgres_config)
49103

50-
@pytest.fixture(scope="session")
51-
def pmr_postgres_container(pytestconfig, pmr_postgres_config: PostgresConfig):
52-
yield from get_container(pytestconfig, pmr_postgres_config)
104+
return fixture
53105

54106

55107
def create_postgres_fixture(
56108
*ordered_actions,
57-
scope="function",
109+
scope: Scope = "function",
58110
tables=None,
59111
session=None,
60112
async_=False,
@@ -302,3 +354,7 @@ def _generate_database_name(conn):
302354
result = conn.execute(text("INSERT INTO pytest_mock_resource_db VALUES (DEFAULT) RETURNING id"))
303355
id_ = next(iter(result))[0]
304356
return f"pytest_mock_resource_db_{id_}"
357+
358+
359+
pmr_postgres_config = create_postgres_config_fixture()
360+
pmr_postgres_container = create_postgres_container_fixture()

src/pytest_mock_resources/fixture/redis.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from pytest_mock_resources.compat import redis
34
from pytest_mock_resources.container.base import get_container
45
from pytest_mock_resources.container.redis import RedisConfig

0 commit comments

Comments
 (0)