Skip to content

Commit

Permalink
test: add full-text retrieval test
Browse files Browse the repository at this point in the history
  • Loading branch information
kanenorman committed Dec 1, 2024
1 parent 9394e23 commit f20437a
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions integrations/mongodb_atlas/tests/test_fulltext_retrieval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# SPDX-FileCopyrightText: 2023-present deepset GmbH <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
import os
from typing import List, Union

import pytest

from haystack_integrations.document_stores.mongodb_atlas import MongoDBAtlasDocumentStore


@pytest.mark.skipif(
"MONGO_CONNECTION_STRING" not in os.environ,
reason="No MongoDB Atlas connection string provided",
)
@pytest.mark.integration
class TestFullTextRetrieval:

@pytest.fixture()
def document_store(self) -> MongoDBAtlasDocumentStore:
return MongoDBAtlasDocumentStore(
database_name="haystack_integration_test",
collection_name="test_full_text_collection",
vector_search_index="cosine_index",
full_text_search_index="full_text_index",
)

def test_query_retrieval(self, document_store: MongoDBAtlasDocumentStore):
results = document_store._fulltext_retrieval(query="fox", top_k=2)
assert len(results) == 2
for doc in results:
assert "fox" in doc.content
assert results[0].score >= results[1].score

def test_fuzzy_retrieval(self, document_store: MongoDBAtlasDocumentStore):
results = document_store._fulltext_retrieval(query="fax", fuzzy={"maxEdits": 1}, top_k=2)
assert len(results) == 2
for doc in results:
assert "fox" in doc.content
assert results[0].score >= results[1].score

def test_filters_retrieval(self, document_store: MongoDBAtlasDocumentStore):
filters = {"field": "meta.meta_field", "operator": "==", "value": "right_value"}

results = document_store._fulltext_retrieval(query="fox", top_k=3, filters=filters)
assert len(results) == 2
for doc in results:
assert "fox" in doc.content
assert doc.meta["meta_field"] == "right_value"

def test_synonyms_retrieval(self, document_store: MongoDBAtlasDocumentStore):
results = document_store._fulltext_retrieval(query="reynard", synonyms="synonym_mapping", top_k=2)
assert len(results) == 2
for doc in results:
assert "fox" in doc.content
assert results[0].score >= results[1].score

@pytest.mark.parametrize("query", ["", []])
def test_empty_query_raises_value_error(self, query: Union[str, List], document_store: MongoDBAtlasDocumentStore):
with pytest.raises(ValueError):
document_store._fulltext_retrieval(query=query)

def test_empty_synonyms_raises_value_error(self, document_store: MongoDBAtlasDocumentStore):
with pytest.raises(ValueError):
document_store._fulltext_retrieval(query="fox", synonyms="")

def test_synonyms_and_fuzzy_raises_value_error(self, document_store: MongoDBAtlasDocumentStore):
with pytest.raises(ValueError):
document_store._fulltext_retrieval(query="fox", synonyms="wolf", fuzzy={"maxEdits": 1})

0 comments on commit f20437a

Please sign in to comment.