diff --git a/app/routes/thematic/geoencoder.py b/app/routes/thematic/geoencoder.py index b5c28192..2c89c5f0 100644 --- a/app/routes/thematic/geoencoder.py +++ b/app/routes/thematic/geoencoder.py @@ -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 @@ -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 @@ -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, diff --git a/tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py b/tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py index 2943d287..35b8ca81 100644 --- a/tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py +++ b/tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py @@ -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" @@ -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" @@ -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" @@ -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" @@ -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"}, } ], },