|
| 1 | +"""Test warning behavior when using sync methods with async-only client.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from redis import Redis |
| 8 | + |
| 9 | +from redisvl.extensions.cache.embeddings import EmbeddingsCache |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(autouse=True) |
| 13 | +def reset_warning_flag(): |
| 14 | + """Reset the warning flag before each test to ensure test isolation.""" |
| 15 | + EmbeddingsCache._warning_shown = False |
| 16 | + yield |
| 17 | + # Optionally reset after test as well for cleanup |
| 18 | + EmbeddingsCache._warning_shown = False |
| 19 | + |
| 20 | + |
| 21 | +@pytest.mark.asyncio |
| 22 | +async def test_sync_methods_warn_with_async_only_client(async_client, caplog): |
| 23 | + """Test that sync methods warn when only async client is provided.""" |
| 24 | + # Initialize EmbeddingsCache with only async_redis_client |
| 25 | + cache = EmbeddingsCache(name="test_cache", async_redis_client=async_client) |
| 26 | + |
| 27 | + # Mock _get_redis_client to prevent actual connection attempt |
| 28 | + with patch.object(cache, "_get_redis_client") as mock_get_client: |
| 29 | + # Mock the Redis client methods that would be called |
| 30 | + mock_client = mock_get_client.return_value |
| 31 | + mock_client.hgetall.return_value = {} # Empty result for get_by_key |
| 32 | + mock_client.hset.return_value = 1 # Success for set |
| 33 | + |
| 34 | + # Capture log warnings |
| 35 | + with caplog.at_level(logging.WARNING): |
| 36 | + # First sync method call should warn |
| 37 | + _ = cache.get_by_key("test_key") |
| 38 | + |
| 39 | + # Check warning was logged |
| 40 | + assert len(caplog.records) == 1 |
| 41 | + assert ( |
| 42 | + "initialized with async_redis_client only" in caplog.records[0].message |
| 43 | + ) |
| 44 | + assert "Use async methods" in caplog.records[0].message |
| 45 | + |
| 46 | + # Clear captured logs |
| 47 | + caplog.clear() |
| 48 | + |
| 49 | + # Second sync method call should NOT warn (flag prevents spam) |
| 50 | + _ = cache.set(text="test", model_name="model", embedding=[0.1, 0.2]) |
| 51 | + |
| 52 | + # Should not have logged another warning |
| 53 | + assert len(caplog.records) == 0 |
| 54 | + |
| 55 | + |
| 56 | +def test_no_warning_with_sync_client(redis_url): |
| 57 | + """Test that no warning is shown when sync client is provided.""" |
| 58 | + # Create sync redis client from redis_url |
| 59 | + sync_client = Redis.from_url(redis_url) |
| 60 | + |
| 61 | + try: |
| 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]) |
| 69 | + |
| 70 | + # No warnings should have been logged |
| 71 | + mock_logger.return_value.warning.assert_not_called() |
| 72 | + finally: |
| 73 | + sync_client.close() |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.asyncio |
| 77 | +async def test_async_methods_no_warning(async_client): |
| 78 | + """Test that async methods don't trigger warnings.""" |
| 79 | + # Initialize EmbeddingsCache with only async_redis_client |
| 80 | + cache = EmbeddingsCache(name="test_cache", async_redis_client=async_client) |
| 81 | + |
| 82 | + with patch("redisvl.utils.log.get_logger") as mock_logger: |
| 83 | + # Async methods should not warn |
| 84 | + _ = await cache.aget_by_key("test_key") |
| 85 | + _ = await cache.aset(text="test", model_name="model", embedding=[0.1, 0.2]) |
| 86 | + |
| 87 | + # No warnings should have been logged |
| 88 | + mock_logger.return_value.warning.assert_not_called() |
0 commit comments