Skip to content

Commit 38cddfd

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

File tree

4 files changed

+117
-1
lines changed

4 files changed

+117
-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: 114 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,107 @@ 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_all_parameters():
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_instance = AsyncMock()
1655+
mock_redis_instance = AsyncMock()
1656+
mock_pool.return_value = mock_pool_instance
1657+
mock_redis.return_value = mock_redis_instance
1658+
1659+
import stac_fastapi.core.redis_utils as redis_utils
1660+
redis_utils.redis_pool = None
1661+
1662+
result = await connect_redis(test_settings)
1663+
1664+
mock_pool.assert_called_once_with(
1665+
host="test-redis-host",
1666+
port=6380,
1667+
db=5,
1668+
max_connections=20,
1669+
decode_responses=False,
1670+
retry_on_timeout=False,
1671+
health_check_interval=50,
1672+
)
1673+
1674+
mock_redis.assert_called_once_with(
1675+
connection_pool=mock_pool_instance, client_name="custom-client"
1676+
)
1677+
1678+
assert result == mock_redis_instance
1679+
1680+
1681+
@pytest.mark.asyncio
1682+
async def test_connect_redis_sentinel(monkeypatch):
1683+
# Reset the global before the test
1684+
redis_utils.redis_pool = None
1685+
1686+
master_mock = AsyncMock()
1687+
1688+
# Sentinel itself is synchronous
1689+
sentinel_mock = MagicMock()
1690+
sentinel_mock.master_for.return_value = master_mock
1691+
1692+
# Patch Sentinel constructor
1693+
monkeypatch.setattr(
1694+
"stac_fastapi.core.redis_utils.Sentinel",
1695+
lambda *a, **k: sentinel_mock,
1696+
)
1697+
1698+
settings = RedisSentinelSettings(
1699+
REDIS_SENTINEL_HOSTS="localhost",
1700+
REDIS_SENTINEL_PORTS="26379",
1701+
REDIS_SENTINEL_MASTER_NAME="master",
1702+
)
1703+
1704+
redis = await connect_redis_sentinel(settings)
1705+
1706+
# Should return our injected master_mock
1707+
assert redis is master_mock
1708+
1709+
# Verify correct args passed
1710+
sentinel_mock.master_for.assert_called_once_with(
1711+
service_name="master",
1712+
db=0,
1713+
decode_responses=True,
1714+
retry_on_timeout=True,
1715+
client_name="stac-fastapi-app",
1716+
max_connections=10,
1717+
health_check_interval=30,
1718+
)
1719+
1720+
1721+
@pytest.mark.asyncio
1722+
async def test_save_and_get_prev_link(monkeypatch):
1723+
fake_redis = AsyncMock()
1724+
1725+
await save_self_link(fake_redis, "token123", "http://example.com/page2")
1726+
fake_redis.setex.assert_awaited_once_with(
1727+
"nav:self:token123", 1800, "http://example.com/page2"
1728+
)
1729+
1730+
fake_redis.get.return_value = "http://example.com/page2"
1731+
result = await get_prev_link(fake_redis, "token123")
1732+
assert result == "http://example.com/page2"
1733+
1734+
result_none = await get_prev_link(fake_redis, None)
1735+
assert result_none is None
1736+
1737+
await get_prev_link(fake_redis, "token456")
1738+
fake_redis.get.assert_awaited_with("nav:self:token456")

0 commit comments

Comments
 (0)