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