Skip to content

Commit 9cc01a7

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

File tree

4 files changed

+119
-1
lines changed

4 files changed

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

0 commit comments

Comments
 (0)