1
1
import logging
2
- from typing import cast
2
+ from typing import cast , Optional
3
3
4
4
import pytest
5
5
import sqlalchemy
16
16
get_sqlalchemy_engine ,
17
17
PostgresConfig ,
18
18
)
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
20
20
from pytest_mock_resources .sqlalchemy import (
21
21
bifurcate_actions ,
22
22
EngineManager ,
23
23
normalize_actions ,
24
24
)
25
25
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
+
26
36
log = logging .getLogger (__name__ )
27
37
28
38
@@ -35,26 +45,68 @@ class DatabaseExistsError(RuntimeError):
35
45
"""
36
46
37
47
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 : Optional [PostgresConfig ] = 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 : Optional [PostgresConfig ] = 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.
41
89
42
90
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".
46
97
"""
47
- return PostgresConfig ()
48
98
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 )
49
103
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
53
105
54
106
55
107
def create_postgres_fixture (
56
108
* ordered_actions ,
57
- scope = "function" ,
109
+ scope : Scope = "function" ,
58
110
tables = None ,
59
111
session = None ,
60
112
async_ = False ,
@@ -302,3 +354,7 @@ def _generate_database_name(conn):
302
354
result = conn .execute (text ("INSERT INTO pytest_mock_resource_db VALUES (DEFAULT) RETURNING id" ))
303
355
id_ = next (iter (result ))[0 ]
304
356
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 ()
0 commit comments