|
| 1 | +""" |
| 2 | +Test for the fix to issue #40 - Fixing numeric version handling with Tag type. |
| 3 | +""" |
| 4 | + |
| 5 | +from contextlib import contextmanager |
| 6 | + |
| 7 | +import pytest |
| 8 | +from langgraph.checkpoint.base import empty_checkpoint |
| 9 | +from redis import Redis |
| 10 | + |
| 11 | +from langgraph.checkpoint.redis import RedisSaver |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(autouse=True) |
| 15 | +async def clear_test_redis(redis_url: str) -> None: |
| 16 | + """Clear Redis before each test.""" |
| 17 | + client = Redis.from_url(redis_url) |
| 18 | + try: |
| 19 | + client.flushall() |
| 20 | + finally: |
| 21 | + client.close() |
| 22 | + |
| 23 | + |
| 24 | +@contextmanager |
| 25 | +def patched_redis_saver(redis_url): |
| 26 | + """ |
| 27 | + Create a RedisSaver with a patched _dump_blobs method to fix the issue. |
| 28 | + This demonstrates the fix approach. |
| 29 | + """ |
| 30 | + original_dump_blobs = RedisSaver._dump_blobs |
| 31 | + |
| 32 | + def patched_dump_blobs(self, thread_id, checkpoint_ns, values, versions): |
| 33 | + """ |
| 34 | + Patched version of _dump_blobs that ensures version is a string. |
| 35 | + """ |
| 36 | + # Convert version to string in versions dictionary |
| 37 | + string_versions = {k: str(v) for k, v in versions.items()} |
| 38 | + |
| 39 | + # Call the original method with string versions |
| 40 | + return original_dump_blobs( |
| 41 | + self, thread_id, checkpoint_ns, values, string_versions |
| 42 | + ) |
| 43 | + |
| 44 | + # Apply the patch |
| 45 | + RedisSaver._dump_blobs = patched_dump_blobs |
| 46 | + |
| 47 | + try: |
| 48 | + # Create the saver with patched method |
| 49 | + saver = RedisSaver(redis_url) |
| 50 | + saver.setup() |
| 51 | + yield saver |
| 52 | + finally: |
| 53 | + # Restore the original method |
| 54 | + RedisSaver._dump_blobs = original_dump_blobs |
| 55 | + # Clean up |
| 56 | + if saver._owns_its_client: |
| 57 | + saver._redis.close() |
| 58 | + |
| 59 | + |
| 60 | +def test_numeric_version_fix(redis_url: str) -> None: |
| 61 | + """ |
| 62 | + Test that demonstrates the fix for issue #40. |
| 63 | +
|
| 64 | + This shows how to handle numeric versions correctly by ensuring |
| 65 | + they are converted to strings before being used with Tag. |
| 66 | + """ |
| 67 | + # Use our patched version that converts numeric versions to strings |
| 68 | + with patched_redis_saver(redis_url) as saver: |
| 69 | + # Set up a basic config |
| 70 | + config = { |
| 71 | + "configurable": { |
| 72 | + "thread_id": "thread-numeric-version-fix", |
| 73 | + "checkpoint_ns": "", |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + # Create a basic checkpoint |
| 78 | + checkpoint = empty_checkpoint() |
| 79 | + |
| 80 | + # Store the checkpoint with our patched method |
| 81 | + saved_config = saver.put( |
| 82 | + config, checkpoint, {}, {"test_channel": 1} |
| 83 | + ) # Numeric version |
| 84 | + |
| 85 | + # Get the checkpoint ID from the saved config |
| 86 | + thread_id = saved_config["configurable"]["thread_id"] |
| 87 | + checkpoint_ns = saved_config["configurable"].get("checkpoint_ns", "") |
| 88 | + |
| 89 | + # Now query the data - this should work with the fix |
| 90 | + query = f"@channel:{{test_channel}}" |
| 91 | + |
| 92 | + # This should not raise an error now with our patch |
| 93 | + results = saver.checkpoint_blobs_index.search(query) |
| 94 | + |
| 95 | + # Verify we can find the data |
| 96 | + assert len(results.docs) > 0 |
| 97 | + |
| 98 | + # Load one document and verify the version is a string |
| 99 | + doc = results.docs[0] |
| 100 | + data = saver._redis.json().get(doc.id) |
| 101 | + |
| 102 | + # The key test: version should be a string even though we passed a numeric value |
| 103 | + assert isinstance(data["version"], str) |
0 commit comments