Skip to content

Commit

Permalink
feat: Use HEAD HTTP method to check if a document exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkheir committed Jul 1, 2024
1 parent e44a3e8 commit efbfe7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
6 changes: 4 additions & 2 deletions arango/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def has(
headers["x-arango-allow-dirty-read"] = "true"

request = Request(
method="get",
method="head",
endpoint=f"/_api/document/{handle}",
headers=headers,
read=self.name,
Expand All @@ -641,9 +641,11 @@ def response_handler(resp: Response) -> bool:
return False
if resp.status_code == 412:
raise DocumentRevisionError(resp, request)
if resp.status_code == 404:
return False
if not resp.is_success:
raise DocumentInError(resp, request)
return bool(resp.body)
return True

return self._execute(request, response_handler)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,7 +1554,7 @@ def test_document_has(col, bad_col, docs):

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev, check_rev=True)
assert err.value.error_code == 1200
assert err.value.error_code in {412, 1200}

# Test existing documents with bad revision
for doc_input in [
Expand All @@ -1564,15 +1564,15 @@ def test_document_has(col, bad_col, docs):
]:
with assert_raises(DocumentRevisionError) as err:
col.has(doc_input)
assert err.value.error_code == 1200
assert err.value.error_code in {412, 1200}

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev)
assert err.value.error_code == 1200
assert err.value.error_code in {412, 1200}

with assert_raises(DocumentRevisionError) as err:
col.has(doc_input, rev=bad_rev, check_rev=True)
assert err.value.error_code == 1200
assert err.value.error_code in {412, 1200}

assert doc_input in col
assert col.has(doc_input, rev=rev, check_rev=True) is True
Expand Down Expand Up @@ -1651,12 +1651,12 @@ def test_document_has(col, bad_col, docs):
# Test get with bad database
with assert_raises(DocumentInError) as err:
bad_col.has(doc_key)
assert err.value.error_code in {11, 1228}
assert err.value.error_code in {11, 401}

# Test contains with bad database
with assert_raises(DocumentInError) as err:
assert doc_key in bad_col
assert err.value.error_code in {11, 1228}
assert err.value.error_code in {11, 401}


def test_document_get(col, bad_col, docs):
Expand Down

0 comments on commit efbfe7d

Please sign in to comment.