Skip to content

Commit b3ac0b4

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

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,3 +1623,99 @@ async def test_use_datetime_false(app_client, load_test_data, txn_client, monkey
16231623

16241624
assert "test-item-datetime-only" not in found_ids
16251625
assert "test-item-start-end-only" in found_ids
1626+
1627+
import pytest
1628+
from unittest.mock import AsyncMock, MagicMock
1629+
1630+
import stac_fastapi.core.redis_utils as redis_utils
1631+
from stac_fastapi.core.redis_utils import (
1632+
connect_redis,
1633+
connect_redis_sentinel,
1634+
RedisSettings,
1635+
RedisSentinelSettings,
1636+
save_self_link,
1637+
get_prev_link,
1638+
)
1639+
1640+
1641+
@pytest.mark.asyncio
1642+
async def test_connect_redis(monkeypatch):
1643+
# Reset the global before the test
1644+
redis_utils.redis_pool = None
1645+
1646+
redis_mock = AsyncMock()
1647+
redis_pool_mock = AsyncMock()
1648+
1649+
# Patch aioredis classes
1650+
monkeypatch.setattr(
1651+
"stac_fastapi.core.redis_utils.aioredis.Redis",
1652+
lambda **kwargs: redis_mock,
1653+
)
1654+
monkeypatch.setattr(
1655+
"stac_fastapi.core.redis_utils.aioredis.ConnectionPool",
1656+
lambda **kwargs: redis_pool_mock,
1657+
)
1658+
1659+
settings = RedisSettings(REDIS_HOST="localhost", REDIS_PORT=6379)
1660+
redis = await connect_redis(settings)
1661+
1662+
assert redis is redis_mock
1663+
1664+
1665+
@pytest.mark.asyncio
1666+
async def test_connect_redis_sentinel(monkeypatch):
1667+
# Reset the global before the test
1668+
redis_utils.redis_pool = None
1669+
1670+
master_mock = AsyncMock()
1671+
1672+
# Sentinel itself is synchronous
1673+
sentinel_mock = MagicMock()
1674+
sentinel_mock.master_for.return_value = master_mock
1675+
1676+
# Patch Sentinel constructor
1677+
monkeypatch.setattr(
1678+
"stac_fastapi.core.redis_utils.Sentinel",
1679+
lambda *a, **k: sentinel_mock,
1680+
)
1681+
1682+
settings = RedisSentinelSettings(
1683+
REDIS_SENTINEL_HOSTS="localhost",
1684+
REDIS_SENTINEL_PORTS="26379",
1685+
REDIS_SENTINEL_MASTER_NAME="master",
1686+
)
1687+
1688+
redis = await connect_redis_sentinel(settings)
1689+
1690+
# Should return our injected master_mock
1691+
assert redis is master_mock
1692+
1693+
# Verify correct args passed
1694+
sentinel_mock.master_for.assert_called_once_with(
1695+
service_name="master",
1696+
db=0,
1697+
decode_responses=True,
1698+
retry_on_timeout=True,
1699+
client_name="stac-fastapi-app",
1700+
max_connections=10,
1701+
health_check_interval=30,
1702+
)
1703+
1704+
@pytest.mark.asyncio
1705+
async def test_save_and_get_prev_link(monkeypatch):
1706+
fake_redis = AsyncMock()
1707+
1708+
await save_self_link(fake_redis, "token123", "http://example.com/page2")
1709+
fake_redis.setex.assert_awaited_once_with(
1710+
"nav:self:token123", 1800, "http://example.com/page2"
1711+
)
1712+
1713+
fake_redis.get.return_value = "http://example.com/page2"
1714+
result = await get_prev_link(fake_redis, "token123")
1715+
assert result == "http://example.com/page2"
1716+
1717+
result_none = await get_prev_link(fake_redis, None)
1718+
assert result_none is None
1719+
1720+
await get_prev_link(fake_redis, "token456")
1721+
fake_redis.get.assert_awaited_with("nav:self:token456")

0 commit comments

Comments
 (0)