Skip to content

Commit a5c6278

Browse files
committed
Updating edge
1 parent 3eed2b2 commit a5c6278

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

arangoasync/collection.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,3 +2307,81 @@ def response_handler(resp: Response) -> Json:
23072307
raise DocumentInsertError(resp, request, msg)
23082308

23092309
return await self._executor.execute(request, response_handler)
2310+
2311+
async def update(
2312+
self,
2313+
edge: T,
2314+
wait_for_sync: Optional[bool] = None,
2315+
keep_null: Optional[bool] = None,
2316+
return_new: Optional[bool] = None,
2317+
return_old: Optional[bool] = None,
2318+
if_match: Optional[str] = None,
2319+
) -> Result[Json]:
2320+
"""Update a vertex in the graph.
2321+
2322+
Args:
2323+
edge (dict): Partial or full document with the updated values.
2324+
It must contain the "_key" or "_id" field, along with "_from" and
2325+
"_to" fields.
2326+
wait_for_sync (bool | None): Wait until document has been synced to disk.
2327+
keep_null (bool | None): If the intention is to delete existing attributes
2328+
with the patch command, set this parameter to `False`.
2329+
return_new (bool | None): Additionally return the complete new document
2330+
under the attribute `new` in the result.
2331+
return_old (bool | None): Additionally return the complete old document
2332+
under the attribute `old` in the result.
2333+
if_match (str | None): You can conditionally update a document based on a
2334+
target revision id by using the "if-match" HTTP header.
2335+
2336+
Returns:
2337+
dict: Document metadata (e.g. document id, key, revision).
2338+
If `return_new` or "return_old" are specified, the result contains
2339+
the document metadata in the "vertex" field and two additional fields
2340+
("new" and "old").
2341+
2342+
Raises:
2343+
DocumentUpdateError: If update fails.
2344+
2345+
References:
2346+
- `update-an-edge <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#update-an-edge>`__
2347+
""" # noqa: E501
2348+
params: Params = {}
2349+
if wait_for_sync is not None:
2350+
params["waitForSync"] = wait_for_sync
2351+
if keep_null is not None:
2352+
params["keepNull"] = keep_null
2353+
if return_new is not None:
2354+
params["returnNew"] = return_new
2355+
if return_old is not None:
2356+
params["returnOld"] = return_old
2357+
2358+
headers: RequestHeaders = {}
2359+
if if_match is not None:
2360+
headers["If-Match"] = if_match
2361+
2362+
request = Request(
2363+
method=Method.PATCH,
2364+
endpoint=f"/_api/gharial/{self._graph}/edge/"
2365+
f"{self._prep_from_doc(cast(Json, edge))}",
2366+
params=params,
2367+
headers=headers,
2368+
data=self._doc_serializer.dumps(edge),
2369+
)
2370+
2371+
def response_handler(resp: Response) -> Json:
2372+
if resp.is_success:
2373+
return self._parse_result(self.deserializer.loads(resp.raw_body))
2374+
msg: Optional[str] = None
2375+
if resp.status_code == HTTP_PRECONDITION_FAILED:
2376+
raise DocumentRevisionError(resp, request)
2377+
elif resp.status_code == HTTP_NOT_FOUND:
2378+
msg = (
2379+
"The graph cannot be found or the edge collection is not "
2380+
"part of the graph. It is also possible that the vertex "
2381+
"collection referenced in the _from or _to attribute is not part "
2382+
"of the graph or the vertex collection is part of the graph, but "
2383+
"does not exist. Finally check that _from or _to vertex do exist."
2384+
)
2385+
raise DocumentUpdateError(resp, request, msg)
2386+
2387+
return await self._executor.execute(request, response_handler)

arangoasync/graph.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,50 @@ async def insert_edge(
828828
wait_for_sync=wait_for_sync,
829829
return_new=return_new,
830830
)
831+
832+
async def update_edge(
833+
self,
834+
edge: T,
835+
wait_for_sync: Optional[bool] = None,
836+
keep_null: Optional[bool] = None,
837+
return_new: Optional[bool] = None,
838+
return_old: Optional[bool] = None,
839+
if_match: Optional[str] = None,
840+
) -> Result[Json]:
841+
"""Update a vertex in the graph.
842+
843+
Args:
844+
edge (dict): Partial or full document with the updated values.
845+
It must contain the "_key" or "_id" field, along with "_from" and
846+
"_to" fields.
847+
wait_for_sync (bool | None): Wait until document has been synced to disk.
848+
keep_null (bool | None): If the intention is to delete existing attributes
849+
with the patch command, set this parameter to `False`.
850+
return_new (bool | None): Additionally return the complete new document
851+
under the attribute `new` in the result.
852+
return_old (bool | None): Additionally return the complete old document
853+
under the attribute `old` in the result.
854+
if_match (str | None): You can conditionally update a document based on a
855+
target revision id by using the "if-match" HTTP header.
856+
857+
Returns:
858+
dict: Document metadata (e.g. document id, key, revision).
859+
If `return_new` or "return_old" are specified, the result contains
860+
the document metadata in the "vertex" field and two additional fields
861+
("new" and "old").
862+
863+
Raises:
864+
DocumentUpdateError: If update fails.
865+
866+
References:
867+
- `update-an-edge <https://docs.arangodb.com/stable/develop/http-api/graphs/named-graphs/#update-an-edge>`__
868+
""" # noqa: E501
869+
col = Collection.get_col_name(cast(Json | str, edge))
870+
return await self.edge_collection(col).update(
871+
edge,
872+
wait_for_sync=wait_for_sync,
873+
keep_null=keep_null,
874+
return_new=return_new,
875+
return_old=return_old,
876+
if_match=if_match,
877+
)

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def db_version():
245245
return global_data.db_version
246246

247247

248-
@pytest_asyncio.fixture(scope="session", autouse=True)
248+
@pytest_asyncio.fixture(autouse=True)
249249
async def teardown():
250250
yield
251251
async with ArangoClient(hosts=global_data.url) as client:

tests/test_graph.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ async def test_edge_collections(db, bad_graph):
271271
edge = await graph.edge(edge_id)
272272
assert edge is not None
273273

274+
# Update an edge
275+
edge["subject"] = "Advanced Math"
276+
updated_edge_meta = await graph.update_edge(edge, return_new=True, return_old=True)
277+
assert "new" in updated_edge_meta
278+
assert "old" in updated_edge_meta
279+
edge = await graph.edge(edge_id)
280+
assert edge["subject"] == "Advanced Math"
281+
274282
# Replace the edge definition
275283
new_from_collections = [students_col_name]
276284
new_to_collections = [teachers_col_name]

0 commit comments

Comments
 (0)