Skip to content

Commit f3b3519

Browse files
committed
feat: Use HEAD HTTP method to check if a document exists
1 parent 77cbc68 commit f3b3519

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

arango/collection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def has(
508508
handle, body, headers = self._prep_from_doc(document, rev, check_rev)
509509

510510
request = Request(
511-
method="get",
511+
method="head",
512512
endpoint=f"/_api/document/{handle}",
513513
headers=headers,
514514
read=self.name,
@@ -519,9 +519,11 @@ def response_handler(resp: Response) -> bool:
519519
return False
520520
if resp.status_code == 412:
521521
raise DocumentRevisionError(resp, request)
522+
if resp.status_code == 404:
523+
return False
522524
if not resp.is_success:
523525
raise DocumentInError(resp, request)
524-
return bool(resp.body)
526+
return True
525527

526528
return self._execute(request, response_handler)
527529

tests/test_document.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def test_document_has(col, bad_col, docs):
13901390

13911391
with assert_raises(DocumentRevisionError) as err:
13921392
col.has(doc_input, rev=bad_rev, check_rev=True)
1393-
assert err.value.error_code == 1200
1393+
assert err.value.error_code == 412
13941394

13951395
# Test existing documents with bad revision
13961396
for doc_input in [
@@ -1400,15 +1400,15 @@ def test_document_has(col, bad_col, docs):
14001400
]:
14011401
with assert_raises(DocumentRevisionError) as err:
14021402
col.has(doc_input)
1403-
assert err.value.error_code == 1200
1403+
assert err.value.error_code == 412
14041404

14051405
with assert_raises(DocumentRevisionError) as err:
14061406
col.has(doc_input, rev=bad_rev)
1407-
assert err.value.error_code == 1200
1407+
assert err.value.error_code == 412
14081408

14091409
with assert_raises(DocumentRevisionError) as err:
14101410
col.has(doc_input, rev=bad_rev, check_rev=True)
1411-
assert err.value.error_code == 1200
1411+
assert err.value.error_code == 412
14121412

14131413
assert doc_input in col
14141414
assert col.has(doc_input, rev=rev, check_rev=True) is True
@@ -1487,12 +1487,12 @@ def test_document_has(col, bad_col, docs):
14871487
# Test get with bad database
14881488
with assert_raises(DocumentInError) as err:
14891489
bad_col.has(doc_key)
1490-
assert err.value.error_code in {11, 1228}
1490+
assert err.value.error_code == 401
14911491

14921492
# Test contains with bad database
14931493
with assert_raises(DocumentInError) as err:
14941494
assert doc_key in bad_col
1495-
assert err.value.error_code in {11, 1228}
1495+
assert err.value.error_code == 401
14961496

14971497

14981498
def test_document_get(col, bad_col, docs):

0 commit comments

Comments
 (0)