|
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 | from haystack.dataclasses import Document |
| 7 | +from haystack.document_stores.errors import DocumentStoreError, DuplicateDocumentError |
| 8 | +from haystack.document_stores.types import DuplicatePolicy |
7 | 9 | from haystack.errors import FilterError |
8 | 10 | from haystack.testing.document_store import ( |
9 | 11 | CountDocumentsByFilterTest, |
|
22 | 24 | from haystack_integrations.document_stores.faiss import FAISSDocumentStore |
23 | 25 |
|
24 | 26 |
|
| 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 | + |
25 | 74 | @pytest.mark.integration |
26 | 75 | class TestFAISSDocumentStore( |
27 | 76 | CountDocumentsTest, |
@@ -61,7 +110,6 @@ def test_persistence(self, tmp_path): |
61 | 110 | assert ds_loaded.filter_documents()[0].embedding == [0.1, 0.2, 0.3] |
62 | 111 |
|
63 | 112 | def test_persistence_no_embeddings(self, tmp_path): |
64 | | - |
65 | 113 | path = tmp_path / "persistent_index_no_embed" |
66 | 114 | ds = FAISSDocumentStore(index_path=str(path), embedding_dim=3) |
67 | 115 |
|
|
0 commit comments