Skip to content

Commit 29f6ead

Browse files
committed
Adding delete_many method
1 parent c5bf9af commit 29f6ead

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

arangoasync/collection.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,3 +1447,77 @@ def response_handler(
14471447
return self.deserializer.loads_many(resp.raw_body)
14481448

14491449
return await self._executor.execute(request, response_handler)
1450+
1451+
async def delete_many(
1452+
self,
1453+
documents: Sequence[T],
1454+
wait_for_sync: Optional[bool] = None,
1455+
ignore_revs: Optional[bool] = None,
1456+
return_old: Optional[bool] = None,
1457+
silent: Optional[bool] = None,
1458+
refill_index_caches: Optional[bool] = None,
1459+
) -> Result[Jsons]:
1460+
"""Delete multiple documents.
1461+
1462+
Note:
1463+
If deleting a document fails, the exception is not raised but
1464+
returned as an object in the "errors" list. It is up to you to
1465+
inspect the list to determine which documents were deleted
1466+
successfully (returned as document metadata) and which were not
1467+
(returned as exception object).
1468+
1469+
Args:
1470+
documents (list): Documents to delete. An item must contain the "_key" or
1471+
"_id" field.
1472+
wait_for_sync (bool | None): Wait until documents have been synced to disk.
1473+
ignore_revs (bool | None): If this is set to `False`, then any `_rev`
1474+
attribute given in a body document is taken as a precondition. The
1475+
document is only updated if the current revision is the one
1476+
specified.
1477+
return_old (bool | None): Additionally return the complete old document
1478+
under the attribute `old` in the result.
1479+
silent (bool | None): If set to `True`, an empty object is returned as
1480+
response if all document operations succeed. No meta-data is returned
1481+
for the created documents. If any of the operations raises an error,
1482+
an array with the error object(s) is returned.
1483+
refill_index_caches (bool | None): Whether to add new entries to
1484+
in-memory index caches if document operations affect the edge index
1485+
or cache-enabled persistent indexes.
1486+
1487+
Returns:
1488+
list: Documents metadata (e.g. document id, key, revision) and
1489+
errors or just errors if **silent** is set to `True`.
1490+
1491+
Raises:
1492+
DocumentRemoveError: If removal fails.
1493+
1494+
References:
1495+
- `remove-multiple-documents <https://docs.arangodb.com/stable/develop/http-api/documents/#remove-multiple-documents>`__
1496+
""" # noqa: E501
1497+
params: Params = {}
1498+
if wait_for_sync is not None:
1499+
params["waitForSync"] = wait_for_sync
1500+
if ignore_revs is not None:
1501+
params["ignoreRevs"] = ignore_revs
1502+
if return_old is not None:
1503+
params["returnOld"] = return_old
1504+
if silent is not None:
1505+
params["silent"] = silent
1506+
if refill_index_caches is not None:
1507+
params["refillIndexCaches"] = refill_index_caches
1508+
1509+
request = Request(
1510+
method=Method.DELETE,
1511+
endpoint=f"/_api/document/{self.name}",
1512+
data=self._doc_serializer.dumps(documents),
1513+
params=params,
1514+
)
1515+
1516+
def response_handler(
1517+
resp: Response,
1518+
) -> Jsons:
1519+
if not resp.is_success:
1520+
raise DocumentDeleteError(resp, request)
1521+
return self.deserializer.loads_many(resp.raw_body)
1522+
1523+
return await self._executor.execute(request, response_handler)

tests/test_document.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,3 +409,46 @@ async def test_document_update_many(doc_col, bad_col, docs):
409409
assert len(result) == len(docs)
410410
for res in result:
411411
assert "error" in res
412+
413+
414+
@pytest.mark.asyncio
415+
async def test_document_delete_many(doc_col, bad_col, docs):
416+
# Check errors
417+
with pytest.raises(DocumentDeleteError):
418+
await bad_col.delete_many(docs)
419+
420+
# Empty list
421+
result = await doc_col.delete_many([])
422+
assert len(result) == 0
423+
424+
# Delete "not found" documents
425+
result = await doc_col.delete_many(docs, return_old=True)
426+
assert len(result) == len(docs)
427+
for res in result:
428+
assert "error" in res
429+
430+
# Delete successfully
431+
result = await doc_col.insert_many(docs, return_new=True)
432+
deleted = []
433+
for doc in result:
434+
deleted.append({"_key": doc["_key"], "val": 42})
435+
result = await doc_col.delete_many(deleted, return_old=True)
436+
assert len(result) == len(docs)
437+
438+
# Wrong and right rev
439+
result = await doc_col.insert_many(docs, return_new=True)
440+
deleted = [result[0]["new"], result[1]["new"]]
441+
deleted[1]["_rev"] = "foobar"
442+
result = await doc_col.delete_many(deleted, ignore_revs=False)
443+
assert "_key" in result[0]
444+
assert "error" in result[1]
445+
446+
# Silent mode
447+
await doc_col.truncate()
448+
_ = await doc_col.insert_many(docs)
449+
result = await doc_col.delete_many(docs, silent=True)
450+
assert len(result) == 0
451+
result = await doc_col.delete_many(docs, silent=True)
452+
assert len(result) == len(docs)
453+
for res in result:
454+
assert "error" in res

0 commit comments

Comments
 (0)