Skip to content

Commit 7493656

Browse files
committed
fix: use redis_url fixture in warning tests for CI compatibility
- Replace direct Redis connection with redis_url fixture - Use AsyncRedis.from_url() instead of RedisConnectionFactory - Add proper try/finally blocks for client cleanup - Follow established test patterns from test_embedcache.py This fixes 'Event loop is closed' errors in CI by using the same Docker container-based Redis connection that other tests use.
1 parent 095a974 commit 7493656

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

tests/integration/test_embedcache_warnings.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
"""Test warning behavior when using sync methods with async-only client."""
22

3-
import asyncio
43
import logging
54
from unittest.mock import patch
65

76
import pytest
87
from redis import Redis
8+
from redis.asyncio import Redis as AsyncRedis
99

1010
from redisvl.extensions.cache.embeddings import EmbeddingsCache
11-
from redisvl.redis.connection import RedisConnectionFactory
1211

1312

1413
@pytest.mark.asyncio
15-
async def test_sync_methods_warn_with_async_only_client(caplog):
14+
async def test_sync_methods_warn_with_async_only_client(redis_url, caplog):
1615
"""Test that sync methods warn when only async client is provided."""
1716
# Reset the warning flag for testing
1817
EmbeddingsCache._warning_shown = False
1918

20-
# Create async redis client using the async method
21-
async_client = await RedisConnectionFactory._get_aredis_connection(
22-
"redis://localhost:6379"
23-
)
19+
# Create async redis client from the redis_url
20+
async_client = await AsyncRedis.from_url(redis_url)
2421

2522
try:
2623
# Initialize EmbeddingsCache with only async_redis_client
@@ -51,38 +48,37 @@ async def test_sync_methods_warn_with_async_only_client(caplog):
5148
await async_client.aclose()
5249

5350

54-
def test_no_warning_with_sync_client():
51+
def test_no_warning_with_sync_client(redis_url):
5552
"""Test that no warning is shown when sync client is provided."""
5653
# Reset the warning flag for testing
5754
EmbeddingsCache._warning_shown = False
5855

59-
# Create sync redis client
60-
sync_client = Redis.from_url("redis://localhost:6379")
56+
# Create sync redis client from redis_url
57+
sync_client = Redis.from_url(redis_url)
6158

62-
# Initialize EmbeddingsCache with sync_redis_client
63-
cache = EmbeddingsCache(name="test_cache", redis_client=sync_client)
64-
65-
with patch("redisvl.utils.log.get_logger") as mock_logger:
66-
# Sync methods should not warn
67-
_ = cache.get_by_key("test_key")
68-
_ = cache.set(text="test", model_name="model", embedding=[0.1, 0.2])
59+
try:
60+
# Initialize EmbeddingsCache with sync_redis_client
61+
cache = EmbeddingsCache(name="test_cache", redis_client=sync_client)
6962

70-
# No warnings should have been logged
71-
mock_logger.return_value.warning.assert_not_called()
63+
with patch("redisvl.utils.log.get_logger") as mock_logger:
64+
# Sync methods should not warn
65+
_ = cache.get_by_key("test_key")
66+
_ = cache.set(text="test", model_name="model", embedding=[0.1, 0.2])
7267

73-
sync_client.close()
68+
# No warnings should have been logged
69+
mock_logger.return_value.warning.assert_not_called()
70+
finally:
71+
sync_client.close()
7472

7573

7674
@pytest.mark.asyncio
77-
async def test_async_methods_no_warning():
75+
async def test_async_methods_no_warning(redis_url):
7876
"""Test that async methods don't trigger warnings."""
7977
# Reset the warning flag for testing
8078
EmbeddingsCache._warning_shown = False
8179

82-
# Create async redis client using the async method
83-
async_client = await RedisConnectionFactory._get_aredis_connection(
84-
"redis://localhost:6379"
85-
)
80+
# Create async redis client from redis_url
81+
async_client = await AsyncRedis.from_url(redis_url)
8682

8783
try:
8884
# Initialize EmbeddingsCache with only async_redis_client

0 commit comments

Comments
 (0)