1
1
from __future__ import annotations
2
2
3
3
import asyncio
4
- import contextlib
5
4
import sys
6
5
from typing import Callable
7
6
17
16
import pytest
18
17
import pytest_asyncio
19
18
19
+ from test .asynchronous .pymongo_mocks import AsyncMockClient
20
20
from test .utils import FunctionCallRecorder
21
21
22
22
_IS_SYNC = False
@@ -157,11 +157,17 @@ async def _async_mongo_client(
157
157
return client
158
158
159
159
160
- async def async_single_client_noauth (
161
- async_client_context , h : Any = None , p : Any = None , ** kwargs : Any
162
- ) -> AsyncMongoClient [dict ]:
160
+ @pytest_asyncio .fixture (loop_scope = "session" )
161
+ async def async_single_client_noauth (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
163
162
"""Make a direct connection. Don't authenticate."""
164
- return await _async_mongo_client (async_client_context , h , p , authenticate = False , directConnection = True , ** kwargs )
163
+ clients = []
164
+ 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 )
166
+ clients .append (client )
167
+ return client
168
+ yield _make_client
169
+ for client in clients :
170
+ await client .close ()
165
171
166
172
@pytest_asyncio .fixture (loop_scope = "session" )
167
173
async def async_single_client (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
@@ -175,13 +181,18 @@ async def _make_client(h: Any = None, p: Any = None, **kwargs: Any):
175
181
for client in clients :
176
182
await client .close ()
177
183
178
- # @pytest_asyncio.fixture(loop_scope="function")
179
- # async def async_rs_client_noauth(
180
- # async_client_context, h: Any = None, p: Any = None, **kwargs: Any
181
- # ) -> AsyncMongoClient[dict]:
182
- # """Connect to the replica set. Don't authenticate."""
183
- # return await _async_mongo_client(async_client_context, h, p, authenticate=False, **kwargs)
184
- #
184
+ @pytest_asyncio .fixture (loop_scope = "session" )
185
+ async def async_rs_client_noauth (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
186
+ """Connect to the replica set. Don't authenticate."""
187
+ clients = []
188
+ 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 )
190
+ clients .append (client )
191
+ return client
192
+ yield _make_client
193
+ for client in clients :
194
+ await client .close ()
195
+
185
196
186
197
@pytest_asyncio .fixture (loop_scope = "session" )
187
198
async def async_rs_client (async_client_context_fixture ) -> Callable [..., AsyncMongoClient ]:
@@ -250,6 +261,23 @@ def patch_resolver():
250
261
yield patched_resolver
251
262
pymongo .srv_resolver ._resolve = _resolve
252
263
253
-
264
+ @pytest_asyncio .fixture (loop_scope = "session" )
265
+ async def async_mock_client ():
266
+ clients = []
267
+
268
+ async def _make_client (standalones ,
269
+ members ,
270
+ mongoses ,
271
+ hello_hosts = None ,
272
+ arbiters = None ,
273
+ down_hosts = None ,
274
+ * args ,
275
+ ** kwargs ):
276
+ client = await AsyncMockClient .get_async_mock_client (standalones , members , mongoses , hello_hosts , arbiters , down_hosts , * args , ** kwargs )
277
+ clients .append (client )
278
+ return client
279
+ yield _make_client
280
+ for client in clients :
281
+ await client .close ()
254
282
255
283
pytest_collection_modifyitems = pytest_conf .pytest_collection_modifyitems
0 commit comments