Skip to content

Commit

Permalink
Add output example from ticket as a test, and adjust code to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
dmannarino committed Dec 27, 2024
1 parent 1b95ca2 commit 79ae7c3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
18 changes: 17 additions & 1 deletion app/routes/thematic/geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,23 @@ async def geoencode(
data={
"adminSource": admin_source,
"adminVersion": admin_version,
"matches": json_data,
"matches": [
{
"country": {
"id": match["gid_0"].rsplit("_")[0],
"name": match["country"],
},
"region": {
"id": match["gid_1"].rsplit("_")[0],
"name": match["name_1"],
},
"subregion": {
"id": match["gid_2"].rsplit("_")[0],
"name": match["name_2"],
},
}
for match in json_data
],
}
)

Expand Down
62 changes: 60 additions & 2 deletions tests_v2/unit/app/routes/thematic/geoencoder/test_geoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def mock_version_is_valid(dataset: str, version: str):

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

resp = await async_client.get("/thematic/geoencode", params=params)
Expand All @@ -121,10 +121,68 @@ async def mock_version_is_valid(dataset: str, version: str):
assert resp.status_code == 200


async def _query_dataset_json_mocked_results(
@pytest.mark.asyncio
async def test_geoencoder_matches(
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": "TWN.1", "name": "Fujian"},
"subregion": {"id": "TWN.1.1", "name": "Kinmen"},
}
],
},
}
assert resp.status_code == 200


async def _query_dataset_json_mocked_no_results(
dataset: str,
version: str,
sql: str,
geostore: Optional[GeostoreCommon],
) -> List[Dict[str, Any]]:
return []


async def _query_dataset_json_mocked_results(
dataset: str,
version: str,
sql: str,
geostore: Optional[GeostoreCommon],
) -> List[Dict[str, Any]]:
return [
{
"gid_0": "TWN",
"gid_1": "TWN.1_1",
"gid_2": "TWN.1.1_1",
"country": "Taiwan",
"name_1": "Fujian",
"name_2": "Kinmen",
}
]

0 comments on commit 79ae7c3

Please sign in to comment.