Skip to content

Commit

Permalink
Return GIDs as relative, not complete values. eg: GID_1=1 instead of …
Browse files Browse the repository at this point in the history
…GID_1=TWN.1
  • Loading branch information
dmannarino committed Dec 28, 2024
1 parent ed5f2cd commit 34c41f8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
24 changes: 12 additions & 12 deletions app/routes/thematic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def geoencode(
normalize_search, country, region, subregion
)

adm_level: str = determine_admin_level(*names)
adm_level: int = determine_admin_level(*names)

sql: str = _admin_boundary_lookup_sql(
adm_level, normalize_search, admin_source, *names
Expand All @@ -89,19 +89,19 @@ async def geoencode(
},
"region": {
"id": (
match["gid_1"].rsplit("_")[0]
if int(adm_level) >= 1
(match["gid_1"].rsplit("_")[0]).split(".")[1]
if adm_level >= 1
else None
),
"name": match["name_1"] if int(adm_level) >= 1 else None,
"name": match["name_1"] if adm_level >= 1 else None,
},
"subregion": {
"id": (
match["gid_2"].rsplit("_")[0]
if int(adm_level) >= 2
(match["gid_2"].rsplit("_")[0]).split(".")[2]
if adm_level >= 2
else None
),
"name": match["name_2"] if int(adm_level) >= 2 else None,
"name": match["name_2"] if adm_level >= 2 else None,
},
}
for match in json_data
Expand Down Expand Up @@ -139,22 +139,22 @@ def sanitize_names(

def determine_admin_level(
country: str | None, region: str | None, subregion: str | None
) -> str:
) -> int:
"""Infer the native admin level of a request based on the presence of
non-empty fields
"""
if subregion:
return "2"
return 2
elif region:
return "1"
return 1
elif country:
return "0"
return 0
else: # Shouldn't get here if FastAPI route definition worked
raise HTTPException(status_code=400, detail="Country MUST be specified.")


def _admin_boundary_lookup_sql(
adm_level: str,
adm_level: int,
normalize_search: bool,
dataset: str,
country_name: str,
Expand Down
12 changes: 6 additions & 6 deletions tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async def test_sanitize_names_tolerate_enforce_hierarchy() -> None:
@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_country() -> None:
sql = _admin_boundary_lookup_sql(
"0", False, "some_dataset", "some_country", None, None
0, False, "some_dataset", "some_country", None, None
)
assert sql == (
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
Expand All @@ -74,7 +74,7 @@ async def test__admin_boundary_lookup_sql_country() -> None:
@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_country_region() -> None:
sql = _admin_boundary_lookup_sql(
"1", False, "some_dataset", "some_country", "some_region", None
1, False, "some_dataset", "some_country", "some_region", None
)
assert sql == (
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
Expand All @@ -87,7 +87,7 @@ async def test__admin_boundary_lookup_sql_country_region() -> None:
@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_all() -> None:
sql = _admin_boundary_lookup_sql(
"2", False, "some_dataset", "some_country", "some_region", "some_subregion"
2, False, "some_dataset", "some_country", "some_region", "some_subregion"
)
assert sql == (
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
Expand All @@ -101,7 +101,7 @@ async def test__admin_boundary_lookup_sql_all() -> None:
@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_all_normalized() -> None:
sql = _admin_boundary_lookup_sql(
"2", True, "some_dataset", "some_country", "some_region", "some_subregion"
2, True, "some_dataset", "some_country", "some_region", "some_subregion"
)
assert sql == (
"SELECT gid_0, gid_1, gid_2, country, name_1, name_2 FROM some_dataset"
Expand Down Expand Up @@ -238,8 +238,8 @@ async def mock_version_is_valid(dataset: str, version: str):
"matches": [
{
"country": {"id": "TWN", "name": "Taiwan"},
"region": {"id": "TWN.1", "name": "Fujian"},
"subregion": {"id": "TWN.1.1", "name": "Kinmen"},
"region": {"id": "1", "name": "Fujian"},
"subregion": {"id": "1", "name": "Kinmen"},
}
],
},
Expand Down

0 comments on commit 34c41f8

Please sign in to comment.