Skip to content

Commit 048edf2

Browse files
committed
test_client.py conversion complete
1 parent 8402799 commit 048edf2

File tree

3 files changed

+233
-218
lines changed

3 files changed

+233
-218
lines changed

test/asynchronous/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import unittest
2727
import warnings
2828
from asyncio import iscoroutinefunction
29-
30-
from pymongo.lock import _async_create_lock
31-
3229
from test.helpers import (
3330
COMPRESSORS,
3431
IS_SRV,
@@ -52,6 +49,7 @@
5249
sanitize_reply,
5350
)
5451

52+
from pymongo.lock import _async_create_lock
5553
from pymongo.uri_parser import parse_uri
5654

5755
try:

test/asynchronous/conftest.py

Lines changed: 75 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
import asyncio
44
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
515
from typing import Callable
616

7-
import pymongo
17+
import pytest
18+
import pytest_asyncio
819
from typing_extensions import Any
920

21+
import pymongo
1022
from pymongo import AsyncMongoClient
23+
from pymongo.asynchronous.database import AsyncDatabase
1124
from pymongo.uri_parser import parse_uri
1225

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-
2226
_IS_SYNC = False
2327

2428

@@ -33,38 +37,45 @@ def event_loop_policy():
3337
return asyncio.get_event_loop_policy()
3438

3539
@pytest_asyncio.fixture(loop_scope="session", scope="session")
36-
async def async_client_context_fixture():
40+
async def async_client_context():
3741
client = AsyncClientContext()
3842
await client.init()
3943
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")
4152

4253
@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):
4455
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
4758
requirements["IS_SYNC"] = _IS_SYNC
4859
requirements["IS_SYNC"] = _IS_SYNC
4960
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()
6879
yield requirements
6980

7081

@@ -158,35 +169,35 @@ async def _async_mongo_client(
158169

159170

160171
@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]:
162173
"""Make a direct connection. Don't authenticate."""
163174
clients = []
164175
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)
166177
clients.append(client)
167178
return client
168179
yield _make_client
169180
for client in clients:
170181
await client.close()
171182

172183
@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]:
174185
"""Make a direct connection, and authenticate if necessary."""
175186
clients = []
176187
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)
178189
clients.append(client)
179190
return client
180191
yield _make_client
181192
for client in clients:
182193
await client.close()
183194

184195
@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]:
186197
"""Connect to the replica set. Don't authenticate."""
187198
clients = []
188199
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)
190201
clients.append(client)
191202
return client
192203
yield _make_client
@@ -195,11 +206,11 @@ async def _make_client(h: Any = None, p: Any = None, **kwargs: Any):
195206

196207

197208
@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]:
199210
"""Connect to the replica set and authenticate if necessary."""
200211
clients = []
201212
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)
203214
clients.append(client)
204215
return client
205216
yield _make_client
@@ -208,29 +219,29 @@ async def _make_client(h: Any = None, p: Any = None, **kwargs: Any):
208219

209220

210221
@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]:
212223
"""Connect to the replica set if there is one, otherwise the standalone.
213224
214225
Like rs_or_single_client, but does not authenticate.
215226
"""
216227
clients = []
217228
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)
219230
clients.append(client)
220231
return client
221232
yield _make_client
222233
for client in clients:
223234
await client.close()
224235

225236
@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]:
227238
"""Connect to the replica set if there is one, otherwise the standalone.
228239
229240
Authenticates if necessary.
230241
"""
231242
clients = []
232243
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)
234245
clients.append(client)
235246
return client
236247
yield _make_client
@@ -280,4 +291,23 @@ async def _make_client(standalones,
280291
for client in clients:
281292
await client.close()
282293

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+
283313
pytest_collection_modifyitems = pytest_conf.pytest_collection_modifyitems

0 commit comments

Comments
 (0)