55
66import pytest
77from redis import Redis
8- from redis .asyncio import Redis as AsyncRedis
98
109from redisvl .extensions .cache .embeddings import EmbeddingsCache
1110
1211
1312@pytest .mark .asyncio
14- async def test_sync_methods_warn_with_async_only_client (redis_url , caplog ):
13+ async def test_sync_methods_warn_with_async_only_client (async_client , caplog ):
1514 """Test that sync methods warn when only async client is provided."""
1615 # Reset the warning flag for testing
1716 EmbeddingsCache ._warning_shown = False
1817
19- # Create async redis client from the redis_url
20- async_client = await AsyncRedis . from_url ( redis_url )
18+ # Initialize EmbeddingsCache with only async_redis_client
19+ cache = EmbeddingsCache ( name = "test_cache" , async_redis_client = async_client )
2120
22- try :
23- # Initialize EmbeddingsCache with only async_redis_client
24- cache = EmbeddingsCache (name = "test_cache" , async_redis_client = async_client )
25-
26- # Capture log warnings
27- with caplog .at_level (logging .WARNING ):
28- # First sync method call should warn
29- _ = cache .get_by_key ("test_key" )
21+ # Capture log warnings
22+ with caplog .at_level (logging .WARNING ):
23+ # First sync method call should warn
24+ _ = cache .get_by_key ("test_key" )
3025
31- # Check warning was logged
32- assert len (caplog .records ) == 1
33- assert (
34- "initialized with async_redis_client only" in caplog .records [0 ].message
35- )
36- assert "Use async methods" in caplog .records [0 ].message
26+ # Check warning was logged
27+ assert len (caplog .records ) == 1
28+ assert (
29+ "initialized with async_redis_client only" in caplog .records [0 ].message
30+ )
31+ assert "Use async methods" in caplog .records [0 ].message
3732
38- # Clear captured logs
39- caplog .clear ()
33+ # Clear captured logs
34+ caplog .clear ()
4035
41- # Second sync method call should NOT warn (flag prevents spam)
42- _ = cache .set (text = "test" , model_name = "model" , embedding = [0.1 , 0.2 ])
36+ # Second sync method call should NOT warn (flag prevents spam)
37+ _ = cache .set (text = "test" , model_name = "model" , embedding = [0.1 , 0.2 ])
4338
44- # Should not have logged another warning
45- assert len (caplog .records ) == 0
46- finally :
47- # Properly close the async client
48- await async_client .aclose ()
39+ # Should not have logged another warning
40+ assert len (caplog .records ) == 0
4941
5042
5143def test_no_warning_with_sync_client (redis_url ):
@@ -72,25 +64,18 @@ def test_no_warning_with_sync_client(redis_url):
7264
7365
7466@pytest .mark .asyncio
75- async def test_async_methods_no_warning (redis_url ):
67+ async def test_async_methods_no_warning (async_client ):
7668 """Test that async methods don't trigger warnings."""
7769 # Reset the warning flag for testing
7870 EmbeddingsCache ._warning_shown = False
7971
80- # Create async redis client from redis_url
81- async_client = await AsyncRedis . from_url ( redis_url )
72+ # Initialize EmbeddingsCache with only async_redis_client
73+ cache = EmbeddingsCache ( name = "test_cache" , async_redis_client = async_client )
8274
83- try :
84- # Initialize EmbeddingsCache with only async_redis_client
85- cache = EmbeddingsCache (name = "test_cache" , async_redis_client = async_client )
75+ with patch ("redisvl.utils.log.get_logger" ) as mock_logger :
76+ # Async methods should not warn
77+ _ = await cache .aget_by_key ("test_key" )
78+ _ = await cache .aset (text = "test" , model_name = "model" , embedding = [0.1 , 0.2 ])
8679
87- with patch ("redisvl.utils.log.get_logger" ) as mock_logger :
88- # Async methods should not warn
89- _ = await cache .aget_by_key ("test_key" )
90- _ = await cache .aset (text = "test" , model_name = "model" , embedding = [0.1 , 0.2 ])
91-
92- # No warnings should have been logged
93- mock_logger .return_value .warning .assert_not_called ()
94- finally :
95- # Properly close the async client
96- await async_client .aclose ()
80+ # No warnings should have been logged
81+ mock_logger .return_value .warning .assert_not_called ()
0 commit comments