Skip to content

Commit a11c26e

Browse files
test: FaissDocumentStore use Mixin tests (#3024)
* using Mixin tests + updated parameter name to metadata_fields * chore: update haystack-ai dependency to >=2.26.1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * removing duplicated tests * removing duplicated tests * adding missing comma --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 414aa44 commit a11c26e

3 files changed

Lines changed: 14 additions & 51 deletions

File tree

integrations/faiss/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ classifiers = [
2424
"Programming Language :: Python :: Implementation :: PyPy",
2525
]
2626
dependencies = [
27-
"haystack-ai>=2.24.0",
27+
"haystack-ai>=2.26.1",
2828
"faiss-cpu>=1.8.0",
2929
"numpy>=1.22,<2; python_version < '3.13'",
3030
]

integrations/faiss/src/haystack_integrations/document_stores/faiss/document_store.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -489,18 +489,18 @@ def get_metadata_field_unique_values(self, field_name: str) -> list[Any]:
489489
values.add(val)
490490
return list(values)
491491

492-
def count_unique_metadata_by_filter(self, filters: dict[str, Any], fields: list[str]) -> dict[str, int]:
492+
def count_unique_metadata_by_filter(self, filters: dict[str, Any], metadata_fields: list[str]) -> dict[str, int]:
493493
"""
494494
Returns a count of unique values for multiple metadata fields, optionally scoped by a filter.
495495
496496
:param filters: A dictionary of filters to apply.
497-
:param fields: A list of metadata field names to count unique values for.
497+
:param metadata_fields: A list of metadata field names to count unique values for.
498498
:returns: A dictionary mapping each field name to the count of its unique values.
499499
"""
500500
filtered_docs = self.filter_documents(filters)
501501
counts = {}
502502

503-
for field in fields:
503+
for field in metadata_fields:
504504
unique_vals = set()
505505
for doc in filtered_docs:
506506
val = FAISSDocumentStore._get_doc_value(doc, field)

integrations/faiss/tests/test_document_store.py

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
from haystack.dataclasses import Document
77
from haystack.errors import FilterError
88
from haystack.testing.document_store import (
9+
CountDocumentsByFilterTest,
910
CountDocumentsTest,
11+
CountUniqueMetadataByFilterTest,
1012
DeleteAllTest,
1113
DeleteByFilterTest,
1214
DeleteDocumentsTest,
1315
FilterDocumentsTest,
16+
GetMetadataFieldMinMaxTest,
17+
GetMetadataFieldsInfoTest,
18+
GetMetadataFieldUniqueValuesTest,
1419
UpdateByFilterTest,
1520
)
1621

@@ -24,20 +29,23 @@ class TestFAISSDocumentStore(
2429
UpdateByFilterTest,
2530
DeleteAllTest,
2631
DeleteByFilterTest,
32+
CountDocumentsByFilterTest,
33+
CountUniqueMetadataByFilterTest,
34+
GetMetadataFieldsInfoTest,
35+
GetMetadataFieldMinMaxTest,
36+
GetMetadataFieldUniqueValuesTest,
2737
):
2838
@pytest.fixture
2939
def document_store(self, tmp_path):
3040
return FAISSDocumentStore(index_path=str(tmp_path / "test_index"))
3141

3242
def test_write_documents(self, document_store):
33-
3443
doc = Document(content="test")
3544
document_store.write_documents([doc])
3645
assert document_store.count_documents() == 1
3746
assert document_store.filter_documents()[0].id == doc.id
3847

3948
def test_persistence(self, tmp_path):
40-
4149
path = tmp_path / "persistent_index"
4250
ds = FAISSDocumentStore(index_path=str(path), embedding_dim=3)
4351

@@ -73,7 +81,6 @@ def test_load_missing_files(self, tmp_path):
7381
ds.load(path)
7482

7583
def test_search_with_and_without_filters(self, document_store):
76-
7784
# Setup documents with missing/varied embeddings to test edge cases
7885
doc1 = Document(content="test1", embedding=[0.1, 0.2, 0.3], meta={"category": "A"})
7986
doc2 = Document(content="test2", embedding=[0.4, 0.5, 0.6], meta={"category": "B"})
@@ -97,7 +104,6 @@ def test_search_with_and_without_filters(self, document_store):
97104

98105
def test_to_dict_from_dict(self):
99106
ds = FAISSDocumentStore(index_path="test_index", index_string="Flat", embedding_dim=128)
100-
101107
data = ds.to_dict()
102108
assert data["type"] == "haystack_integrations.document_stores.faiss.document_store.FAISSDocumentStore"
103109
assert data["init_parameters"]["index_path"] == "test_index"
@@ -109,50 +115,7 @@ def test_to_dict_from_dict(self):
109115
assert ds_loaded.index_string == "Flat"
110116
assert ds_loaded.embedding_dim == 128
111117

112-
def test_count_documents_by_filter(self, document_store):
113-
114-
docs = [
115-
Document(content="test1", meta={"category": "A"}),
116-
Document(content="test2", meta={"category": "B"}),
117-
Document(content="test3", meta={"category": "A"}),
118-
]
119-
document_store.write_documents(docs)
120-
121-
count = document_store.count_documents_by_filter(
122-
filters={"field": "meta.category", "operator": "==", "value": "A"}
123-
)
124-
assert count == 2
125-
126-
def test_get_metadata_fields_info(self, document_store):
127-
128-
docs = [Document(content="test1", meta={"category": "A", "count": 1, "is_active": True})]
129-
document_store.write_documents(docs)
130-
131-
info = document_store.get_metadata_fields_info()
132-
assert "category" in info
133-
assert info["category"]["type"] == "keyword"
134-
assert "count" in info
135-
assert info["count"]["type"] == "long"
136-
assert "is_active" in info
137-
assert info["is_active"]["type"] == "boolean"
138-
139-
def test_count_unique_metadata_by_filter(self, document_store):
140-
141-
docs = [
142-
Document(content="test1", meta={"category": "A", "status": "active"}),
143-
Document(content="test2", meta={"category": "B", "status": "inactive"}),
144-
Document(content="test3", meta={"category": "A", "status": "active"}),
145-
]
146-
document_store.write_documents(docs)
147-
148-
counts = document_store.count_unique_metadata_by_filter(
149-
filters={"field": "meta.category", "operator": "==", "value": "A"}, fields=["meta.status"]
150-
)
151-
assert "meta.status" in counts
152-
assert counts["meta.status"] == 1 # Only "active" status for category A
153-
154118
def test_not_filter_with_empty_conditions_raises_filter_error(self, document_store):
155119
document_store.write_documents([Document(content="test", meta={"category": "A"})])
156-
157120
with pytest.raises(FilterError, match="NOT operator expects at least one condition"):
158121
document_store.filter_documents(filters={"operator": "NOT", "conditions": []})

0 commit comments

Comments
 (0)