userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
postgresql.py
1
"""
2
Plugin that imports the required fixtures to start the database
3
and adjusts the PostgreSQL "dbconnection" static config value.
4
"""
5
6
from
contextlib
import
contextmanager
7
from
enum
import
Enum
8
9
import
pytest
10
11
from
pytest_userver
import
sql
12
import
pytest_userver.config
13
14
pytest_plugins = [
15
'testsuite.databases.pgsql.pytest_plugin'
,
16
'pytest_userver.plugins.core'
,
17
]
18
19
20
class
RegisteredNtrxFailureType
(Enum):
21
Error = 1
22
LogicError = 2
23
RuntimeError = 3
24
ConnectionError = 4
25
26
27
class
RegisteredNtrx
:
28
def
__init__(self, testpoint):
29
self.
_registered_ntrx
= dict()
30
self.
_testpoint
= testpoint
31
32
def
_enable_failure(
33
self,
34
name: str,
35
failure_type: RegisteredNtrxFailureType,
36
) ->
None
:
37
self.
_registered_ntrx
[name] = failure_type
38
39
@self._testpoint(f'pg_ntrx_execute::{name}')
40
def
_failure_tp(data):
41
return
{
42
'inject_failure'
: self.
is_failure_enabled
(name),
43
'failure_type'
: self.
_get_failure_type
(name).name
if
self.
is_failure_enabled
(name)
else
None
,
44
}
45
46
def
_disable_failure(self, name: str) ->
None
:
47
if
self.
is_failure_enabled
(name):
48
del self.
_registered_ntrx
[name]
49
50
def
_get_failure_type(self, name: str) -> RegisteredNtrxFailureType:
51
return
self.
_registered_ntrx
[name]
52
53
def
is_failure_enabled(self, name: str) -> bool:
54
return
name
in
self.
_registered_ntrx
55
56
@contextmanager
57
def
mock_failure(
58
self,
59
name: str,
60
failure_type: RegisteredNtrxFailureType = RegisteredNtrxFailureType.Error,
61
):
62
self.
_enable_failure
(name, failure_type)
63
try
:
64
yield
65
finally
:
66
self.
_disable_failure
(name)
67
68
69
@pytest.fixture(scope='session')
70
@pytest_userver.config.patch
71
def
userver_pg_config
(pgsql_local):
72
"""
73
Returns a function that adjusts the static configuration file for
74
the testsuite.
75
Sets the `dbconnection` to the testsuite started PostgreSQL credentials
76
if there's only one `dbconnection` in static config.
77
78
@ingroup userver_testsuite_fixtures
79
"""
80
81
if
not
pgsql_local:
82
raise
ValueError(
83
'Override the "pgsql_local" fixture so that testsuite knowns how to start the PostgreSQL database'
,
84
)
85
86
if
len(pgsql_local) > 1:
87
raise
ValueError(
88
f
'Found more than one entry in "pgsql_local": '
89
f
'{list(pgsql_local.keys())}. '
90
f
'The "userver_pg_config" fixture supports '
91
f
'only one entry in "pgsql_local" fixture. The '
92
f
'"userver_pg_config" fixture should be overridden and '
93
f
'the "dbconnection" for the components::Postgres '
94
f
'components should be adjusted via the overridden fixture.'
,
95
)
96
97
uri = list(pgsql_local.values())[0].get_uri()
98
99
def
_patch_config(config_yaml, config_vars):
100
components = config_yaml[
'components_manager'
][
'components'
]
101
postgre_dbs = {
102
name: params
103
for
name, params
in
components.items()
104
if
params
and
(
'dbconnection'
in
params
or
'dbconnection#env'
in
params)
105
}
106
107
if
len(postgre_dbs) > 1:
108
raise
ValueError(
109
f
'Found more than one components with "dbconnection": '
110
f
'{list(postgre_dbs.keys())}. '
111
f
'The "userver_pg_config" fixture should be overridden and '
112
f
'the "dbconnection" for the components::Postgres '
113
f
'components should be adjusted via the overridden fixture.'
,
114
)
115
116
for
config
in
postgre_dbs.values():
117
config[
'dbconnection'
] = uri
118
config.pop(
'dbalias'
,
None
)
119
120
return
_patch_config
121
122
123
@pytest.fixture
124
def
userver_pg_trx
(testpoint) -> sql.RegisteredTrx:
125
"""
126
The fixture maintains transaction fault injection state using
127
RegisteredTrx class.
128
129
@see pytest_userver.sql.RegisteredTrx
130
131
@snippet postgresql/functional_tests/integration_tests/tests/test_trx_failure.py fault injection
132
133
@ingroup userver_testsuite_fixtures
134
"""
135
136
registered =
sql.RegisteredTrx
()
137
138
@testpoint('pg_trx_commit')
139
def
_pg_trx_tp(data):
140
should_fail = registered.is_failure_enabled(data[
'trx_name'
])
141
return
{
'trx_should_fail'
: should_fail}
142
143
return
registered
144
145
146
@pytest.fixture
147
def
userver_pg_ntrx
(testpoint) -> RegisteredNtrx:
148
"""
149
The fixture maintains single query fault injection state using
150
RegisteredNtrx class.
151
152
@see pytest_userver.plugins.postgresql.RegisteredNtrx
153
154
@snippet postgresql/functional_tests/integration_tests/tests/test_ntrx_failure.py fault injection
155
156
@ingroup userver_testsuite_fixtures
157
"""
158
159
return
RegisteredNtrx
(testpoint)
testsuite
pytest_plugins
pytest_userver
plugins
postgresql.py
Generated on
for userver by
Doxygen
1.17.0