|
| 1 | +"""Test Redis key decoding functionality to ensure it works with both decode_responses=True and False.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from redis import Redis |
| 5 | +from redisvl.redis.connection import RedisConnectionFactory |
| 6 | + |
| 7 | +from langgraph.checkpoint.redis.base import BaseRedisSaver |
| 8 | +from langgraph.checkpoint.redis.util import safely_decode |
| 9 | + |
| 10 | + |
| 11 | +def test_safely_decode(): |
| 12 | + """Test the safely_decode function with both bytes and strings.""" |
| 13 | + # Test with bytes |
| 14 | + assert safely_decode(b"test_key") == "test_key" |
| 15 | + |
| 16 | + # Test with string |
| 17 | + assert safely_decode("test_key") == "test_key" |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def redis_client_decoded(): |
| 22 | + """Redis client with decode_responses=True.""" |
| 23 | + client = Redis.from_url("redis://localhost:6379", decode_responses=True) |
| 24 | + yield client |
| 25 | + client.close() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def redis_client_bytes(): |
| 30 | + """Redis client with decode_responses=False (default).""" |
| 31 | + client = Redis.from_url("redis://localhost:6379", decode_responses=False) |
| 32 | + yield client |
| 33 | + client.close() |
| 34 | + |
| 35 | + |
| 36 | +def test_redis_keys_with_decode_responses(redis_client_decoded, redis_client_bytes): |
| 37 | + """Test that redis.keys() behaves as expected with different decode_responses settings.""" |
| 38 | + # Generate a unique key prefix for this test |
| 39 | + test_key_prefix = "test_decode_responses_" |
| 40 | + |
| 41 | + # Create some test keys |
| 42 | + for i in range(3): |
| 43 | + key = f"{test_key_prefix}{i}" |
| 44 | + redis_client_bytes.set(key, f"value{i}") |
| 45 | + |
| 46 | + try: |
| 47 | + # Test with decode_responses=False (returns bytes) |
| 48 | + keys_bytes = redis_client_bytes.keys(f"{test_key_prefix}*") |
| 49 | + assert all(isinstance(k, bytes) for k in keys_bytes) |
| 50 | + |
| 51 | + # Test with decode_responses=True (returns strings) |
| 52 | + keys_str = redis_client_decoded.keys(f"{test_key_prefix}*") |
| 53 | + assert all(isinstance(k, str) for k in keys_str) |
| 54 | + |
| 55 | + # Test that our safely_decode function works with both |
| 56 | + decoded_bytes = [safely_decode(k) for k in keys_bytes] |
| 57 | + decoded_str = [safely_decode(k) for k in keys_str] |
| 58 | + |
| 59 | + # Both should now be lists of strings |
| 60 | + assert all(isinstance(k, str) for k in decoded_bytes) |
| 61 | + assert all(isinstance(k, str) for k in decoded_str) |
| 62 | + |
| 63 | + # Both should contain the same keys |
| 64 | + assert sorted(decoded_bytes) == sorted(decoded_str) |
| 65 | + |
| 66 | + finally: |
| 67 | + # Clean up |
| 68 | + for i in range(3): |
| 69 | + redis_client_bytes.delete(f"{test_key_prefix}{i}") |
| 70 | + |
| 71 | + |
| 72 | +def test_parse_redis_key_with_different_clients( |
| 73 | + redis_client_decoded, redis_client_bytes |
| 74 | +): |
| 75 | + """Test that our _parse_redis_checkpoint_writes_key method works correctly.""" |
| 76 | + # Create a test key using the format expected by the parser |
| 77 | + from langgraph.checkpoint.redis.base import ( |
| 78 | + CHECKPOINT_WRITE_PREFIX, |
| 79 | + REDIS_KEY_SEPARATOR, |
| 80 | + ) |
| 81 | + |
| 82 | + test_key = f"{CHECKPOINT_WRITE_PREFIX}{REDIS_KEY_SEPARATOR}thread1{REDIS_KEY_SEPARATOR}ns1{REDIS_KEY_SEPARATOR}cp1{REDIS_KEY_SEPARATOR}task1{REDIS_KEY_SEPARATOR}0" |
| 83 | + |
| 84 | + # Test parsing with bytes key (as would come from decode_responses=False) |
| 85 | + bytes_key = test_key.encode() |
| 86 | + parsed_bytes = BaseRedisSaver._parse_redis_checkpoint_writes_key( |
| 87 | + safely_decode(bytes_key) |
| 88 | + ) |
| 89 | + |
| 90 | + # Test parsing with string key (as would come from decode_responses=True) |
| 91 | + parsed_str = BaseRedisSaver._parse_redis_checkpoint_writes_key( |
| 92 | + safely_decode(test_key) |
| 93 | + ) |
| 94 | + |
| 95 | + # Both should produce the same result |
| 96 | + assert parsed_bytes == parsed_str |
| 97 | + assert parsed_bytes["thread_id"] == "thread1" |
| 98 | + assert parsed_bytes["checkpoint_ns"] == "ns1" |
| 99 | + assert parsed_bytes["checkpoint_id"] == "cp1" |
| 100 | + assert parsed_bytes["task_id"] == "task1" |
| 101 | + assert parsed_bytes["idx"] == "0" |
0 commit comments