From efbfe7d2dddefcb404155fb057375218b562a4ad Mon Sep 17 00:00:00 2001 From: Raphael Cohen Date: Fri, 21 Jan 2022 19:24:07 +0100 Subject: [PATCH 1/2] feat: Use HEAD HTTP method to check if a document exists --- arango/collection.py | 6 ++++-- tests/test_document.py | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/arango/collection.py b/arango/collection.py index 054b0a1e..c03c62b6 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -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, @@ -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) diff --git a/tests/test_document.py b/tests/test_document.py index 8e53ba26..8c755eb2 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -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 [ @@ -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 @@ -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): From 05eb769f155a934966539da6266bb9af79087bdf Mon Sep 17 00:00:00 2001 From: Raphael Cohen Date: Mon, 1 Jul 2024 14:29:50 +0200 Subject: [PATCH 2/2] feat: Applies PR review suggestion --- arango/collection.py | 2 -- tests/test_document.py | 12 ++++++------ 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/arango/collection.py b/arango/collection.py index c03c62b6..446200fb 100644 --- a/arango/collection.py +++ b/arango/collection.py @@ -637,8 +637,6 @@ def has( ) def response_handler(resp: Response) -> bool: - if resp.error_code == 1202: - return False if resp.status_code == 412: raise DocumentRevisionError(resp, request) if resp.status_code == 404: diff --git a/tests/test_document.py b/tests/test_document.py index 8c755eb2..805486fe 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -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 in {412, 1200} + assert err.value.error_code == 412 # Test existing documents with bad revision for doc_input in [ @@ -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 in {412, 1200} + assert err.value.error_code == 412 with assert_raises(DocumentRevisionError) as err: col.has(doc_input, rev=bad_rev) - assert err.value.error_code in {412, 1200} + assert err.value.error_code == 412 with assert_raises(DocumentRevisionError) as err: col.has(doc_input, rev=bad_rev, check_rev=True) - assert err.value.error_code in {412, 1200} + assert err.value.error_code == 412 assert doc_input in col assert col.has(doc_input, rev=rev, check_rev=True) is True @@ -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, 401} + assert err.value.error_code == 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, 401} + assert err.value.error_code == 401 def test_document_get(col, bad_col, docs):