Skip to content

Commit ccc3b00

Browse files
authored
Implement efficient get_database function (#335)
I always use the ?verbose=true parameter because it remains backwards compatible with the existing call this way, as every property that was previously fetched is now fetched as well.
1 parent 30d53f6 commit ccc3b00

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

terminusdb_client/client/Client.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,13 +2705,15 @@ def change_user_password(self, username: str, password: str) -> Optional[dict]:
27052705
)
27062706
return json.loads(_finish_response(result))
27072707

2708-
def get_database(self, dbid: str) -> Optional[dict]:
2708+
def get_database(self, dbid: str, team: Optional[str] = None) -> Optional[dict]:
27092709
"""
27102710
Returns metadata (id, organization, label, comment) about the requested database
27112711
Parameters
27122712
----------
27132713
dbid : str
27142714
The id of the database
2715+
team : str
2716+
The organization of the database (default self.team)
27152717
27162718
Raises
27172719
------
@@ -2723,10 +2725,13 @@ def get_database(self, dbid: str) -> Optional[dict]:
27232725
dict or None if not found
27242726
"""
27252727
self._check_connection(check_db=False)
2726-
for this_db in self.get_databases():
2727-
if this_db["name"] == dbid:
2728-
return this_db
2729-
return None
2728+
team = team if team else self.team
2729+
result = requests.get(
2730+
f"{self.api}/db/{team}/{dbid}?verbose=true",
2731+
headers=self._default_headers,
2732+
auth=self._auth(),
2733+
)
2734+
return json.loads(_finish_response(result))
27302735

27312736
def get_databases(self) -> List[dict]:
27322737
"""

terminusdb_client/tests/integration_tests/test_client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,17 @@ def test_add_get_remove_org(docker_url):
110110
client.get_organization("testOrg")
111111

112112

113+
def test_get_database(docker_url):
114+
client = Client(docker_url, user_agent=test_user_agent, team="admin")
115+
client.connect()
116+
db_name = "testDB" + str(random())
117+
client.create_database(db_name, team="admin")
118+
db = client.get_database(db_name)
119+
assert db['name'] == db_name
120+
db_with_team = client.get_database(db_name, team='admin')
121+
assert db_with_team['name'] == db_name
122+
123+
113124
def test_add_get_remove_user(docker_url):
114125
# create client
115126
client = Client(docker_url, user_agent=test_user_agent)

terminusdb_client/tests/test_Client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ def test_crazy_branch(mocked_requests, mocked_requests2, mocked_requests3):
166166
)
167167

168168

169+
@mock.patch("requests.get", side_effect=mocked_request_success)
170+
@mock.patch("requests.post", side_effect=mocked_request_success)
171+
def test_get_database(mocked_requests, mocked_requests2):
172+
client = Client("http://localhost:6363")
173+
client.connect(user="admin", team="admin", key="root")
174+
db_name = "testDB" + str(random.randrange(100000))
175+
client.create_database(db_name)
176+
client.get_database(db_name)
177+
requests.get.assert_called_with(
178+
f"http://localhost:6363/api/db/admin/{db_name}?verbose=true",
179+
auth=("admin", "root"),
180+
headers={"user-agent": f"terminusdb-client-python/{__version__}"},
181+
)
182+
183+
169184
@pytest.mark.skip(reason="temporary not avaliable")
170185
@mock.patch("requests.head", side_effect=mocked_request_success)
171186
@mock.patch("requests.get", side_effect=mocked_request_success)

0 commit comments

Comments
 (0)