2
2
3
3
import asyncio
4
4
import sys
5
+ from test import MONGODB_API_VERSION , db_pwd , db_user , pytest_conf
6
+ from test .asynchronous import (
7
+ AsyncClientContext ,
8
+ _connection_string ,
9
+ async_setup ,
10
+ async_teardown ,
11
+ remove_all_users ,
12
+ )
13
+ from test .asynchronous .pymongo_mocks import AsyncMockClient
14
+ from test .utils import FunctionCallRecorder
5
15
from typing import Callable
6
16
7
- import pymongo
17
+ import pytest
18
+ import pytest_asyncio
8
19
from typing_extensions import Any
9
20
21
+ import pymongo
10
22
from pymongo import AsyncMongoClient
23
+ from pymongo .asynchronous .database import AsyncDatabase
11
24
from pymongo .uri_parser import parse_uri
12
25
13
- from test import pytest_conf , db_user , db_pwd , MONGODB_API_VERSION
14
- from test .asynchronous import async_setup , async_teardown , _connection_string , AsyncClientContext
15
-
16
- import pytest
17
- import pytest_asyncio
18
-
19
- from test .asynchronous .pymongo_mocks import AsyncMockClient
20
- from test .utils import FunctionCallRecorder
21
-
22
26
_IS_SYNC = False
23
27
24
28
@@ -33,38 +37,45 @@ def event_loop_policy():
33
37
return asyncio .get_event_loop_policy ()
34
38
35
39
@pytest_asyncio .fixture (loop_scope = "session" , scope = "session" )
36
- async def async_client_context_fixture ():
40
+ async def async_client_context ():
37
41
client = AsyncClientContext ()
38
42
await client .init ()
39
43
yield client
40
- await client .client .close ()
44
+ if client .client is not None :
45
+ await client .client .close ()
46
+
47
+
48
+ @pytest_asyncio .fixture
49
+ async def integration_test (async_client_context ):
50
+ if not async_client_context .connected :
51
+ pytest .fail ("Integration tests require a MongoDB server" )
41
52
42
53
@pytest_asyncio .fixture (loop_scope = "session" , scope = "session" )
43
- async def test_environment (async_client_context_fixture ):
54
+ async def test_environment (async_client_context ):
44
55
requirements = {}
45
- requirements ["SUPPORT_TRANSACTIONS" ] = async_client_context_fixture .supports_transactions ()
46
- requirements ["IS_DATA_LAKE" ] = async_client_context_fixture .is_data_lake
56
+ requirements ["SUPPORT_TRANSACTIONS" ] = async_client_context .supports_transactions ()
57
+ requirements ["IS_DATA_LAKE" ] = async_client_context .is_data_lake
47
58
requirements ["IS_SYNC" ] = _IS_SYNC
48
59
requirements ["IS_SYNC" ] = _IS_SYNC
49
60
requirements ["REQUIRE_API_VERSION" ] = MONGODB_API_VERSION
50
- requirements ["SUPPORTS_FAILCOMMAND_FAIL_POINT" ] = async_client_context_fixture .supports_failCommand_fail_point
51
- requirements ["IS_NOT_MMAP" ] = async_client_context_fixture .is_not_mmap
52
- requirements ["SERVER_VERSION" ] = async_client_context_fixture .version
53
- requirements ["AUTH_ENABLED" ] = async_client_context_fixture .auth_enabled
54
- requirements ["FIPS_ENABLED" ] = async_client_context_fixture .fips_enabled
55
- requirements ["IS_RS" ] = async_client_context_fixture .is_rs
56
- requirements ["MONGOSES" ] = len (async_client_context_fixture .mongoses )
57
- requirements ["SECONDARIES_COUNT" ] = await async_client_context_fixture .secondaries_count
58
- requirements ["SECONDARY_READ_PREF" ] = await async_client_context_fixture .supports_secondary_read_pref
59
- requirements ["HAS_IPV6" ] = async_client_context_fixture .has_ipv6
60
- requirements ["IS_SERVERLESS" ] = async_client_context_fixture .serverless
61
- requirements ["IS_LOAD_BALANCER" ] = async_client_context_fixture .load_balancer
62
- requirements ["TEST_COMMANDS_ENABLED" ] = async_client_context_fixture .test_commands_enabled
63
- requirements ["IS_TLS" ] = async_client_context_fixture .tls
64
- requirements ["IS_TLS_CERT" ] = async_client_context_fixture .tlsCertificateKeyFile
65
- requirements ["SERVER_IS_RESOLVEABLE" ] = async_client_context_fixture .server_is_resolvable
66
- requirements ["SESSIONS_ENABLED" ] = async_client_context_fixture .sessions_enabled
67
- requirements ["SUPPORTS_RETRYABLE_WRITES" ] = async_client_context_fixture .supports_retryable_writes ()
61
+ requirements ["SUPPORTS_FAILCOMMAND_FAIL_POINT" ] = async_client_context .supports_failCommand_fail_point
62
+ requirements ["IS_NOT_MMAP" ] = async_client_context .is_not_mmap
63
+ requirements ["SERVER_VERSION" ] = async_client_context .version
64
+ requirements ["AUTH_ENABLED" ] = async_client_context .auth_enabled
65
+ requirements ["FIPS_ENABLED" ] = async_client_context .fips_enabled
66
+ requirements ["IS_RS" ] = async_client_context .is_rs
67
+ requirements ["MONGOSES" ] = len (async_client_context .mongoses )
68
+ requirements ["SECONDARIES_COUNT" ] = await async_client_context .secondaries_count
69
+ requirements ["SECONDARY_READ_PREF" ] = await async_client_context .supports_secondary_read_pref
70
+ requirements ["HAS_IPV6" ] = async_client_context .has_ipv6
71
+ requirements ["IS_SERVERLESS" ] = async_client_context .serverless
72
+ requirements ["IS_LOAD_BALANCER" ] = async_client_context .load_balancer
73
+ requirements ["TEST_COMMANDS_ENABLED" ] = async_client_context .test_commands_enabled
74
+ requirements ["IS_TLS" ] = async_client_context .tls
75
+ requirements ["IS_TLS_CERT" ] = async_client_context .tlsCertificateKeyFile
76
+ requirements ["SERVER_IS_RESOLVEABLE" ] = async_client_context .server_is_resolvable
77
+ requirements ["SESSIONS_ENABLED" ] = async_client_context .sessions_enabled
78
+ requirements ["SUPPORTS_RETRYABLE_WRITES" ] = async_client_context .supports_retryable_writes ()
68
79
yield requirements
69
80
70
81
@@ -158,35 +169,35 @@ async def _async_mongo_client(
158
169
159
170
160
171
@pytest_asyncio .fixture (loop_scope = "session" )
161
- async def async_single_client_noauth (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
172
+ async def async_single_client_noauth (async_client_context ) -> Callable [..., AsyncMongoClient ]:
162
173
"""Make a direct connection. Don't authenticate."""
163
174
clients = []
164
175
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
165
- client = await _async_mongo_client (async_client_context_fixture , h , p , authenticate = False , directConnection = True , ** kwargs )
176
+ client = await _async_mongo_client (async_client_context , h , p , authenticate = False , directConnection = True , ** kwargs )
166
177
clients .append (client )
167
178
return client
168
179
yield _make_client
169
180
for client in clients :
170
181
await client .close ()
171
182
172
183
@pytest_asyncio .fixture (loop_scope = "session" )
173
- async def async_single_client (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
184
+ async def async_single_client (async_client_context ) -> Callable [..., AsyncMongoClient ]:
174
185
"""Make a direct connection, and authenticate if necessary."""
175
186
clients = []
176
187
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
177
- client = await _async_mongo_client (async_client_context_fixture , h , p , directConnection = True , ** kwargs )
188
+ client = await _async_mongo_client (async_client_context , h , p , directConnection = True , ** kwargs )
178
189
clients .append (client )
179
190
return client
180
191
yield _make_client
181
192
for client in clients :
182
193
await client .close ()
183
194
184
195
@pytest_asyncio .fixture (loop_scope = "session" )
185
- async def async_rs_client_noauth (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
196
+ async def async_rs_client_noauth (async_client_context ) -> Callable [..., AsyncMongoClient ]:
186
197
"""Connect to the replica set. Don't authenticate."""
187
198
clients = []
188
199
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
189
- client = await _async_mongo_client (async_client_context_fixture , h , p , authenticate = False , ** kwargs )
200
+ client = await _async_mongo_client (async_client_context , h , p , authenticate = False , ** kwargs )
190
201
clients .append (client )
191
202
return client
192
203
yield _make_client
@@ -195,11 +206,11 @@ async def _make_client(h: Any = None, p: Any = None, **kwargs: Any):
195
206
196
207
197
208
@pytest_asyncio .fixture (loop_scope = "session" )
198
- async def async_rs_client (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
209
+ async def async_rs_client (async_client_context ) -> Callable [..., AsyncMongoClient ]:
199
210
"""Connect to the replica set and authenticate if necessary."""
200
211
clients = []
201
212
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
202
- client = await _async_mongo_client (async_client_context_fixture , h , p , ** kwargs )
213
+ client = await _async_mongo_client (async_client_context , h , p , ** kwargs )
203
214
clients .append (client )
204
215
return client
205
216
yield _make_client
@@ -208,29 +219,29 @@ async def _make_client(h: Any = None, p: Any = None, **kwargs: Any):
208
219
209
220
210
221
@pytest_asyncio .fixture (loop_scope = "session" )
211
- async def async_rs_or_single_client_noauth (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
222
+ async def async_rs_or_single_client_noauth (async_client_context ) -> Callable [..., AsyncMongoClient ]:
212
223
"""Connect to the replica set if there is one, otherwise the standalone.
213
224
214
225
Like rs_or_single_client, but does not authenticate.
215
226
"""
216
227
clients = []
217
228
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
218
- client = await _async_mongo_client (async_client_context_fixture , h , p , authenticate = False , ** kwargs )
229
+ client = await _async_mongo_client (async_client_context , h , p , authenticate = False , ** kwargs )
219
230
clients .append (client )
220
231
return client
221
232
yield _make_client
222
233
for client in clients :
223
234
await client .close ()
224
235
225
236
@pytest_asyncio .fixture (loop_scope = "session" )
226
- async def async_rs_or_single_client (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
237
+ async def async_rs_or_single_client (async_client_context ) -> Callable [..., AsyncMongoClient ]:
227
238
"""Connect to the replica set if there is one, otherwise the standalone.
228
239
229
240
Authenticates if necessary.
230
241
"""
231
242
clients = []
232
243
async def _make_client (h : Any = None , p : Any = None , ** kwargs : Any ):
233
- client = await _async_mongo_client (async_client_context_fixture , h , p , ** kwargs )
244
+ client = await _async_mongo_client (async_client_context , h , p , ** kwargs )
234
245
clients .append (client )
235
246
return client
236
247
yield _make_client
@@ -280,4 +291,23 @@ async def _make_client(standalones,
280
291
for client in clients :
281
292
await client .close ()
282
293
294
+ @pytest_asyncio .fixture (loop_scope = "session" )
295
+ async def remove_all_users_fixture (async_client_context , request ):
296
+ db_name = request .param
297
+ yield
298
+ await async_client_context .client [db_name ].command ("dropAllUsersFromDatabase" , 1 , writeConcern = {"w" : async_client_context .w })
299
+
300
+ @pytest_asyncio .fixture (loop_scope = "session" )
301
+ async def drop_user_fixture (async_client_context , request ):
302
+ db , user = request .param
303
+ yield
304
+ await async_client_context .drop_user (db , user )
305
+
306
+ @pytest_asyncio .fixture (loop_scope = "session" )
307
+ async def drop_database_fixture (async_client_context , request ):
308
+ db = request .param
309
+ yield
310
+ await async_client_context .client .drop_database (db )
311
+
312
+
283
313
pytest_collection_modifyitems = pytest_conf .pytest_collection_modifyitems
0 commit comments