Skip to content

Commit 4ec6ab9

Browse files
committed
feat: Class async methods tests
1 parent 4817d64 commit 4ec6ab9

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

tests/test_async_backend_clients.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,35 @@ async def async_cached_redis(x: int) -> float:
5656
assert val1 == val2
5757

5858

59+
@pytest.mark.redis
60+
@pytest.mark.asyncio
61+
async def test_async_redis_client_factory_method_args_and_kwargs():
62+
pytest.importorskip("redis")
63+
64+
client = _AsyncInMemoryRedis()
65+
66+
async def get_redis_client():
67+
return client
68+
69+
call_count = 0
70+
71+
class _RedisMethods:
72+
@cachier(backend="redis", redis_client=get_redis_client)
73+
async def async_cached_redis_method_args_kwargs(self, x: int, y: int) -> int:
74+
nonlocal call_count
75+
call_count += 1
76+
await asyncio.sleep(0.01)
77+
return call_count
78+
79+
obj = _RedisMethods()
80+
val1 = await obj.async_cached_redis_method_args_kwargs(1, 2)
81+
val2 = await obj.async_cached_redis_method_args_kwargs(y=2, x=1)
82+
assert val1 == val2 == 1
83+
assert call_count == 1
84+
85+
5986
@pytest.mark.mongo
87+
@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning")
6088
@pytest.mark.asyncio
6189
async def test_async_mongo_mongetter():
6290
pymongo_inmemory = pytest.importorskip("pymongo_inmemory")
@@ -87,3 +115,42 @@ async def async_cached_mongo(x: int) -> float:
87115
with suppress(Exception):
88116
client._mongod._client.close() # type: ignore[attr-defined]
89117
client.close()
118+
119+
120+
@pytest.mark.mongo
121+
@pytest.mark.filterwarnings("ignore:Python 3\\.14 will, by default, filter extracted tar archives.*:DeprecationWarning")
122+
@pytest.mark.asyncio
123+
async def test_async_mongo_mongetter_method_args_and_kwargs():
124+
pymongo_inmemory = pytest.importorskip("pymongo_inmemory")
125+
126+
from cachier.cores.mongo import _MongoCore
127+
128+
client = pymongo_inmemory.MongoClient()
129+
try:
130+
collection = client["cachier_test"]["cachier_async_mongetter_methods"]
131+
collection.delete_many({})
132+
133+
async def async_mongetter():
134+
return collection
135+
136+
call_count = 0
137+
138+
class _MongoMethods:
139+
@cachier(mongetter=async_mongetter)
140+
async def async_cached_mongo_method_args_kwargs(self, x: int, y: int) -> int:
141+
nonlocal call_count
142+
call_count += 1
143+
await asyncio.sleep(0.01)
144+
return call_count
145+
146+
obj = _MongoMethods()
147+
val1 = await obj.async_cached_mongo_method_args_kwargs(4, 5)
148+
val2 = await obj.async_cached_mongo_method_args_kwargs(y=5, x=4)
149+
assert val1 == val2 == 1
150+
assert call_count == 1
151+
152+
assert _MongoCore._INDEX_NAME in collection.index_information()
153+
finally:
154+
with suppress(Exception):
155+
client._mongod._client.close() # type: ignore[attr-defined]
156+
client.close()

0 commit comments

Comments
 (0)