Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61ba99b

Browse files
authoredMar 27, 2025··
Document CRUD operations (#41)
* Document update * Document replace * Adding delete and has, plus updating get method. * Adding get_many method * Adjusting typing information - The Json serializer must be able to deal with sequences * Adding truncate and count operations * Adding find method * Adding insert_many method * Adding update_many method * Adding update_many method * Adding delete_many method * Adding update_match * Adding replace_match * Adding delete_match
1 parent 10ac7b7 commit 61ba99b

File tree

6 files changed

+1741
-28
lines changed

6 files changed

+1741
-28
lines changed
 

‎arangoasync/aql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ def response_handler(resp: Response) -> Cursor:
327327
if not resp.is_success:
328328
raise AQLQueryExecuteError(resp, request)
329329
if self._executor.context == "async":
330-
# We cannot have a cursor getting back async jobs
330+
# We cannot have a cursor giving back async jobs
331331
executor: NonAsyncExecutor = DefaultApiExecutor(
332332
self._executor.connection
333333
)

‎arangoasync/collection.py

Lines changed: 1161 additions & 18 deletions
Large diffs are not rendered by default.

‎arangoasync/exceptions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ class ClientConnectionError(ArangoClientError):
187187
"""The request was unable to reach the server."""
188188

189189

190+
class CollectionTruncateError(ArangoServerError):
191+
"""Failed to truncate collection."""
192+
193+
190194
class CursorCloseError(ArangoServerError):
191195
"""Failed to delete the cursor result from server."""
192196

@@ -227,6 +231,14 @@ class DeserializationError(ArangoClientError):
227231
"""Failed to deserialize the server response."""
228232

229233

234+
class DocumentCountError(ArangoServerError):
235+
"""Failed to retrieve document count."""
236+
237+
238+
class DocumentDeleteError(ArangoServerError):
239+
"""Failed to delete document."""
240+
241+
230242
class DocumentGetError(ArangoServerError):
231243
"""Failed to retrieve document."""
232244

@@ -239,10 +251,18 @@ class DocumentParseError(ArangoClientError):
239251
"""Failed to parse document input."""
240252

241253

254+
class DocumentReplaceError(ArangoServerError):
255+
"""Failed to replace document."""
256+
257+
242258
class DocumentRevisionError(ArangoServerError):
243259
"""The expected and actual document revisions mismatched."""
244260

245261

262+
class DocumentUpdateError(ArangoServerError):
263+
"""Failed to update document."""
264+
265+
246266
class IndexCreateError(ArangoServerError):
247267
"""Failed to create collection index."""
248268

@@ -307,6 +327,10 @@ class ServerVersionError(ArangoServerError):
307327
"""Failed to retrieve server version."""
308328

309329

330+
class SortValidationError(ArangoClientError):
331+
"""Invalid sort parameters."""
332+
333+
310334
class TransactionAbortError(ArangoServerError):
311335
"""Failed to abort transaction."""
312336

‎arangoasync/serialization.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import json
1111
from abc import ABC, abstractmethod
12-
from typing import Generic, TypeVar
12+
from typing import Generic, Sequence, TypeVar
1313

1414
from arangoasync.exceptions import DeserializationError, SerializationError
1515
from arangoasync.typings import Json, Jsons
@@ -26,7 +26,7 @@ class Serializer(ABC, Generic[T]): # pragma: no cover
2626
"""
2727

2828
@abstractmethod
29-
def dumps(self, data: T) -> str:
29+
def dumps(self, data: T | Sequence[T | str]) -> str:
3030
"""Serialize any generic data.
3131
3232
Args:
@@ -87,7 +87,7 @@ def loads_many(self, data: bytes) -> U:
8787
class JsonSerializer(Serializer[Json]):
8888
"""JSON serializer."""
8989

90-
def dumps(self, data: T) -> str:
90+
def dumps(self, data: Json | Sequence[str | Json]) -> str:
9191
try:
9292
return json.dumps(data, separators=(",", ":"))
9393
except Exception as e:
@@ -104,10 +104,7 @@ def loads(self, data: bytes) -> Json:
104104
raise DeserializationError("Failed to deserialize data from JSON.") from e
105105

106106
def loads_many(self, data: bytes) -> Jsons:
107-
try:
108-
return json.loads(data) # type: ignore[no-any-return]
109-
except Exception as e:
110-
raise DeserializationError("Failed to deserialize data from JSON.") from e
107+
return self.loads(data) # type: ignore[return-value]
111108

112109

113110
DefaultSerializer = JsonSerializer

‎tests/test_collection.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from arangoasync.errno import DATA_SOURCE_NOT_FOUND, INDEX_NOT_FOUND
66
from arangoasync.exceptions import (
77
CollectionPropertiesError,
8+
CollectionTruncateError,
9+
DocumentCountError,
810
IndexCreateError,
911
IndexDeleteError,
1012
IndexGetError,
@@ -157,3 +159,26 @@ async def test_collection_index(doc_col, bad_col, cluster):
157159
await doc_col.delete_index(idx1.id)
158160
assert err.value.error_code == INDEX_NOT_FOUND
159161
assert await doc_col.delete_index(idx2.id, ignore_missing=True) is False
162+
163+
164+
@pytest.mark.asyncio
165+
async def test_collection_truncate_count(docs, doc_col, bad_col):
166+
# Test errors
167+
with pytest.raises(CollectionTruncateError):
168+
await bad_col.truncate()
169+
with pytest.raises(DocumentCountError):
170+
await bad_col.count()
171+
172+
# Test regular operations
173+
await asyncio.gather(*[doc_col.insert(doc) for doc in docs])
174+
cnt = await doc_col.count()
175+
assert cnt == len(docs)
176+
177+
await doc_col.truncate()
178+
cnt = await doc_col.count()
179+
assert cnt == 0
180+
181+
await asyncio.gather(*[doc_col.insert(doc) for doc in docs])
182+
await doc_col.truncate(wait_for_sync=True, compact=True)
183+
cnt = await doc_col.count()
184+
assert cnt == 0

‎tests/test_document.py

Lines changed: 526 additions & 2 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.