Skip to content

Commit 2fc911b

Browse files
committed
fix: mock Redis client in warning test to prevent connection attempts
The test was failing in CI because sync methods were still trying to create a Redis connection to localhost:6379 after logging the warning. Now we mock _get_redis_client() to prevent actual connection attempts while still testing the warning functionality. This allows the test to verify warning behavior without requiring a Redis connection for the sync methods that shouldn't be used anyway.
1 parent 0a7b6bc commit 2fc911b

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

tests/integration/test_embedcache_warnings.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,33 @@ async def test_sync_methods_warn_with_async_only_client(async_client, caplog):
1818
# Initialize EmbeddingsCache with only async_redis_client
1919
cache = EmbeddingsCache(name="test_cache", async_redis_client=async_client)
2020

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")
25-
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
32-
33-
# Clear captured logs
34-
caplog.clear()
35-
36-
# Second sync method call should NOT warn (flag prevents spam)
37-
_ = cache.set(text="test", model_name="model", embedding=[0.1, 0.2])
38-
39-
# Should not have logged another warning
40-
assert len(caplog.records) == 0
21+
# Mock _get_redis_client to prevent actual connection attempt
22+
with patch.object(cache, "_get_redis_client") as mock_get_client:
23+
# Mock the Redis client methods that would be called
24+
mock_client = mock_get_client.return_value
25+
mock_client.hgetall.return_value = {} # Empty result for get_by_key
26+
mock_client.hset.return_value = 1 # Success for set
27+
28+
# Capture log warnings
29+
with caplog.at_level(logging.WARNING):
30+
# First sync method call should warn
31+
_ = cache.get_by_key("test_key")
32+
33+
# Check warning was logged
34+
assert len(caplog.records) == 1
35+
assert (
36+
"initialized with async_redis_client only" in caplog.records[0].message
37+
)
38+
assert "Use async methods" in caplog.records[0].message
39+
40+
# Clear captured logs
41+
caplog.clear()
42+
43+
# Second sync method call should NOT warn (flag prevents spam)
44+
_ = cache.set(text="test", model_name="model", embedding=[0.1, 0.2])
45+
46+
# Should not have logged another warning
47+
assert len(caplog.records) == 0
4148

4249

4350
def test_no_warning_with_sync_client(redis_url):

0 commit comments

Comments
 (0)