Skip to content

Commit 3295754

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
fix: implement recommendations
1 parent ab6e1db commit 3295754

File tree

7 files changed

+144
-11
lines changed

7 files changed

+144
-11
lines changed

Makefile

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ docker-shell-os:
6363

6464
.PHONY: test-elasticsearch
6565
test-elasticsearch:
66-
-$(run_es) /bin/bash -c 'export && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest'
67-
docker compose down
66+
docker compose -f compose-redis.yml up -d
67+
-$(run_es) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest'
68+
docker compose -f compose-redis.yml down
6869

69-
.PHONY: test-opensearch
70+
.PHONY: test-opensearch
7071
test-opensearch:
71-
-$(run_os) /bin/bash -c 'export && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest'
72-
docker compose down
72+
docker compose -f compose-redis.yml up -d
73+
-$(run_os) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest'
74+
docker compose -f compose-redis.yml down
7375

7476
.PHONY: test-datetime-filtering-es
7577
test-datetime-filtering-es:
@@ -82,7 +84,7 @@ test-datetime-filtering-os:
8284
docker compose down
8385

8486
.PHONY: test
85-
test: test-elasticsearch test-datetime-filtering-es test-opensearch test-datetime-filtering-os
87+
test: test-elasticsearch test-datetime-filtering-es test-opensearch test-datetime-filtering-os test-redis-es test-redis-os
8688

8789
.PHONY: run-database-es
8890
run-database-es:
@@ -117,4 +119,16 @@ docs-image:
117119
.PHONY: docs
118120
docs: docs-image
119121
docker compose -f compose.docs.yml \
120-
run docs
122+
run docs
123+
124+
.PHONY: test-redis-es
125+
test-redis-es:
126+
docker compose -f compose-redis.yml up -d
127+
-$(run_es) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh elasticsearch:9200 && cd stac_fastapi/tests/ && pytest redis/ -v'
128+
docker compose -f compose-redis.yml down
129+
130+
.PHONY: test-redis-os
131+
test-redis-os:
132+
docker compose -f compose-redis.yml up -d
133+
-$(run_os) /bin/bash -c 'export REDIS_ENABLE=true REDIS_HOST=redis REDIS_PORT=6379 && ./scripts/wait-for-it-es.sh opensearch:9202 && cd stac_fastapi/tests/ && pytest redis/ -v'
134+
docker compose -f compose-redis.yml down

compose-redis.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3.8'
2+
3+
services:
4+
redis:
5+
image: redis:7-alpine
6+
ports:
7+
- "6379:6379"
8+
volumes:
9+
- redis_test_data:/data
10+
command: redis-server --appendonly yes
11+
12+
volumes:
13+
redis_test_data:

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from stac_fastapi.core.datetime_utils import format_datetime_range
2727
from stac_fastapi.core.models.links import PagingLinks
2828
from stac_fastapi.core.redis_utils import (
29-
connect_redis_sentinel,
29+
connect_redis,
3030
get_prev_link,
3131
save_self_link,
3232
)
@@ -282,7 +282,7 @@ async def all_collections(
282282

283283
if redis_enable:
284284
try:
285-
redis = await connect_redis_sentinel()
285+
redis = await connect_redis()
286286
except Exception:
287287
redis = None
288288

@@ -565,7 +565,7 @@ async def post_search(
565565
redis = None
566566
if redis_enable:
567567
try:
568-
redis = await connect_redis_sentinel()
568+
redis = await connect_redis()
569569
except Exception:
570570
redis = None
571571

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class RedisSettings(BaseSettings):
3939

4040

4141
# Select the Redis or Redis Sentinel configuration
42-
redis_settings: BaseSettings = RedisSentinelSettings()
42+
redis_settings: BaseSettings = RedisSettings()
4343

4444

4545
async def connect_redis_sentinel(

stac_fastapi/tests/redis/__init__.py

Whitespace-only changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
from stac_fastapi.core.redis_utils import connect_redis, save_self_link, get_prev_link
3+
4+
5+
@pytest.mark.asyncio
6+
async def test_redis_connection():
7+
"""Test Redis connection."""
8+
redis = await connect_redis()
9+
assert redis is not None
10+
11+
# Test set/get
12+
await redis.set("string_key", "string_value")
13+
string_value = await redis.get("string_key")
14+
assert string_value == "string_value"
15+
16+
# Test key retrieval operation
17+
exists = await redis.exists("string_key")
18+
assert exists == 1
19+
20+
# Test key deletion
21+
await redis.delete("string_key")
22+
deleted_value = await redis.get("string_key")
23+
assert deleted_value is None
24+
25+
@pytest.mark.asyncio
26+
async def test_redis_utils_functions():
27+
redis = await connect_redis()
28+
assert redis is not None
29+
30+
token = "test_token_123"
31+
self_link = "http://mywebsite.com/search?token=test_token_123"
32+
33+
await save_self_link(redis, token, self_link)
34+
retrieved_link = await get_prev_link(redis, token)
35+
assert retrieved_link == self_link
36+
37+
await save_self_link(redis, None, "should_not_save")
38+
null_result = await get_prev_link(redis, None)
39+
assert null_result is None
40+
41+
non_existent = await get_prev_link(redis, "non_existent_token")
42+
assert non_existent is None

0 commit comments

Comments
 (0)