Skip to content

Commit 6972d24

Browse files
authored
Fix has_doc function (#346)
* Fix has_doc function Now only gets a specific document in a database instead of all documents. * Add integration test
1 parent c39b659 commit 6972d24

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

terminusdb_client/client/Client.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,16 +1481,33 @@ def has_doc(self, doc_id: str, graph_type: str = "instance") -> bool:
14811481
graph_type : str
14821482
Graph type, either "instance" or "schema".
14831483
1484+
Raises
1485+
------
1486+
InterfaceError
1487+
if the client does not connect to a database
1488+
14841489
Returns
14851490
-------
14861491
Bool
14871492
if the document exist
14881493
"""
14891494
self._validate_graph_type(graph_type)
14901495
self._check_connection()
1491-
all_existing_obj = self.get_all_documents(graph_type=graph_type)
1492-
all_existing_id = [x.get("@id") for x in all_existing_obj]
1493-
return doc_id in all_existing_id
1496+
1497+
response = requests.get(
1498+
self._documents_url(),
1499+
headers=self._default_headers,
1500+
json={"id": doc_id, "graph_type": graph_type},
1501+
auth=self._auth(),
1502+
)
1503+
try:
1504+
_finish_response(response)
1505+
return True
1506+
except DatabaseError as exception:
1507+
body = exception.error_obj
1508+
if exception.status_code == 404 and "api:error" in body and body["api:error"]["@type"] == "api:DocumentNotFound":
1509+
return False
1510+
raise exception
14941511

14951512
def get_class_frame(self, class_name):
14961513
"""Get the frame of the class of class_name. Provide information about all the avaliable properties of that class.

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,18 @@ def test_get_database(docker_url):
264264
client.get_database("DOES_NOT_EXISTDB")
265265

266266

267+
def test_has_doc(docker_url):
268+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
269+
client.connect()
270+
db_name = "testDB" + str(random())
271+
client.create_database(db_name, team="admin")
272+
client.connect(db=db_name)
273+
assert not client.has_doc("THIS_DOCUMENT_DOES_NOT_EXIST")
274+
client.insert_document({"test": "test"}, raw_json=True)
275+
doc_id = list(client.get_all_documents())[0]["@id"]
276+
assert client.has_doc(doc_id)
277+
278+
267279
def test_has_database(docker_url):
268280
client = Client(docker_url, user_agent=test_user_agent, team="admin")
269281
client.connect()

0 commit comments

Comments
 (0)