Skip to content

Commit ea4d4de

Browse files
committed
adding tests
1 parent a641fd3 commit ea4d4de

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

integrations/weaviate/tests/test_document_store.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,53 @@ def test_delete_all_documents_excessive_batch_size(self, document_store, caplog)
832832
document_store.delete_all_documents(batch_size=20000)
833833
assert document_store.count_documents() == 5
834834
assert "Not all documents have been deleted." in caplog.text
835+
836+
def test_delete_by_filter(self, document_store):
837+
docs = [
838+
Document(content="Doc 1", meta={"category": "A"}),
839+
Document(content="Doc 2", meta={"category": "B"}),
840+
Document(content="Doc 3", meta={"category": "A"}),
841+
]
842+
document_store.write_documents(docs)
843+
assert document_store.count_documents() == 3
844+
845+
# Delete documents with category="A"
846+
deleted_count = document_store.delete_by_filter(
847+
filters={"field": "meta.category", "operator": "==", "value": "A"}
848+
)
849+
assert deleted_count == 2
850+
assert document_store.count_documents() == 1
851+
852+
# Verify only category B remains
853+
remaining_docs = document_store.filter_documents()
854+
assert len(remaining_docs) == 1
855+
assert remaining_docs[0].meta["category"] == "B"
856+
857+
def test_update_by_filter(self, document_store):
858+
docs = [
859+
Document(content="Doc 1", meta={"category": "A", "status": "draft"}),
860+
Document(content="Doc 2", meta={"category": "B", "status": "draft"}),
861+
Document(content="Doc 3", meta={"category": "A", "status": "draft"}),
862+
]
863+
document_store.write_documents(docs)
864+
assert document_store.count_documents() == 3
865+
866+
# Update status for category="A" documents
867+
updated_count = document_store.update_by_filter(
868+
filters={"field": "meta.category", "operator": "==", "value": "A"}, meta={"status": "published"}
869+
)
870+
assert updated_count == 2
871+
872+
# Verify the updates
873+
published_docs = document_store.filter_documents(
874+
filters={"field": "meta.status", "operator": "==", "value": "published"}
875+
)
876+
assert len(published_docs) == 2
877+
for doc in published_docs:
878+
assert doc.meta["category"] == "A"
879+
assert doc.meta["status"] == "published"
880+
881+
# Verify category B still has draft status
882+
draft_docs = document_store.filter_documents(filters={"field": "meta.status", "operator": "==", "value": "draft"})
883+
assert len(draft_docs) == 1
884+
assert draft_docs[0].meta["category"] == "B"

integrations/weaviate/tests/test_document_store_async.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,55 @@ async def test_hybrid_retrieval_async_with_alpha(self, document_store):
161161
)
162162
assert len(result_vector) > 0
163163
assert result_vector[0].score > 0.0
164+
165+
@pytest.mark.asyncio
166+
async def test_delete_by_filter_async(self, document_store):
167+
docs = [
168+
Document(content="Doc 1", meta={"category": "A"}),
169+
Document(content="Doc 2", meta={"category": "B"}),
170+
Document(content="Doc 3", meta={"category": "A"}),
171+
]
172+
document_store.write_documents(docs)
173+
assert document_store.count_documents() == 3
174+
175+
# Delete documents with category="A"
176+
deleted_count = await document_store.delete_by_filter_async(
177+
filters={"field": "meta.category", "operator": "==", "value": "A"}
178+
)
179+
assert deleted_count == 2
180+
assert document_store.count_documents() == 1
181+
182+
# Verify only category B remains
183+
remaining_docs = document_store.filter_documents()
184+
assert len(remaining_docs) == 1
185+
assert remaining_docs[0].meta["category"] == "B"
186+
187+
@pytest.mark.asyncio
188+
async def test_update_by_filter_async(self, document_store):
189+
docs = [
190+
Document(content="Doc 1", meta={"category": "A", "status": "draft"}),
191+
Document(content="Doc 2", meta={"category": "B", "status": "draft"}),
192+
Document(content="Doc 3", meta={"category": "A", "status": "draft"}),
193+
]
194+
document_store.write_documents(docs)
195+
assert document_store.count_documents() == 3
196+
197+
# Update status for category="A" documents
198+
updated_count = await document_store.update_by_filter_async(
199+
filters={"field": "meta.category", "operator": "==", "value": "A"}, meta={"status": "published"}
200+
)
201+
assert updated_count == 2
202+
203+
# Verify the updates
204+
published_docs = document_store.filter_documents(
205+
filters={"field": "meta.status", "operator": "==", "value": "published"}
206+
)
207+
assert len(published_docs) == 2
208+
for doc in published_docs:
209+
assert doc.meta["category"] == "A"
210+
assert doc.meta["status"] == "published"
211+
212+
# Verify category B still has draft status
213+
draft_docs = document_store.filter_documents(filters={"field": "meta.status", "operator": "==", "value": "draft"})
214+
assert len(draft_docs) == 1
215+
assert draft_docs[0].meta["category"] == "B"

0 commit comments

Comments
 (0)