|
| 1 | +import pytest |
| 2 | +import uuid |
| 3 | +from ..conftest import create_item, create_collection |
| 4 | + |
| 5 | +@pytest.mark.asyncio |
| 6 | +async def test_search_pagination_uses_redis_cache(app_client, txn_client, load_test_data): |
| 7 | + """Test Redis caching and navigation for the /search endpoint.""" |
| 8 | + |
| 9 | + collection = load_test_data("test_collection.json") |
| 10 | + collection_id = f"test-pagination-collection-{uuid.uuid4()}" |
| 11 | + collection["id"] = collection_id |
| 12 | + await create_collection(txn_client, collection) |
| 13 | + |
| 14 | + for i in range(5): |
| 15 | + item = load_test_data("test_item.json") |
| 16 | + item["id"] = f"test-pagination-item-{uuid.uuid4()}" |
| 17 | + item["collection"] = collection_id |
| 18 | + await create_item(txn_client, item) |
| 19 | + |
| 20 | + resp = await app_client.post("/search", json={ |
| 21 | + "collections": [collection_id], |
| 22 | + "limit": 1 |
| 23 | + }) |
| 24 | + resp_json = resp.json() |
| 25 | + |
| 26 | + next_link = next((link for link in resp_json["links"] if link["rel"] == "next"), None) |
| 27 | + next_token = next_link["body"]["token"] |
| 28 | + |
| 29 | + # Expect the previous link on the second page to be retrieved from Redis cache |
| 30 | + resp2 = await app_client.post("/search", json={ |
| 31 | + "collections": [collection_id], |
| 32 | + "limit": 1, |
| 33 | + "token": next_token |
| 34 | + }) |
| 35 | + resp2_json = resp2.json() |
| 36 | + |
| 37 | + prev_link = next((link for link in resp2_json["links"] if link["rel"] == "prev"), None) |
| 38 | + assert prev_link is not None |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_collections_pagination_uses_redis_cache(app_client, txn_client, load_test_data): |
| 42 | + """Test Redis caching and navigation for the /collection endpoint.""" |
| 43 | + |
| 44 | + collection_data = load_test_data("test_collection.json") |
| 45 | + for i in range(5): |
| 46 | + collection = collection_data.copy() |
| 47 | + collection["id"] = f"test-collection-pagination-{uuid.uuid4()}" |
| 48 | + collection["title"] = f"Test Collection Pagination {i}" |
| 49 | + await create_collection(txn_client, collection) |
| 50 | + |
| 51 | + resp = await app_client.get("/collections", params={"limit": 1}) |
| 52 | + assert resp.status_code == 200 |
| 53 | + resp1_json = resp.json() |
| 54 | + |
| 55 | + next_link = next((link for link in resp1_json["links"] if link["rel"] == "next"), None) |
| 56 | + next_token = next_link["href"].split("token=")[1] |
| 57 | + |
| 58 | + # Expect the previous link on the second page to be retrieved from Redis cache |
| 59 | + resp2 = await app_client.get("/collections", params={"limit": 1, "token": next_token}) |
| 60 | + assert resp2.status_code == 200 |
| 61 | + resp2_json = resp2.json() |
| 62 | + |
| 63 | + prev_link = next((link for link in resp2_json["links"] if link["rel"] == "prev"), None) |
| 64 | + assert prev_link is not None |
0 commit comments