Skip to content

Commit 4c4cf2a

Browse files
committed
fix: Attempt to fix the Mongo versioning across multiple versions of Mongo driver / server
1 parent eff402b commit 4c4cf2a

1 file changed

Lines changed: 38 additions & 56 deletions

File tree

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,64 @@
11
"""Tests for async Mongo client with async functions."""
22

33
import asyncio
4-
from contextlib import suppress
54
from random import random
65

76
import pytest
87

98
from cachier import cachier
9+
from tests.mongo_tests.test_mongo_core import _test_mongetter
1010

1111

1212
@pytest.mark.mongo
13-
@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning")
1413
@pytest.mark.asyncio
1514
async def test_async_mongo_mongetter():
16-
pymongo_inmemory = pytest.importorskip("pymongo_inmemory")
17-
1815
from cachier.cores.mongo import _MongoCore
1916

20-
client = pymongo_inmemory.MongoClient()
21-
try:
22-
collection = client["cachier_test"]["cachier_async_mongetter"]
23-
collection.delete_many({})
17+
base_collection = _test_mongetter()
18+
collection = base_collection.database["cachier_async_mongetter"]
19+
collection.delete_many({})
2420

25-
async def async_mongetter():
26-
return collection
21+
async def async_mongetter():
22+
return collection
2723

28-
@cachier(mongetter=async_mongetter)
29-
async def async_cached_mongo(x: int) -> float:
30-
await asyncio.sleep(0.01)
31-
return random() + x
24+
@cachier(mongetter=async_mongetter)
25+
async def async_cached_mongo(x: int) -> float:
26+
await asyncio.sleep(0.01)
27+
return random() + x
3228

33-
val1 = await async_cached_mongo(7)
34-
val2 = await async_cached_mongo(7)
35-
assert val1 == val2
29+
val1 = await async_cached_mongo(7)
30+
val2 = await async_cached_mongo(7)
31+
assert val1 == val2
3632

37-
assert _MongoCore._INDEX_NAME in collection.index_information()
38-
finally:
39-
# pymongo_inmemory doesn't close the internal health-check client.
40-
# Close it to avoid ResourceWarning being treated as an error.
41-
with suppress(Exception):
42-
client._mongod._client.close() # type: ignore[attr-defined]
43-
client.close()
33+
assert _MongoCore._INDEX_NAME in collection.index_information()
4434

4535

4636
@pytest.mark.mongo
47-
@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning")
4837
@pytest.mark.asyncio
4938
async def test_async_mongo_mongetter_method_args_and_kwargs():
50-
pymongo_inmemory = pytest.importorskip("pymongo_inmemory")
51-
5239
from cachier.cores.mongo import _MongoCore
5340

54-
client = pymongo_inmemory.MongoClient()
55-
try:
56-
collection = client["cachier_test"]["cachier_async_mongetter_methods"]
57-
collection.delete_many({})
58-
59-
async def async_mongetter():
60-
return collection
61-
62-
call_count = 0
63-
64-
class _MongoMethods:
65-
@cachier(mongetter=async_mongetter)
66-
async def async_cached_mongo_method_args_kwargs(self, x: int, y: int) -> int:
67-
nonlocal call_count
68-
call_count += 1
69-
await asyncio.sleep(0.01)
70-
return call_count
71-
72-
obj = _MongoMethods()
73-
val1 = await obj.async_cached_mongo_method_args_kwargs(4, 5)
74-
val2 = await obj.async_cached_mongo_method_args_kwargs(y=5, x=4)
75-
assert val1 == val2 == 1
76-
assert call_count == 1
77-
78-
assert _MongoCore._INDEX_NAME in collection.index_information()
79-
finally:
80-
with suppress(Exception):
81-
client._mongod._client.close() # type: ignore[attr-defined]
82-
client.close()
41+
base_collection = _test_mongetter()
42+
collection = base_collection.database["cachier_async_mongetter_methods"]
43+
collection.delete_many({})
44+
45+
async def async_mongetter():
46+
return collection
47+
48+
call_count = 0
49+
50+
class _MongoMethods:
51+
@cachier(mongetter=async_mongetter)
52+
async def async_cached_mongo_method_args_kwargs(self, x: int, y: int) -> int:
53+
nonlocal call_count
54+
call_count += 1
55+
await asyncio.sleep(0.01)
56+
return call_count
57+
58+
obj = _MongoMethods()
59+
val1 = await obj.async_cached_mongo_method_args_kwargs(4, 5)
60+
val2 = await obj.async_cached_mongo_method_args_kwargs(y=5, x=4)
61+
assert val1 == val2 == 1
62+
assert call_count == 1
63+
64+
assert _MongoCore._INDEX_NAME in collection.index_information()

0 commit comments

Comments
 (0)