Skip to content

Commit d9048c7

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
tests: adding redis tests
1 parent ba6728d commit d9048c7

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

stac_fastapi/elasticsearch/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"elasticsearch[async]~=8.18.0",
1212
"uvicorn~=0.23.0",
1313
"starlette>=0.35.0,<0.36.0",
14+
"redis==6.4.0",
1415
]
1516

1617
extra_reqs = {

stac_fastapi/opensearch/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"opensearch-py[async]~=2.8.0",
1313
"uvicorn~=0.23.0",
1414
"starlette>=0.35.0,<0.36.0",
15+
"redis==6.4.0",
1516
]
1617

1718
extra_reqs = {

stac_fastapi/sfeos_helpers/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
install_requires = [
99
"stac-fastapi.core==6.4.0",
10+
"redis==6.4.0",
1011
]
1112

1213
setup(

stac_fastapi/tests/api/test_api.py

Lines changed: 115 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33
import uuid
44
from copy import deepcopy
55
from datetime import datetime, timedelta
6-
from unittest.mock import patch
6+
from unittest.mock import AsyncMock, MagicMock, patch
77

88
import pytest
99

10+
import stac_fastapi.core.redis_utils as redis_utils
11+
from stac_fastapi.core.redis_utils import (
12+
RedisSentinelSettings,
13+
RedisSettings,
14+
connect_redis,
15+
connect_redis_sentinel,
16+
get_prev_link,
17+
save_self_link,
18+
)
1019
from stac_fastapi.types.errors import ConflictError
1120

1221
from ..conftest import create_collection, create_item
@@ -1623,3 +1632,108 @@ async def test_use_datetime_false(app_client, load_test_data, txn_client, monkey
16231632

16241633
assert "test-item-datetime-only" not in found_ids
16251634
assert "test-item-start-end-only" in found_ids
1635+
1636+
1637+
@pytest.mark.asyncio
1638+
async def test_connect_redis():
1639+
test_settings = RedisSettings(
1640+
REDIS_HOST="test-redis-host",
1641+
REDIS_PORT=6380,
1642+
REDIS_DB=5,
1643+
REDIS_MAX_CONNECTIONS=20,
1644+
REDIS_RETRY_TIMEOUT=False,
1645+
REDIS_DECODE_RESPONSES=False,
1646+
REDIS_CLIENT_NAME="custom-client",
1647+
REDIS_HEALTH_CHECK_INTERVAL=50,
1648+
)
1649+
1650+
with patch(
1651+
"stac_fastapi.core.redis_utils.aioredis.ConnectionPool"
1652+
) as mock_pool, patch("stac_fastapi.core.redis_utils.aioredis.Redis") as mock_redis:
1653+
1654+
mock_pool = AsyncMock()
1655+
mock_redis = AsyncMock()
1656+
mock_pool.return_value = mock_pool
1657+
mock_redis.return_value = mock_redis
1658+
1659+
import stac_fastapi.core.redis_utils as redis_utils
1660+
1661+
redis_utils.redis_pool = None
1662+
1663+
result = await connect_redis(test_settings)
1664+
1665+
mock_pool.assert_called_once_with(
1666+
host="test-redis-host",
1667+
port=6380,
1668+
db=5,
1669+
max_connections=20,
1670+
decode_responses=False,
1671+
retry_on_timeout=False,
1672+
health_check_interval=50,
1673+
)
1674+
1675+
mock_redis.assert_called_once_with(
1676+
connection_pool=mock_pool, client_name="custom-client"
1677+
)
1678+
1679+
assert result == mock_redis
1680+
1681+
1682+
@pytest.mark.asyncio
1683+
async def test_connect_redis_sentinel(monkeypatch):
1684+
# Reset the global before the test
1685+
redis_utils.redis_pool = None
1686+
1687+
master_mock = AsyncMock()
1688+
1689+
# Sentinel itself is synchronous
1690+
sentinel_mock = MagicMock()
1691+
sentinel_mock.master_for.return_value = master_mock
1692+
1693+
# Patch Sentinel constructor
1694+
monkeypatch.setattr(
1695+
"stac_fastapi.core.redis_utils.Sentinel",
1696+
lambda *a, **k: sentinel_mock,
1697+
)
1698+
1699+
settings = RedisSentinelSettings(
1700+
REDIS_SENTINEL_HOSTS="localhost",
1701+
REDIS_SENTINEL_PORTS="26379",
1702+
REDIS_SENTINEL_MASTER_NAME="master",
1703+
)
1704+
1705+
redis = await connect_redis_sentinel(settings)
1706+
1707+
# Should return our injected master_mock
1708+
assert redis is master_mock
1709+
1710+
# Verify correct args passed
1711+
sentinel_mock.master_for.assert_called_once_with(
1712+
service_name="master",
1713+
db=0,
1714+
decode_responses=True,
1715+
retry_on_timeout=True,
1716+
client_name="stac-fastapi-app",
1717+
max_connections=10,
1718+
health_check_interval=30,
1719+
)
1720+
1721+
1722+
@pytest.mark.asyncio
1723+
async def test_save_and_get_prev_link(monkeypatch):
1724+
fake_redis = AsyncMock()
1725+
1726+
await save_self_link(fake_redis, "token123", "http://example.com/page2")
1727+
fake_redis.setex.assert_awaited_once_with(
1728+
"nav:self:token123", 1800, "http://example.com/page2"
1729+
)
1730+
1731+
fake_redis.get.return_value = "http://example.com/page2"
1732+
result = await get_prev_link(fake_redis, "token123")
1733+
assert result == "http://example.com/page2"
1734+
1735+
result_none = await get_prev_link(fake_redis, None)
1736+
assert result_none is None
1737+
1738+
await get_prev_link(fake_redis, "token456")
1739+
fake_redis.get.assert_awaited_with("nav:self:token456")

0 commit comments

Comments
 (0)