|
1 | 1 | """Test warning behavior when using sync methods with async-only client.""" |
2 | 2 |
|
3 | | -import asyncio |
4 | 3 | import logging |
5 | 4 | from unittest.mock import patch |
6 | 5 |
|
7 | 6 | import pytest |
8 | 7 | from redis import Redis |
| 8 | +from redis.asyncio import Redis as AsyncRedis |
9 | 9 |
|
10 | 10 | from redisvl.extensions.cache.embeddings import EmbeddingsCache |
11 | | -from redisvl.redis.connection import RedisConnectionFactory |
12 | 11 |
|
13 | 12 |
|
14 | 13 | @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): |
16 | 15 | """Test that sync methods warn when only async client is provided.""" |
17 | 16 | # Reset the warning flag for testing |
18 | 17 | EmbeddingsCache._warning_shown = False |
19 | 18 |
|
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) |
24 | 21 |
|
25 | 22 | try: |
26 | 23 | # Initialize EmbeddingsCache with only async_redis_client |
@@ -51,38 +48,37 @@ async def test_sync_methods_warn_with_async_only_client(caplog): |
51 | 48 | await async_client.aclose() |
52 | 49 |
|
53 | 50 |
|
54 | | -def test_no_warning_with_sync_client(): |
| 51 | +def test_no_warning_with_sync_client(redis_url): |
55 | 52 | """Test that no warning is shown when sync client is provided.""" |
56 | 53 | # Reset the warning flag for testing |
57 | 54 | EmbeddingsCache._warning_shown = False |
58 | 55 |
|
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) |
61 | 58 |
|
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) |
69 | 62 |
|
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]) |
72 | 67 |
|
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() |
74 | 72 |
|
75 | 73 |
|
76 | 74 | @pytest.mark.asyncio |
77 | | -async def test_async_methods_no_warning(): |
| 75 | +async def test_async_methods_no_warning(redis_url): |
78 | 76 | """Test that async methods don't trigger warnings.""" |
79 | 77 | # Reset the warning flag for testing |
80 | 78 | EmbeddingsCache._warning_shown = False |
81 | 79 |
|
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) |
86 | 82 |
|
87 | 83 | try: |
88 | 84 | # Initialize EmbeddingsCache with only async_redis_client |
|
0 commit comments