2
2
3
3
import asyncio
4
4
import sys
5
- from test import pytest_conf
6
- from test .asynchronous import async_setup , async_teardown
5
+
6
+ from typing_extensions import Any
7
+
8
+ from pymongo import AsyncMongoClient
9
+ from pymongo .uri_parser import parse_uri
10
+
11
+ from test import pytest_conf , db_user , db_pwd
12
+ from test .asynchronous import async_setup , async_teardown , _connection_string , AsyncClientContext
7
13
8
14
import pytest
9
15
import pytest_asyncio
@@ -21,12 +27,103 @@ def event_loop_policy():
21
27
22
28
return asyncio .get_event_loop_policy ()
23
29
30
+ @pytest_asyncio .fixture (loop_scope = "session" )
31
+ async def async_client_context_fixture ():
32
+ client = AsyncClientContext ()
33
+ await client .init ()
34
+ yield client
35
+ await client .client .close ()
24
36
25
- @pytest_asyncio .fixture (scope = "package " , autouse = True )
37
+ @pytest_asyncio .fixture (loop_scope = "session " , autouse = True )
26
38
async def test_setup_and_teardown ():
27
39
await async_setup ()
28
40
yield
29
41
await async_teardown ()
30
42
43
+ async def _async_mongo_client (
44
+ async_client_context , host , port , authenticate = True , directConnection = None , ** kwargs
45
+ ):
46
+ """Create a new client over SSL/TLS if necessary."""
47
+ host = host or await async_client_context .host
48
+ port = port or await async_client_context .port
49
+ client_options : dict = async_client_context .default_client_options .copy ()
50
+ if async_client_context .replica_set_name and not directConnection :
51
+ client_options ["replicaSet" ] = async_client_context .replica_set_name
52
+ if directConnection is not None :
53
+ client_options ["directConnection" ] = directConnection
54
+ client_options .update (kwargs )
55
+
56
+ uri = _connection_string (host )
57
+ auth_mech = kwargs .get ("authMechanism" , "" )
58
+ if async_client_context .auth_enabled and authenticate and auth_mech != "MONGODB-OIDC" :
59
+ # Only add the default username or password if one is not provided.
60
+ res = parse_uri (uri )
61
+ if (
62
+ not res ["username" ]
63
+ and not res ["password" ]
64
+ and "username" not in client_options
65
+ and "password" not in client_options
66
+ ):
67
+ client_options ["username" ] = db_user
68
+ client_options ["password" ] = db_pwd
69
+ client = AsyncMongoClient (uri , port , ** client_options )
70
+ if client ._options .connect :
71
+ await client .aconnect ()
72
+ return client
73
+
74
+
75
+ async def async_single_client_noauth (
76
+ async_client_context , h : Any = None , p : Any = None , ** kwargs : Any
77
+ ) -> AsyncMongoClient [dict ]:
78
+ """Make a direct connection. Don't authenticate."""
79
+ return await _async_mongo_client (async_client_context , h , p , authenticate = False , directConnection = True , ** kwargs )
80
+ #
81
+ async def async_single_client (
82
+ async_client_context , h : Any = None , p : Any = None , ** kwargs : Any
83
+ ) -> AsyncMongoClient [dict ]:
84
+ """Make a direct connection, and authenticate if necessary."""
85
+ return await _async_mongo_client (async_client_context , h , p , directConnection = True , ** kwargs )
86
+
87
+ # @pytest_asyncio.fixture(loop_scope="function")
88
+ # async def async_rs_client_noauth(
89
+ # async_client_context, h: Any = None, p: Any = None, **kwargs: Any
90
+ # ) -> AsyncMongoClient[dict]:
91
+ # """Connect to the replica set. Don't authenticate."""
92
+ # return await _async_mongo_client(async_client_context, h, p, authenticate=False, **kwargs)
93
+ #
94
+ # @pytest_asyncio.fixture(loop_scope="function")
95
+ # async def async_rs_client(
96
+ # async_client_context, h: Any = None, p: Any = None, **kwargs: Any
97
+ # ) -> AsyncMongoClient[dict]:
98
+ # """Connect to the replica set and authenticate if necessary."""
99
+ # return await _async_mongo_client(async_client_context, h, p, **kwargs)
100
+ #
101
+ # @pytest_asyncio.fixture(loop_scope="function")
102
+ # async def async_rs_or_single_client_noauth(
103
+ # async_client_context, h: Any = None, p: Any = None, **kwargs: Any
104
+ # ) -> AsyncMongoClient[dict]:
105
+ # """Connect to the replica set if there is one, otherwise the standalone.
106
+ #
107
+ # Like rs_or_single_client, but does not authenticate.
108
+ # """
109
+ # return await _async_mongo_client(async_client_context, h, p, authenticate=False, **kwargs)
110
+
111
+ async def async_rs_or_single_client (
112
+ async_client_context , h : Any = None , p : Any = None , ** kwargs : Any
113
+ ) -> AsyncMongoClient [Any ]:
114
+ """Connect to the replica set if there is one, otherwise the standalone.
115
+
116
+ Authenticates if necessary.
117
+ """
118
+ return await _async_mongo_client (async_client_context , h , p , ** kwargs )
119
+
120
+ def simple_client (h : Any = None , p : Any = None , ** kwargs : Any ) -> AsyncMongoClient :
121
+ if not h and not p :
122
+ client = AsyncMongoClient (** kwargs )
123
+ else :
124
+ client = AsyncMongoClient (h , p , ** kwargs )
125
+ return client
126
+
127
+
31
128
32
129
pytest_collection_modifyitems = pytest_conf .pytest_collection_modifyitems
0 commit comments