|
1 | 1 | # SPDX-FileCopyrightText: 2023-present Anant Corporation <support@anant.us> |
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
4 | 5 | import operator |
5 | 6 | import os |
6 | 7 | from unittest import mock |
@@ -210,6 +211,67 @@ def test_delete_all_documents(self, document_store: AstraDocumentStore): |
210 | 211 | document_store.delete_all_documents() |
211 | 212 | assert document_store.count_documents() == 0 |
212 | 213 |
|
| 214 | + def test_delete_by_filter(self, document_store: AstraDocumentStore, filterable_docs): |
| 215 | + document_store.write_documents(filterable_docs) |
| 216 | + initial_count = document_store.count_documents() |
| 217 | + assert initial_count > 0 |
| 218 | + |
| 219 | + # count documents that match the filter before deletion |
| 220 | + matching_docs = [d for d in filterable_docs if d.meta.get("chapter") == "intro"] |
| 221 | + expected_deleted_count = len(matching_docs) |
| 222 | + |
| 223 | + # delete all documents with chapter="intro" |
| 224 | + deleted_count = document_store.delete_by_filter( |
| 225 | + filters={"field": "meta.chapter", "operator": "==", "value": "intro"} |
| 226 | + ) |
| 227 | + |
| 228 | + assert deleted_count == expected_deleted_count |
| 229 | + assert document_store.count_documents() == initial_count - deleted_count |
| 230 | + |
| 231 | + # remaining documents don't have chapter="intro" |
| 232 | + remaining_docs = document_store.filter_documents() |
| 233 | + for doc in remaining_docs: |
| 234 | + assert doc.meta.get("chapter") != "intro" |
| 235 | + |
| 236 | + # all documents with chapter="intro" were deleted |
| 237 | + intro_docs = document_store.filter_documents( |
| 238 | + filters={"field": "meta.chapter", "operator": "==", "value": "intro"} |
| 239 | + ) |
| 240 | + assert len(intro_docs) == 0 |
| 241 | + |
| 242 | + def test_update_by_filter(self, document_store: AstraDocumentStore, filterable_docs): |
| 243 | + document_store.write_documents(filterable_docs) |
| 244 | + initial_count = document_store.count_documents() |
| 245 | + assert initial_count > 0 |
| 246 | + |
| 247 | + # count documents that match the filter before update |
| 248 | + matching_docs = [d for d in filterable_docs if d.meta.get("chapter") == "intro"] |
| 249 | + expected_updated_count = len(matching_docs) |
| 250 | + |
| 251 | + # update all documents with chapter="intro" to have status="updated" |
| 252 | + updated_count = document_store.update_by_filter( |
| 253 | + filters={"field": "meta.chapter", "operator": "==", "value": "intro"}, |
| 254 | + meta={"status": "updated"}, |
| 255 | + ) |
| 256 | + |
| 257 | + assert updated_count == expected_updated_count |
| 258 | + assert document_store.count_documents() == initial_count |
| 259 | + |
| 260 | + # verify the updated documents have the new metadata |
| 261 | + updated_docs = document_store.filter_documents( |
| 262 | + filters={"field": "meta.status", "operator": "==", "value": "updated"} |
| 263 | + ) |
| 264 | + assert len(updated_docs) == expected_updated_count |
| 265 | + for doc in updated_docs: |
| 266 | + assert doc.meta.get("chapter") == "intro" |
| 267 | + assert doc.meta.get("status") == "updated" |
| 268 | + |
| 269 | + # verify other documents weren't affected |
| 270 | + all_docs = document_store.filter_documents() |
| 271 | + for doc in all_docs: |
| 272 | + if doc.meta.get("chapter") != "intro": |
| 273 | + assert doc.meta.get("status") != "updated" |
| 274 | + |
213 | 275 | @pytest.mark.skip(reason="Unsupported filter operator not.") |
214 | 276 | def test_not_operator(self, document_store, filterable_docs): |
215 | 277 | pass |
|
0 commit comments