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
This commit is contained in:
Massimiliano Pippi 2023-08-04 12:42:13 +02:00 committed by GitHub
parent d26d4201fc
commit c079576a87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 37 deletions

View File

View File

@ -1,3 +1,4 @@
# pylint: disable=too-many-public-methods
from typing import List from typing import List
import pytest import pytest
@ -52,10 +53,10 @@ class DocumentStoreBaseTests:
Document(content=pd.DataFrame([i]), content_type="table", metadata={"name": f"table_doc_{i}"}) Document(content=pd.DataFrame([i]), content_type="table", metadata={"name": f"table_doc_{i}"})
) )
documents.append( 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( 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 return documents
@ -142,7 +143,7 @@ class DocumentStoreBaseTests:
def test_incorrect_filter_type(self, docstore: Store, filterable_docs: List[Document]): def test_incorrect_filter_type(self, docstore: Store, filterable_docs: List[Document]):
docstore.write_documents(filterable_docs) docstore.write_documents(filterable_docs)
with pytest.raises(ValueError, match="dictionaries or lists"): 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 @pytest.mark.unit
def test_incorrect_filter_value(self, docstore: Store, filterable_docs: List[Document]): 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) embedding = np.zeros([768, 1]).astype(np.float32)
result = docstore.filter_documents(filters={"embedding": embedding}) result = docstore.filter_documents(filters={"embedding": embedding})
assert self.contains_same_docs( 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 @pytest.mark.unit
def test_in_filter_explicit(self, docstore: Store, filterable_docs: List[Document]): def test_in_filter_explicit(self, docstore: Store, filterable_docs: List[Document]):
docstore.write_documents(filterable_docs) docstore.write_documents(filterable_docs)
@ -289,18 +272,10 @@ class DocumentStoreBaseTests:
[ [
doc doc
for doc in filterable_docs 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 @pytest.mark.unit
def test_nin_filter_table(self, docstore: Store, filterable_docs: List[Document]): def test_nin_filter_table(self, docstore: Store, filterable_docs: List[Document]):
docstore.write_documents(filterable_docs) docstore.write_documents(filterable_docs)
@ -328,8 +303,8 @@ class DocumentStoreBaseTests:
for doc in filterable_docs for doc in filterable_docs
if not isinstance(doc.content, np.ndarray) if not isinstance(doc.content, np.ndarray)
or ( or (
not np.array_equal(embedding_zeros, doc.embedding) not np.array_equal(embedding_zeros, doc.embedding) # type: ignore
and not np.array_equal(embedding_ones, doc.embedding) 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 object.__setattr__(doc2, "id", doc1.id) # Make two docs with different content but same ID
docstore.write_documents([doc2]) 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) docstore.write_documents(documents=[doc1], policy=DuplicatePolicy.OVERWRITE)
assert docstore.filter_documents(filters={"id": doc1.id}) == [doc1] assert docstore.filter_documents(filters={"id": doc1.id}) == [doc1]
@pytest.mark.unit @pytest.mark.unit
def test_write_not_docs(self, docstore: Store): def test_write_not_docs(self, docstore: Store):
with pytest.raises(ValueError, match="Please provide a list of Documents"): 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 @pytest.mark.unit
def test_write_not_list(self, docstore: Store): def test_write_not_list(self, docstore: Store):
with pytest.raises(ValueError, match="Please provide a list of Documents"): 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 @pytest.mark.unit
def test_delete_empty(self, docstore: Store): def test_delete_empty(self, docstore: Store):

View File

@ -371,3 +371,8 @@ ignore_missing_imports = true
plugins = [ plugins = [
"pydantic.mypy", "pydantic.mypy",
] ]
[tool.coverage.run]
omit = [
"haystack/testing/*",
]

View File

@ -6,7 +6,7 @@ import pytest
from haystack.preview import Document from haystack.preview import Document
from haystack.preview.document_stores import Store, MemoryDocumentStore 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): class TestMemoryDocumentStore(DocumentStoreBaseTests):