|
1 | 1 | """Tests for async Mongo client with async functions.""" |
2 | 2 |
|
3 | 3 | import asyncio |
4 | | -from contextlib import suppress |
5 | 4 | from random import random |
6 | 5 |
|
7 | 6 | import pytest |
8 | 7 |
|
9 | 8 | from cachier import cachier |
| 9 | +from tests.mongo_tests.test_mongo_core import _test_mongetter |
10 | 10 |
|
11 | 11 |
|
12 | 12 | @pytest.mark.mongo |
13 | | -@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning") |
14 | 13 | @pytest.mark.asyncio |
15 | 14 | async def test_async_mongo_mongetter(): |
16 | | - pymongo_inmemory = pytest.importorskip("pymongo_inmemory") |
17 | | - |
18 | 15 | from cachier.cores.mongo import _MongoCore |
19 | 16 |
|
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({}) |
24 | 20 |
|
25 | | - async def async_mongetter(): |
26 | | - return collection |
| 21 | + async def async_mongetter(): |
| 22 | + return collection |
27 | 23 |
|
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 |
32 | 28 |
|
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 |
36 | 32 |
|
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() |
44 | 34 |
|
45 | 35 |
|
46 | 36 | @pytest.mark.mongo |
47 | | -@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning") |
48 | 37 | @pytest.mark.asyncio |
49 | 38 | async def test_async_mongo_mongetter_method_args_and_kwargs(): |
50 | | - pymongo_inmemory = pytest.importorskip("pymongo_inmemory") |
51 | | - |
52 | 39 | from cachier.cores.mongo import _MongoCore |
53 | 40 |
|
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