Skip to content

Commit

Permalink
Rework delete_documents tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silvanocerza committed Nov 20, 2023
1 parent 8fcfe10 commit 44381f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
4 changes: 1 addition & 3 deletions haystack/preview/document_stores/in_memory/document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,11 @@ def write_documents(self, documents: List[Document], policy: DuplicatePolicy = D
def delete_documents(self, document_ids: List[str]) -> None:
"""
Deletes all documents with matching document_ids from the DocumentStore.
Fails with `MissingDocumentError` if no document with this id is present in the DocumentStore.
:param object_ids: The object_ids to delete.
"""
for doc_id in document_ids:
if doc_id not in self.storage.keys():
raise MissingDocumentError(f"ID '{doc_id}' not found, cannot delete it.")
continue
del self.storage[doc_id]

def bm25_retrieval(
Expand Down
35 changes: 22 additions & 13 deletions haystack/preview/testing/document_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from haystack.preview.dataclasses import Document
from haystack.preview.document_stores import DocumentStore, DuplicatePolicy
from haystack.preview.document_stores.errors import MissingDocumentError, DuplicateDocumentError
from haystack.preview.document_stores.errors import DuplicateDocumentError
from haystack.preview.errors import FilterError


Expand Down Expand Up @@ -121,6 +121,7 @@ class DeleteDocumentsTest:
Utility class to test a Document Store `delete_documents` method.
To use it create a custom test class and override the `document_store` fixture to return your Document Store.
The Document Store `write_documents` and `count_documents` methods must be implemented for this tests to work correctly.
Example usage:
```python
Expand All @@ -132,29 +133,37 @@ def document_store(self):
"""

@pytest.mark.unit
def test_delete_empty(self, document_store: DocumentStore):
with pytest.raises(MissingDocumentError):
document_store.delete_documents(["test"])

@pytest.mark.unit
def test_delete_not_empty(self, document_store: DocumentStore):
def test_delete_documents(self, document_store: DocumentStore):
"""
Test delete_documents() normal behaviour.
"""
doc = Document(content="test doc")
document_store.write_documents([doc])
assert document_store.count_documents() == 1

document_store.delete_documents([doc.id])
assert document_store.count_documents() == 0

with pytest.raises(Exception):
assert document_store.filter_documents(filters={"id": doc.id})
@pytest.mark.unit
def test_delete_documents_empty_document_store(self, document_store: DocumentStore):
"""
Test delete_documents() doesn't fail when called using an empty Document Store.
"""
document_store.delete_documents(["non_existing_id"])

@pytest.mark.unit
def test_delete_not_empty_nonexisting(self, document_store: DocumentStore):
def test_delete_documents_non_existing_document(self, document_store: DocumentStore):
"""
Test delete_documents() doesn't delete any Document when called with non existing id.
"""
doc = Document(content="test doc")
document_store.write_documents([doc])
assert document_store.count_documents() == 1

with pytest.raises(MissingDocumentError):
document_store.delete_documents(["non_existing"])
document_store.delete_documents(["non_existing_id"])

assert document_store.filter_documents(filters={"id": doc.id}) == [doc]
# No Document has been deleted
assert document_store.count_documents() == 1


class FilterableDocsFixtureMixin:
Expand Down

0 comments on commit 44381f5

Please sign in to comment.