Skip to content

Commit ef0eca1

Browse files
committed
Adding support for /checksum
1 parent 73e54cf commit ef0eca1

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

arangoasync/collection.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
HTTP_PRECONDITION_FAILED,
1717
)
1818
from arangoasync.exceptions import (
19+
CollectionChecksumError,
1920
CollectionPropertiesError,
2021
CollectionResponsibleShardError,
2122
CollectionRevisionError,
@@ -688,6 +689,43 @@ def response_handler(resp: Response) -> str:
688689

689690
return await self._executor.execute(request, response_handler)
690691

692+
async def checksum(
693+
self, with_rev: Optional[bool] = None, with_data: Optional[bool] = None
694+
) -> Result[str]:
695+
"""Calculate collection checksum.
696+
697+
Args:
698+
with_rev (bool | None): Include document revisions in checksum calculation.
699+
with_data (bool | None): Include document data in checksum calculation.
700+
701+
Returns:
702+
str: Collection checksum.
703+
704+
Raises:
705+
CollectionChecksumError: If retrieval fails.
706+
707+
References:
708+
- `get-the-collection-checksum <https://docs.arangodb.com/stable/develop/http-api/collections/#get-the-collection-checksum>`__
709+
""" # noqa: E501
710+
params: Params = {}
711+
if with_rev is not None:
712+
params["withRevision"] = with_rev
713+
if with_data is not None:
714+
params["withData"] = with_data
715+
716+
request = Request(
717+
method=Method.GET,
718+
endpoint=f"/_api/collection/{self.name}/checksum",
719+
params=params,
720+
)
721+
722+
def response_handler(resp: Response) -> str:
723+
if not resp.is_success:
724+
raise CollectionChecksumError(resp, request)
725+
return cast(str, self.deserializer.loads(resp.raw_body)["checksum"])
726+
727+
return await self._executor.execute(request, response_handler)
728+
691729
async def has(
692730
self,
693731
document: str | Json,

arangoasync/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ class CollectionCreateError(ArangoServerError):
183183
"""Failed to create collection."""
184184

185185

186+
class CollectionChecksumError(ArangoServerError):
187+
"""Failed to retrieve collection checksum."""
188+
189+
186190
class CollectionDeleteError(ArangoServerError):
187191
"""Failed to delete collection."""
188192

tests/test_collection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from arangoasync.errno import DATA_SOURCE_NOT_FOUND, INDEX_NOT_FOUND
66
from arangoasync.exceptions import (
7+
CollectionChecksumError,
78
CollectionPropertiesError,
89
CollectionResponsibleShardError,
910
CollectionRevisionError,
@@ -27,6 +28,8 @@ def test_collection_attributes(db, doc_col):
2728

2829
@pytest.mark.asyncio
2930
async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
31+
doc = await doc_col.insert(docs[0])
32+
3033
# Properties
3134
properties = await doc_col.properties()
3235
assert properties.name == doc_col.name
@@ -44,7 +47,6 @@ async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
4447

4548
# Shards
4649
if cluster:
47-
doc = await doc_col.insert(docs[0])
4850
shard = await doc_col.responsible_shard(doc)
4951
assert isinstance(shard, str)
5052
with pytest.raises(CollectionResponsibleShardError):
@@ -60,6 +62,12 @@ async def test_collection_misc_methods(doc_col, bad_col, docs, cluster):
6062
with pytest.raises(CollectionRevisionError):
6163
await bad_col.revision()
6264

65+
# Checksum
66+
checksum = await doc_col.checksum(with_rev=True, with_data=True)
67+
assert isinstance(checksum, str)
68+
with pytest.raises(CollectionChecksumError):
69+
await bad_col.checksum()
70+
6371

6472
@pytest.mark.asyncio
6573
async def test_collection_index(doc_col, bad_col, cluster):

0 commit comments

Comments
 (0)