diff --git a/haystack/testing/document_store.py b/haystack/testing/document_store.py index 9cace29c798..17ec67d1817 100644 --- a/haystack/testing/document_store.py +++ b/haystack/testing/document_store.py @@ -1052,24 +1052,27 @@ def test_count_unique_metadata_by_filter_with_filter(document_store: DocumentSto @staticmethod def test_count_unique_metadata_by_filter_with_multiple_filters(document_store: DocumentStore): - """Test counting with multiple filters""" + """Test count_unique_metadata_by_filter() with multiple filters.""" docs = [ - Document(content="Doc 1", meta={"category": "A", "year": 2023}), - Document(content="Doc 2", meta={"category": "A", "year": 2024}), - Document(content="Doc 3", meta={"category": "B", "year": 2023}), - Document(content="Doc 4", meta={"category": "B", "year": 2024}), + Document(content="Doc 1", meta={"category": "B", "year": 2023, "status": "draft"}), + Document(content="Doc 2", meta={"category": "B", "year": 2023, "status": "draft"}), + Document(content="Doc 3", meta={"category": "B", "year": 2023, "status": "published"}), + Document(content="Doc 4", meta={"category": "B", "year": 2024, "status": "draft"}), ] document_store.write_documents(docs) - count = document_store.count_documents_by_filter( # type:ignore[attr-defined] + # The compound filter matches three documents with duplicated values, so the counts only come + # out right when the store both applies the filter and properly counts the values. + counts = document_store.count_unique_metadata_by_filter( # type:ignore[attr-defined] filters={ "operator": "AND", "conditions": [ {"field": "meta.category", "operator": "==", "value": "B"}, {"field": "meta.year", "operator": "==", "value": 2023}, ], - } + }, + metadata_fields=["status", "year"], ) - assert count == 1 + assert counts == {"status": 2, "year": 1} class GetMetadataFieldsInfoTest: diff --git a/haystack/testing/document_store_async.py b/haystack/testing/document_store_async.py index e8270381f34..fa469740a72 100644 --- a/haystack/testing/document_store_async.py +++ b/haystack/testing/document_store_async.py @@ -305,13 +305,15 @@ async def test_count_unique_metadata_by_filter_async_with_filter(document_store: async def test_count_unique_metadata_by_filter_async_with_multiple_filters(document_store: AsyncDocumentStore): """Test counting unique metadata asynchronously with multiple filters.""" docs = [ - Document(content="Doc 1", meta={"category": "A", "year": 2023}), - Document(content="Doc 2", meta={"category": "A", "year": 2024}), - Document(content="Doc 3", meta={"category": "B", "year": 2023}), - Document(content="Doc 4", meta={"category": "B", "year": 2024}), + Document(content="Doc 1", meta={"category": "B", "year": 2023, "status": "draft"}), + Document(content="Doc 2", meta={"category": "B", "year": 2023, "status": "draft"}), + Document(content="Doc 3", meta={"category": "B", "year": 2023, "status": "published"}), + Document(content="Doc 4", meta={"category": "B", "year": 2024, "status": "draft"}), ] await document_store.write_documents_async(docs) + # The compound filter matches three documents with duplicated values, so the counts only come + # out right when the store both applies the filter and properly counts the values. counts = await document_store.count_unique_metadata_by_filter_async( # type:ignore[attr-defined] filters={ "operator": "AND", @@ -320,9 +322,9 @@ async def test_count_unique_metadata_by_filter_async_with_multiple_filters(docum {"field": "meta.year", "operator": "==", "value": 2023}, ], }, - metadata_fields=["category", "year"], + metadata_fields=["status", "year"], ) - assert counts == {"category": 1, "year": 1} + assert counts == {"status": 2, "year": 1} class DeleteByFilterAsyncTest: diff --git a/releasenotes/notes/fix-count-unique-metadata-mixin-test-5415d442cdb70c15.yaml b/releasenotes/notes/fix-count-unique-metadata-mixin-test-5415d442cdb70c15.yaml new file mode 100644 index 00000000000..8f36d5b4f36 --- /dev/null +++ b/releasenotes/notes/fix-count-unique-metadata-mixin-test-5415d442cdb70c15.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - | + Fixed ``CountUniqueMetadataByFilterTest.test_count_unique_metadata_by_filter_with_multiple_filters`` + in the document store testing mixins: it called ``count_documents_by_filter`` instead of + ``count_unique_metadata_by_filter``. The test now exercises the intended method with a compound + filter, matching its async counterpart.