Compare commits

..

2 commits

Author SHA1 Message Date
c67826047d Better docstrings 2025-01-08 22:19:08 +01:00
fcefa41956 Combining test cases 2025-01-08 22:12:21 +01:00
3 changed files with 27 additions and 33 deletions

View file

@ -37,7 +37,10 @@ async def save_secret(data: Secret, redis: Redis) -> str:
@lru_cache
async def _is_getdel_available(redis: Redis) -> bool:
"""GETDEL is not available in Redis prior to version 6.2"""
"""Checks the availability of GETDEL command on the Redis server instance
GETDEL is not available in Redis prior to version 6.2
"""
try:
await redis.getdel("test:getdel:availability")
except ResponseError:
@ -47,6 +50,12 @@ async def _is_getdel_available(redis: Redis) -> bool:
async def getdel(redis: Redis, key: str) -> ResponseT:
"""Gets the value of key and deletes the key
Depending on the capabilities of Redis server this function
will either call GETDEL command, either first call GETSET with empty string
and DEL right after that.
"""
result: ResponseT
if await _is_getdel_available(redis):

View file

@ -1,5 +1,6 @@
from unittest.mock import AsyncMock, Mock, patch
from unittest.mock import AsyncMock, patch
import pytest
from redis.asyncio import Redis
from pssecret_server.utils import get_new_key, getdel, save_secret
@ -35,27 +36,14 @@ async def test_save_secret_data(redis_server: Redis) -> None:
assert redis_data.decode() == secret.data
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
async def test_getdel_when_available(
is_getdel_available: Mock, redis_server: Redis
@pytest.mark.parametrize("getdel_available", [True, False])
@patch("pssecret_server.utils._is_getdel_available", new_callable=AsyncMock)
async def test_getdel(
mock_is_getdel_available: AsyncMock,
getdel_available: bool,
redis_server: Redis,
) -> None:
is_getdel_available.side_effect.return_value = True
test_value = "test_data"
test_key = "test_key"
await redis_server.set(test_key, test_value)
result = await getdel(redis_server, test_key)
assert result.decode() == test_value
assert not await redis_server.exists(test_key)
@patch("pssecret_server.utils._is_getdel_available", side_effect=AsyncMock())
async def test_getdel_when_not_available(
is_getdel_available: Mock, redis_server: Redis
) -> None:
is_getdel_available.side_effect.return_value = False
mock_is_getdel_available.return_value = getdel_available
test_value = "test_data"
test_key = "test_key"

View file

@ -34,18 +34,15 @@ def test_secret_is_not_decryptable_by_random_key(fernet: Fernet):
decrypt_secret(encrypted_secret.data.encode(), random_fernet)
async def test_is_getdel_available_when_supported():
@pytest.mark.parametrize(
("getdel_effect", "expected_result"), [(None, True), (ResponseError, False)]
)
async def test_is_getdel_available(
getdel_effect: ResponseError | None, expected_result: bool
):
redis = AsyncMock()
redis.getdel.side_effect = getdel_effect # pyright: ignore[reportAny]
result = await _is_getdel_available(redis)
assert result is True
async def test_is_getdel_available_when_not_supported():
redis = AsyncMock()
redis.getdel.side_effect = ResponseError
result = await _is_getdel_available(redis)
assert result is False
assert result is expected_result