Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/tools/hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ async def hset(
r = RedisConnectionManager.get_connection()
r.hset(name, key, str(value))

if expire_seconds is not None:
# EXPIRE with 0 deletes the key, so treat 0 as "no expiration"
# (as lpush, rpush, zadd and xadd already do).
if expire_seconds:
r.expire(name, expire_seconds)

return f"Field '{key}' set successfully in hash '{name}'." + (
Expand Down
4 changes: 3 additions & 1 deletion src/tools/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ async def json_set(
r = RedisConnectionManager.get_connection()
r.json().set(name, path, parsed_value)

if expire_seconds is not None:
# EXPIRE with 0 deletes the key, so treat 0 as "no expiration"
# (as lpush, rpush, zadd and xadd already do).
if expire_seconds:
r.expire(name, expire_seconds)

return f"JSON value set at path '{path}' in '{name}'." + (
Expand Down
4 changes: 3 additions & 1 deletion src/tools/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ async def sadd(name: str, value: str, expire_seconds: Optional[int] = None) -> s
r = RedisConnectionManager.get_connection()
r.sadd(name, value)

if expire_seconds is not None:
# EXPIRE with 0 deletes the key, so treat 0 as "no expiration"
# (as lpush, rpush, zadd and xadd already do).
if expire_seconds:
r.expire(name, expire_seconds)

return f"Value '{value}' added successfully to set '{name}'." + (
Expand Down
14 changes: 14 additions & 0 deletions tests/tools/test_hash.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ async def test_hset_with_expiration(self, mock_redis_connection_manager):
mock_redis.expire.assert_called_once_with("test_hash", 60)
assert "Expires in 60 seconds." in result

@pytest.mark.asyncio
async def test_hset_zero_expiration_does_not_delete_key(
self, mock_redis_connection_manager
):
"""expire_seconds=0 must not call EXPIRE, which would delete the hash."""
mock_redis = mock_redis_connection_manager
mock_redis.hset.return_value = 1

result = await hset("test_hash", "field1", "value1", 0)

mock_redis.hset.assert_called_once_with("test_hash", "field1", "value1")
mock_redis.expire.assert_not_called()
assert "Expires" not in result

@pytest.mark.asyncio
async def test_hset_integer_value(self, mock_redis_connection_manager):
"""Test hash set operation with integer value."""
Expand Down
13 changes: 13 additions & 0 deletions tests/tools/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ async def test_json_set_with_expiration(
mock_redis.expire.assert_called_once_with("test_doc", 60)
assert "Expires in 60 seconds" in result

@pytest.mark.asyncio
async def test_json_set_zero_expiration_does_not_delete_key(
self, mock_redis_connection_manager
):
"""expire_seconds=0 must not call EXPIRE, which would delete the document."""
mock_redis = mock_redis_connection_manager
mock_redis.json.return_value.set.return_value = "OK"

result = await json_set("test_doc", "$.name", "John Updated", 0)

mock_redis.expire.assert_not_called()
assert "Expires" not in result

@pytest.mark.asyncio
async def test_json_set_nested_path(self, mock_redis_connection_manager):
"""Test JSON set operation with nested path."""
Expand Down
14 changes: 14 additions & 0 deletions tests/tools/test_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ async def test_sadd_with_expiration(self, mock_redis_connection_manager):
mock_redis.expire.assert_called_once_with("test_set", 60)
assert "Expires in 60 seconds" in result

@pytest.mark.asyncio
async def test_sadd_zero_expiration_does_not_delete_key(
self, mock_redis_connection_manager
):
"""expire_seconds=0 must not call EXPIRE, which would delete the set."""
mock_redis = mock_redis_connection_manager
mock_redis.sadd.return_value = 1

result = await sadd("test_set", "member1", 0)

mock_redis.sadd.assert_called_once_with("test_set", "member1")
mock_redis.expire.assert_not_called()
assert "Expires" not in result

@pytest.mark.asyncio
async def test_sadd_member_already_exists(self, mock_redis_connection_manager):
"""Test set add operation when member already exists."""
Expand Down
Loading