diff --git a/haystack/preview/document_stores/in_memory/document_store.py b/haystack/preview/document_stores/in_memory/document_store.py index 29510dd9f8..ff910e76d7 100644 --- a/haystack/preview/document_stores/in_memory/document_store.py +++ b/haystack/preview/document_stores/in_memory/document_store.py @@ -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( diff --git a/haystack/preview/testing/document_store.py b/haystack/preview/testing/document_store.py index dd9c652d21..65a173a0e5 100644 --- a/haystack/preview/testing/document_store.py +++ b/haystack/preview/testing/document_store.py @@ -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 @@ -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 @@ -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: