fix(memory): reject empty search queries (#5258)
This commit is contained in:
@@ -26,6 +26,28 @@ def mock_memory_client():
|
||||
class TestSearchEntityParamRejection:
|
||||
"""Tests that top-level entity params are rejected in search()."""
|
||||
|
||||
@pytest.mark.parametrize("query", ["", " ", "\n\t"])
|
||||
def test_search_rejects_empty_query(self, mock_memory_client, query):
|
||||
"""search() should reject empty or whitespace-only queries before API calls."""
|
||||
with pytest.raises(ValueError, match="Invalid query.*empty or whitespace-only"):
|
||||
mock_memory_client.search(query, filters={"user_id": "u1"})
|
||||
|
||||
mock_memory_client.client.post.assert_not_called()
|
||||
|
||||
def test_search_trims_query_before_api_call(self, mock_memory_client):
|
||||
"""search() should send the normalized query to the API."""
|
||||
mock_response = MagicMock()
|
||||
mock_response.json.return_value = {"results": []}
|
||||
mock_response.raise_for_status.return_value = None
|
||||
mock_memory_client.client.post.return_value = mock_response
|
||||
|
||||
mock_memory_client.search(" test query ", filters={"user_id": "u1"})
|
||||
|
||||
mock_memory_client.client.post.assert_called_once_with(
|
||||
"/v3/memories/search/",
|
||||
json={"query": "test query", "filters": {"user_id": "u1"}},
|
||||
)
|
||||
|
||||
def test_search_rejects_user_id_kwarg(self, mock_memory_client):
|
||||
"""search() should reject user_id as top-level kwarg."""
|
||||
with pytest.raises(ValueError, match=r"user_id"):
|
||||
|
||||
@@ -290,6 +290,29 @@ class TestEntityIdValidation:
|
||||
class TestSearchParamValidation:
|
||||
"""Tests for search parameter validation (threshold and top_k)."""
|
||||
|
||||
@pytest.mark.parametrize("query", ["", " ", "\n\t"])
|
||||
def test_search_rejects_empty_query(self, memory_instance, query):
|
||||
"""Search should reject empty or whitespace-only queries before retrieval work."""
|
||||
memory_instance.embedding_model.embed = Mock()
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid query.*empty or whitespace-only"):
|
||||
memory_instance.search(query, filters={"user_id": "test"})
|
||||
|
||||
memory_instance.embedding_model.embed.assert_not_called()
|
||||
|
||||
def test_search_trims_query_before_embedding(self, memory_instance):
|
||||
"""Search should normalize leading/trailing whitespace before embedding."""
|
||||
mock_memories = []
|
||||
memory_instance.vector_store.search = Mock(return_value=mock_memories)
|
||||
memory_instance.vector_store.keyword_search = Mock(return_value=None)
|
||||
memory_instance.embedding_model.embed = Mock(return_value=[0.1, 0.2, 0.3])
|
||||
|
||||
with patch("mem0.memory.main.lemmatize_for_bm25", return_value="test"), \
|
||||
patch("mem0.memory.main.extract_entities", return_value=[]):
|
||||
memory_instance.search(" test ", filters={"user_id": "test"})
|
||||
|
||||
memory_instance.embedding_model.embed.assert_called_once_with("test", "search")
|
||||
|
||||
def test_search_rejects_threshold_above_1(self, memory_instance):
|
||||
"""Search should reject threshold > 1."""
|
||||
with pytest.raises(ValueError, match="Invalid threshold.*Must be between 0 and 1"):
|
||||
|
||||
Reference in New Issue
Block a user