Skip to content

Commit

Permalink
Hide extraneous fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Dec 27, 2024
1 parent e09cf01 commit 2d979a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 13 deletions.
28 changes: 20 additions & 8 deletions app/routes/thematic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def geoencode(
admin_source_to_dataset: Dict[str, str] = {"GADM": "gadm_administrative_boundaries"}

try:
dataset = admin_source_to_dataset[admin_source.upper()]
dataset: str = admin_source_to_dataset[admin_source.upper()]
except KeyError:
raise HTTPException(
status_code=400,
Expand All @@ -59,15 +59,19 @@ async def geoencode(
),
)

version_str = "v" + str(admin_version).lstrip("v")
version_str: str = "v" + str(admin_version).lstrip("v")

await version_is_valid(dataset, version_str)

names: List[str | None] = sanitize_names(
search_unaccented, country, region, subregion
)

sql: str = _admin_boundary_lookup_sql(search_unaccented, admin_source, *names)
adm_level: str = determine_admin_level(*names)

sql: str = _admin_boundary_lookup_sql(
adm_level, search_unaccented, admin_source, *names
)

json_data: List[Dict[str, Any]] = await _query_dataset_json(
dataset, version_str, sql, None
Expand All @@ -84,12 +88,20 @@ async def geoencode(
"name": match["country"],
},
"region": {
"id": match["gid_1"].rsplit("_")[0],
"name": match["name_1"],
"id": (
match["gid_1"].rsplit("_")[0]
if int(adm_level) >= 1
else None
),
"name": match["name_1"] if int(adm_level) >= 1 else None,
},
"subregion": {
"id": match["gid_2"].rsplit("_")[0],
"name": match["name_2"],
"id": (
match["gid_2"].rsplit("_")[0]
if int(adm_level) >= 2
else None
),
"name": match["name_2"] if int(adm_level) >= 2 else None,
},
}
for match in json_data
Expand Down Expand Up @@ -136,6 +148,7 @@ def determine_admin_level(


def _admin_boundary_lookup_sql(
adm_level: str,
search_unaccented: bool,
dataset: str,
country_name: str,
Expand All @@ -160,7 +173,6 @@ def _admin_boundary_lookup_sql(
if subregion_name is not None:
sql += f" AND {match_name_fields[2]}='{subregion_name}'"

adm_level = determine_admin_level(country_name, region_name, subregion_name)
sql += f" AND adm_level='{adm_level}'"

return sql
Expand Down
54 changes: 49 additions & 5 deletions tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_country() -> None:
sql = _admin_boundary_lookup_sql(False, "some_dataset", "some_country", None, None)
sql = _admin_boundary_lookup_sql(
"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"
" WHERE country='some_country' AND adm_level='0'"
Expand All @@ -20,7 +22,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(
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 @@ -33,7 +35,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(
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 @@ -47,7 +49,7 @@ async def test__admin_boundary_lookup_sql_all() -> None:
@pytest.mark.asyncio
async def test__admin_boundary_lookup_sql_all_unaccented() -> None:
sql = _admin_boundary_lookup_sql(
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 @@ -136,7 +138,7 @@ async def mock_version_is_valid(dataset: str, version: str):


@pytest.mark.asyncio
async def test_geoencoder_matches(
async def test_geoencoder_matches_full(
async_client: AsyncClient, monkeypatch: pytest.MonkeyPatch
) -> None:
admin_source = "gadm"
Expand All @@ -146,6 +148,8 @@ async def test_geoencoder_matches(
"admin_source": admin_source,
"admin_version": admin_version,
"country": "Taiwan",
"region": "Fujian",
"subregion": "Kinmen",
}

async def mock_version_is_valid(dataset: str, version: str):
Expand Down Expand Up @@ -175,6 +179,46 @@ async def mock_version_is_valid(dataset: str, version: str):
assert resp.status_code == 200


@pytest.mark.asyncio
async def test_geoencoder_matches_hide_extraneous(
async_client: AsyncClient, monkeypatch: pytest.MonkeyPatch
) -> None:
admin_source = "gadm"
admin_version = "v4.1"

params = {
"admin_source": admin_source,
"admin_version": admin_version,
"country": "Taiwan",
}

async def mock_version_is_valid(dataset: str, version: str):
return None

monkeypatch.setattr(geoencoder, "version_is_valid", mock_version_is_valid)
monkeypatch.setattr(
geoencoder, "_query_dataset_json", _query_dataset_json_mocked_results
)

resp = await async_client.get("/thematic/geoencode", params=params)

assert resp.json() == {
"status": "success",
"data": {
"adminSource": admin_source,
"adminVersion": admin_version,
"matches": [
{
"country": {"id": "TWN", "name": "Taiwan"},
"region": {"id": None, "name": None},
"subregion": {"id": None, "name": None},
}
],
},
}
assert resp.status_code == 200


async def _query_dataset_json_mocked_no_results(
dataset: str,
version: str,
Expand Down

0 comments on commit 2d979a2

Please sign in to comment.