Skip to content

Commit 414ad66

Browse files
Copilotabrookins
andcommitted
Raise ValueError for empty attributes in delete_by_attributes
- Changed delete_by_attributes() to raise ValueError instead of returning {"deleted_entries_count": 0} - Changed adelete_by_attributes() to raise ValueError instead of returning {"deleted_entries_count": 0} - Updated docstrings to indicate attributes cannot be empty and document the ValueError - Updated tests to verify ValueError is raised with empty attributes - Error message: "Cannot delete by attributes with an empty attributes dictionary." Co-authored-by: abrookins <97182+abrookins@users.noreply.github.com>
1 parent cf800f9 commit 414ad66

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

redisvl/extensions/cache/llm/langcache.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,14 +582,18 @@ def delete_by_attributes(self, attributes: Dict[str, Any]) -> Dict[str, Any]:
582582
583583
Args:
584584
attributes (Dict[str, Any]): Attributes to match for deletion.
585-
If empty, no deletion is performed.
585+
Cannot be empty.
586586
587587
Returns:
588588
Dict[str, Any]: Result of the deletion operation.
589+
590+
Raises:
591+
ValueError: If attributes is an empty dictionary.
589592
"""
590593
if not attributes:
591-
# No attributes provided, return result with zero deletions
592-
return {"deleted_entries_count": 0}
594+
raise ValueError(
595+
"Cannot delete by attributes with an empty attributes dictionary."
596+
)
593597
result = self._client.delete_query(attributes=attributes)
594598
# Convert DeleteQueryResponse to dict
595599
return result.model_dump() if hasattr(result, "model_dump") else {}
@@ -599,14 +603,18 @@ async def adelete_by_attributes(self, attributes: Dict[str, Any]) -> Dict[str, A
599603
600604
Args:
601605
attributes (Dict[str, Any]): Attributes to match for deletion.
602-
If empty, no deletion is performed.
606+
Cannot be empty.
603607
604608
Returns:
605609
Dict[str, Any]: Result of the deletion operation.
610+
611+
Raises:
612+
ValueError: If attributes is an empty dictionary.
606613
"""
607614
if not attributes:
608-
# No attributes provided, return result with zero deletions
609-
return {"deleted_entries_count": 0}
615+
raise ValueError(
616+
"Cannot delete by attributes with an empty attributes dictionary."
617+
)
610618
result = await self._client.delete_query_async(attributes=attributes)
611619
# Convert DeleteQueryResponse to dict
612620
return result.model_dump() if hasattr(result, "model_dump") else {}

tests/unit/test_langcache_semantic_cache.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -455,24 +455,22 @@ def test_delete_by_attributes_with_valid_attributes(self, mock_langcache_client)
455455
assert result == {"deleted_entries_count": 5}
456456
mock_client.delete_query.assert_called_once_with(attributes={"topic": "python"})
457457

458-
def test_delete_by_attributes_with_empty_attributes_returns_empty(
458+
def test_delete_by_attributes_with_empty_attributes_raises_error(
459459
self, mock_langcache_client
460460
):
461-
"""Test that delete_by_attributes returns correct shape with empty attributes."""
462-
_, mock_client = mock_langcache_client
463-
461+
"""Test that delete_by_attributes raises ValueError with empty attributes."""
464462
cache = LangCacheSemanticCache(
465463
name="test",
466464
server_url="https://api.example.com",
467465
cache_id="test-cache",
468466
api_key="test-key",
469467
)
470468

471-
result = cache.delete_by_attributes({})
472-
473-
# Should return dict with correct shape without calling delete_query
474-
assert result == {"deleted_entries_count": 0}
475-
mock_client.delete_query.assert_not_called()
469+
with pytest.raises(
470+
ValueError,
471+
match="Cannot delete by attributes with an empty attributes dictionary",
472+
):
473+
cache.delete_by_attributes({})
476474

477475
@pytest.mark.asyncio
478476
async def test_adelete_by_attributes_with_valid_attributes(
@@ -500,24 +498,22 @@ async def test_adelete_by_attributes_with_valid_attributes(
500498
)
501499

502500
@pytest.mark.asyncio
503-
async def test_adelete_by_attributes_with_empty_attributes_returns_empty(
501+
async def test_adelete_by_attributes_with_empty_attributes_raises_error(
504502
self, mock_langcache_client
505503
):
506-
"""Test that async delete_by_attributes returns correct shape with empty attributes."""
507-
_, mock_client = mock_langcache_client
508-
504+
"""Test that async delete_by_attributes raises ValueError with empty attributes."""
509505
cache = LangCacheSemanticCache(
510506
name="test",
511507
server_url="https://api.example.com",
512508
cache_id="test-cache",
513509
api_key="test-key",
514510
)
515511

516-
result = await cache.adelete_by_attributes({})
517-
518-
# Should return dict with correct shape without calling delete_query_async
519-
assert result == {"deleted_entries_count": 0}
520-
mock_client.delete_query_async.assert_not_called()
512+
with pytest.raises(
513+
ValueError,
514+
match="Cannot delete by attributes with an empty attributes dictionary",
515+
):
516+
await cache.adelete_by_attributes({})
521517

522518
def test_update_not_supported(self, mock_langcache_client):
523519
"""Test that update raises NotImplementedError."""

0 commit comments

Comments
 (0)