Skip to content

Commit 3d6fa1f

Browse files
authored
test: faiss - add unit tests (#3213)
1 parent eb4e065 commit 3d6fa1f

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

integrations/faiss/tests/test_document_store.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import pytest
66
from haystack.dataclasses import Document
7+
from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError
8+
from haystack.document_stores.types import DuplicatePolicy
79
from haystack.errors import FilterError
810
from haystack.testing.document_store import (
911
CountDocumentsByFilterTest,
@@ -22,6 +24,53 @@
2224
from haystack_integrations.document_stores.faiss import FAISSDocumentStore
2325

2426

27+
class TestFAISSDocumentStoreUnit:
28+
def test_create_new_index_with_invalid_factory_raises(self):
29+
with pytest.raises(DocumentStoreError, match="Could not create FAISS index"):
30+
FAISSDocumentStore(index_string="NotARealIndex", embedding_dim=3)
31+
32+
def test_get_index_or_raise_when_index_is_none(self):
33+
ds = FAISSDocumentStore(embedding_dim=3)
34+
ds.index = None
35+
with pytest.raises(DocumentStoreError, match="FAISS index has not been initialized"):
36+
ds._get_index_or_raise()
37+
38+
@pytest.mark.parametrize(
39+
"bad_input",
40+
[
41+
"a string",
42+
b"some bytes",
43+
123,
44+
[Document(content="ok"), "not a document"],
45+
],
46+
)
47+
def test_write_documents_rejects_invalid_input(self, bad_input):
48+
ds = FAISSDocumentStore(embedding_dim=3)
49+
with pytest.raises(ValueError, match="iterable of objects of type Document"):
50+
ds.write_documents(bad_input)
51+
52+
def test_write_documents_fail_policy_raises_on_duplicate(self):
53+
ds = FAISSDocumentStore(embedding_dim=3)
54+
ds.write_documents([Document(id="dup", content="first")])
55+
with pytest.raises(DuplicateDocumentError, match="already exists"):
56+
ds.write_documents([Document(id="dup", content="second")], policy=DuplicatePolicy.FAIL)
57+
58+
@pytest.mark.parametrize(
59+
("filters", "error_match"),
60+
[
61+
({"operator": "OR"}, "Missing 'conditions' for OR operator"),
62+
({"operator": "AND"}, "Missing 'conditions' for AND operator"),
63+
({"operator": "NOT"}, "Missing 'conditions' for NOT operator"),
64+
({"operator": "==", "field": 42, "value": "x"}, "'field' in filter condition must be a string"),
65+
],
66+
)
67+
def test_check_condition_invalid_structure_raises_filter_error(self, filters, error_match):
68+
ds = FAISSDocumentStore(embedding_dim=3)
69+
ds.write_documents([Document(content="test", meta={"category": "A"})])
70+
with pytest.raises(FilterError, match=error_match):
71+
ds.filter_documents(filters=filters)
72+
73+
2574
@pytest.mark.integration
2675
class TestFAISSDocumentStore(
2776
CountDocumentsTest,
@@ -61,7 +110,6 @@ def test_persistence(self, tmp_path):
61110
assert ds_loaded.filter_documents()[0].embedding == [0.1, 0.2, 0.3]
62111

63112
def test_persistence_no_embeddings(self, tmp_path):
64-
65113
path = tmp_path / "persistent_index_no_embed"
66114
ds = FAISSDocumentStore(index_path=str(path), embedding_dim=3)
67115

0 commit comments

Comments
 (0)