Skip to content

Commit 2937bc8

Browse files
Mats-SXFlorentinD
andcommitted
Test, refactor and comment closest_match
Co-authored-by: Florentin Dörre <florentin.dorre@neotechnology.com>
1 parent a958774 commit 2937bc8

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

graphdatascience/gds_session/gds_sessions.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,13 @@ def _ds_region(self, region: str, cloud_provider: str) -> str:
135135
tenant_details = self._aura_api.tenant_details()
136136
available_regions = tenant_details.regions_per_provider[cloud_provider]
137137

138-
if not available_regions:
138+
match = closest_match(region, available_regions)
139+
if not match:
139140
raise ValueError(
140141
f"Tenant `{tenant_details.id}` cannot create GDS sessions at cloud provider `{cloud_provider}`."
141142
)
142143

143-
return closest_match(region, available_regions)
144+
return match
144145

145146
def _construct_client(
146147
self, session_name: str, gds_url: str, db_connection: DbmsConnectionInfo
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
from typing import Iterable
1+
from typing import Iterable, Optional
22

33
import textdistance
44

55

6-
def closest_match(query: str, options: Iterable[str]) -> str:
7-
if not options:
8-
raise ValueError("No options to choose from.")
9-
6+
# AuraDB and AuraDS regions are not the same, so we need to find the closest match.
7+
def closest_match(db_region: str, ds_regions: Iterable[str]) -> Optional[str]:
108
curr_max_similarity = 0.0
11-
for ep in options:
12-
similarity = textdistance.jaro_winkler(query, ep)
9+
closest_option = None
10+
11+
for region in ds_regions:
12+
similarity = textdistance.jaro_winkler(db_region, region)
1313
if similarity > curr_max_similarity:
14-
closest_option = ep
14+
closest_option = region
1515
curr_max_similarity = similarity
1616

1717
return closest_option
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from graphdatascience.gds_session import region_suggester
2+
3+
4+
def test_picks_the_only_option() -> None:
5+
match = region_suggester.closest_match("db_region", ["ds_region1"])
6+
assert match == "ds_region1"
7+
8+
9+
def test_picks_the_closest() -> None:
10+
match = region_suggester.closest_match("eu-1", ["us-1", "aa-1", "ea-1", "eu-4"])
11+
assert match == "eu-4"
12+
13+
14+
def test_picks_exact_match() -> None:
15+
match = region_suggester.closest_match("eu-2", ["eu-1", "eu-2", "eu-3"])
16+
assert match == "eu-2"
17+
18+
19+
def test_no_options() -> None:
20+
match = region_suggester.closest_match("eu-2", [])
21+
assert match is None

0 commit comments

Comments
 (0)