Skip to content

Commit 42d80a7

Browse files
authored
Add has_database function and correct docstring of get_database (#341)
has_database is better to check for existance of a database rather than using connect or catch the exception of get_database
1 parent ab5fe56 commit 42d80a7

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

terminusdb_client/client/Client.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2799,10 +2799,12 @@ def get_database(self, dbid: str, team: Optional[str] = None) -> Optional[dict]:
27992799
------
28002800
InterfaceError
28012801
if the client does not connect to a server
2802+
DatabaseError
2803+
if the database can't be found
28022804
28032805
Returns
28042806
-------
2805-
dict or None if not found
2807+
dict
28062808
"""
28072809
self._check_connection(check_db=False)
28082810
team = team if team else self.team
@@ -2813,6 +2815,35 @@ def get_database(self, dbid: str, team: Optional[str] = None) -> Optional[dict]:
28132815
)
28142816
return json.loads(_finish_response(result))
28152817

2818+
def has_database(self, dbid: str, team: Optional[str] = None) -> bool:
2819+
"""
2820+
Check whether a database exists
2821+
2822+
Parameters
2823+
----------
2824+
dbid : str
2825+
The id of the database
2826+
team : str
2827+
The organization of the database (default self.team)
2828+
2829+
Raises
2830+
------
2831+
InterfaceError
2832+
if the client does not connect to a server
2833+
2834+
Returns
2835+
-------
2836+
True or False if not found
2837+
"""
2838+
self._check_connection(check_db=False)
2839+
team = team if team else self.team
2840+
r = requests.head(
2841+
f"{self.api}/db/{team}/{dbid}",
2842+
headers=self._default_headers,
2843+
auth=self._auth(),
2844+
)
2845+
return r.status_code == 200
2846+
28162847
def get_databases(self) -> List[dict]:
28172848
"""
28182849
Returns a list of database metadata records for all databases the user has access to

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ def test_get_database(docker_url):
175175
assert db['name'] == db_name
176176
db_with_team = client.get_database(db_name, team='admin')
177177
assert db_with_team['name'] == db_name
178+
with pytest.raises(DatabaseError):
179+
client.get_database("DOES_NOT_EXISTDB")
180+
181+
182+
def test_has_database(docker_url):
183+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
184+
client.connect()
185+
db_name = "testDB" + str(random())
186+
client.create_database(db_name, team="admin")
187+
assert client.has_database(db_name)
188+
assert not client.has_database("DOES_NOT_EXISTDB")
178189

179190

180191
def test_add_get_remove_user(docker_url):

terminusdb_client/tests/test_Client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,20 @@ def test_get_database(mocked_requests, mocked_requests2):
181181
)
182182

183183

184+
@mock.patch("requests.get", side_effect=mocked_request_success)
185+
@mock.patch("requests.head", side_effect=mocked_request_success)
186+
def test_has_database(mocked_requests, mocked_requests2):
187+
client = Client("http://localhost:6363")
188+
client.connect(user="admin", team="admin", key="root")
189+
db_name = "testDB" + str(random.randrange(100000))
190+
client.has_database(db_name)
191+
requests.head.assert_called_with(
192+
f"http://localhost:6363/api/db/admin/{db_name}",
193+
auth=("admin", "root"),
194+
headers={"user-agent": f"terminusdb-client-python/{__version__}"},
195+
)
196+
197+
184198
@pytest.mark.skip(reason="temporary not avaliable")
185199
@mock.patch("requests.head", side_effect=mocked_request_success)
186200
@mock.patch("requests.get", side_effect=mocked_request_success)

0 commit comments

Comments
 (0)