From c079576a87259b46c0dac3535fb113606b1134de Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Fri, 4 Aug 2023 12:42:13 +0200 Subject: [PATCH] chore: move base test class into haystack core (#5509) * move base test class into haystack core * fix linter * do not compute coverage of testing code --- haystack/testing/preview/__init__.py | 0 .../testing/preview/document_store.py | 47 +++++-------------- pyproject.toml | 5 ++ test/preview/document_stores/test_memory.py | 2 +- 4 files changed, 17 insertions(+), 37 deletions(-) create mode 100644 haystack/testing/preview/__init__.py rename test/preview/document_stores/_base.py => haystack/testing/preview/document_store.py (94%) diff --git a/haystack/testing/preview/__init__.py b/haystack/testing/preview/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/test/preview/document_stores/_base.py b/haystack/testing/preview/document_store.py similarity index 94% rename from test/preview/document_stores/_base.py rename to haystack/testing/preview/document_store.py index 3f9497d65..05f72b813 100644 --- a/test/preview/document_stores/_base.py +++ b/haystack/testing/preview/document_store.py @@ -1,3 +1,4 @@ +# pylint: disable=too-many-public-methods from typing import List import pytest @@ -52,10 +53,10 @@ class DocumentStoreBaseTests: Document(content=pd.DataFrame([i]), content_type="table", metadata={"name": f"table_doc_{i}"}) ) documents.append( - Document(content=f"Doc {i} with zeros emb", metadata={"name": f"zeros_doc"}, embedding=embedding_zero) + Document(content=f"Doc {i} with zeros emb", metadata={"name": "zeros_doc"}, embedding=embedding_zero) ) documents.append( - Document(content=f"Doc {i} with ones emb", metadata={"name": f"ones_doc"}, embedding=embedding_one) + Document(content=f"Doc {i} with ones emb", metadata={"name": "ones_doc"}, embedding=embedding_one) ) return documents @@ -142,7 +143,7 @@ class DocumentStoreBaseTests: def test_incorrect_filter_type(self, docstore: Store, filterable_docs: List[Document]): docstore.write_documents(filterable_docs) with pytest.raises(ValueError, match="dictionaries or lists"): - docstore.filter_documents(filters="something odd") + docstore.filter_documents(filters="something odd") # type: ignore @pytest.mark.unit def test_incorrect_filter_value(self, docstore: Store, filterable_docs: List[Document]): @@ -193,27 +194,9 @@ class DocumentStoreBaseTests: embedding = np.zeros([768, 1]).astype(np.float32) result = docstore.filter_documents(filters={"embedding": embedding}) assert self.contains_same_docs( - result, [doc for doc in filterable_docs if np.array_equal(embedding, doc.embedding)] + result, [doc for doc in filterable_docs if np.array_equal(embedding, doc.embedding)] # type: ignore ) - @pytest.mark.unit - def test_deeper_incorrect_filter_nesting(self, docstore: Store, filterable_docs: List[Document]): - docstore.write_documents(filterable_docs) - with pytest.raises(ValueError, match="malformed"): - docstore.filter_documents(filters={"number": {"page": {"chapter": "intro"}}}) - - @pytest.mark.unit - def test_eq_filter_explicit(self, docstore: Store, filterable_docs: List[Document]): - docstore.write_documents(filterable_docs) - result = docstore.filter_documents(filters={"page": {"$eq": "100"}}) - assert self.contains_same_docs(result, [doc for doc in filterable_docs if doc.metadata.get("page") == "100"]) - - @pytest.mark.unit - def test_eq_filter_implicit(self, docstore: Store, filterable_docs: List[Document]): - docstore.write_documents(filterable_docs) - result = docstore.filter_documents(filters={"page": "100"}) - assert self.contains_same_docs(result, [doc for doc in filterable_docs if doc.metadata.get("page") == "100"]) - @pytest.mark.unit def test_in_filter_explicit(self, docstore: Store, filterable_docs: List[Document]): docstore.write_documents(filterable_docs) @@ -289,18 +272,10 @@ class DocumentStoreBaseTests: [ doc for doc in filterable_docs - if not isinstance(doc.content, np.ndarray) or not np.array_equal(embedding, doc.embedding) + if not isinstance(doc.content, np.ndarray) or not np.array_equal(embedding, doc.embedding) # type: ignore ], ) - @pytest.mark.unit - def test_nin_filter(self, docstore: Store, filterable_docs: List[Document]): - docstore.write_documents(filterable_docs) - result = docstore.filter_documents(filters={"page": {"$nin": ["100", "123", "n.a."]}}) - assert self.contains_same_docs( - result, [doc for doc in filterable_docs if doc.metadata.get("page", None) not in ["100", "123"]] - ) - @pytest.mark.unit def test_nin_filter_table(self, docstore: Store, filterable_docs: List[Document]): docstore.write_documents(filterable_docs) @@ -328,8 +303,8 @@ class DocumentStoreBaseTests: for doc in filterable_docs if not isinstance(doc.content, np.ndarray) or ( - not np.array_equal(embedding_zeros, doc.embedding) - and not np.array_equal(embedding_ones, doc.embedding) + not np.array_equal(embedding_zeros, doc.embedding) # type: ignore + and not np.array_equal(embedding_ones, doc.embedding) # type: ignore ) ], ) @@ -696,19 +671,19 @@ class DocumentStoreBaseTests: object.__setattr__(doc2, "id", doc1.id) # Make two docs with different content but same ID docstore.write_documents([doc2]) - docstore.filter_documents(filters={"id": doc1.id}) == [doc2] + assert docstore.filter_documents(filters={"id": doc1.id}) == [doc2] docstore.write_documents(documents=[doc1], policy=DuplicatePolicy.OVERWRITE) assert docstore.filter_documents(filters={"id": doc1.id}) == [doc1] @pytest.mark.unit def test_write_not_docs(self, docstore: Store): with pytest.raises(ValueError, match="Please provide a list of Documents"): - docstore.write_documents(["not a document for sure"]) + docstore.write_documents(["not a document for sure"]) # type: ignore @pytest.mark.unit def test_write_not_list(self, docstore: Store): with pytest.raises(ValueError, match="Please provide a list of Documents"): - docstore.write_documents("not a list actually") + docstore.write_documents("not a list actually") # type: ignore @pytest.mark.unit def test_delete_empty(self, docstore: Store): diff --git a/pyproject.toml b/pyproject.toml index 5d3c9ce2b..fa310efb8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -371,3 +371,8 @@ ignore_missing_imports = true plugins = [ "pydantic.mypy", ] + +[tool.coverage.run] +omit = [ + "haystack/testing/*", +] diff --git a/test/preview/document_stores/test_memory.py b/test/preview/document_stores/test_memory.py index 69206d44d..c2db35ba4 100644 --- a/test/preview/document_stores/test_memory.py +++ b/test/preview/document_stores/test_memory.py @@ -6,7 +6,7 @@ import pytest from haystack.preview import Document from haystack.preview.document_stores import Store, MemoryDocumentStore -from test.preview.document_stores._base import DocumentStoreBaseTests +from haystack.testing.preview.document_store import DocumentStoreBaseTests class TestMemoryDocumentStore(DocumentStoreBaseTests):