|
| 1 | +"""Tests for AsyncRedisStore search limits.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import pytest |
| 6 | +import pytest_asyncio |
| 7 | + |
| 8 | +from langgraph.store.redis import AsyncRedisStore |
| 9 | + |
| 10 | + |
| 11 | +@pytest_asyncio.fixture(scope="function") |
| 12 | +async def async_store(redis_url) -> AsyncRedisStore: |
| 13 | + """Fixture to create an AsyncRedisStore.""" |
| 14 | + async with AsyncRedisStore(redis_url) as store: |
| 15 | + await store.setup() # Initialize indices |
| 16 | + yield store |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.asyncio |
| 20 | +async def test_async_search_with_larger_limit(async_store: AsyncRedisStore) -> None: |
| 21 | + """Test async search with limit > 10.""" |
| 22 | + # Create 15 test documents |
| 23 | + for i in range(15): |
| 24 | + await async_store.aput( |
| 25 | + ("test_namespace",), f"key{i}", {"data": f"value{i}", "index": i} |
| 26 | + ) |
| 27 | + |
| 28 | + # Search with a limit of 15 |
| 29 | + results = await async_store.asearch(("test_namespace",), limit=15) |
| 30 | + |
| 31 | + # Should return all 15 results |
| 32 | + assert len(results) == 15, f"Expected 15 results, got {len(results)}" |
| 33 | + |
| 34 | + # Verify we have all the items |
| 35 | + result_keys = {item.key for item in results} |
| 36 | + expected_keys = {f"key{i}" for i in range(15)} |
| 37 | + assert result_keys == expected_keys |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_async_vector_search_with_larger_limit(redis_url) -> None: |
| 42 | + """Test async vector search with limit > 10.""" |
| 43 | + from tests.embed_test_utils import CharacterEmbeddings |
| 44 | + |
| 45 | + # Create vector store with embeddings |
| 46 | + embeddings = CharacterEmbeddings(dims=4) |
| 47 | + index_config = { |
| 48 | + "dims": embeddings.dims, |
| 49 | + "embed": embeddings, |
| 50 | + "distance_type": "cosine", |
| 51 | + "fields": ["text"], |
| 52 | + } |
| 53 | + |
| 54 | + async with AsyncRedisStore(redis_url, index=index_config) as store: |
| 55 | + await store.setup() |
| 56 | + |
| 57 | + # Create 15 test documents |
| 58 | + for i in range(15): |
| 59 | + # Create documents with slightly different texts |
| 60 | + await store.aput( |
| 61 | + ("test_namespace",), f"key{i}", {"text": f"sample text {i}", "index": i} |
| 62 | + ) |
| 63 | + |
| 64 | + # Search with a limit of 15 |
| 65 | + results = await store.asearch(("test_namespace",), query="sample", limit=15) |
| 66 | + |
| 67 | + # Should return all 15 results |
| 68 | + assert len(results) == 15, f"Expected 15 results, got {len(results)}" |
| 69 | + |
| 70 | + # Verify we have all the items |
| 71 | + result_keys = {item.key for item in results} |
| 72 | + expected_keys = {f"key{i}" for i in range(15)} |
| 73 | + assert result_keys == expected_keys |
0 commit comments