Skip to content

Commit 9bd4aaf

Browse files
committed
feat(Geostore): add ability to retrieve geostore ID only
1 parent 8413b53 commit 9bd4aaf

File tree

2 files changed

+131
-5
lines changed

2 files changed

+131
-5
lines changed

app/crud/geostore.py

+21
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,27 @@ async def get_first_row(sql: Select):
191191
return row
192192

193193

194+
async def get_gadm_geostore_id(
195+
admin_provider: str,
196+
admin_version: str,
197+
adm_level: int,
198+
country_id: str,
199+
region_id: str | None = None,
200+
subregion_id: str | None = None,
201+
) -> str:
202+
src_table = await get_versioned_dataset(admin_provider, admin_version)
203+
columns_etc: List[Column | Label] = [db.column("gfw_geostore_id"),]
204+
sql: Select = db.select(columns_etc).select_from(src_table)
205+
sql = await add_where_clauses(adm_level, admin_provider, admin_version, country_id, region_id, sql, subregion_id)
206+
row = await get_first_row(sql)
207+
if row is None:
208+
raise RecordNotFoundError(
209+
f"Admin boundary not found in {admin_provider} version {admin_version}"
210+
)
211+
212+
return await row.gfw_geostore_id
213+
214+
194215
async def build_gadm_geostore(
195216
admin_provider: str,
196217
admin_version: str,

tests_v2/unit/app/crud/test_geostore.py

+110-5
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import pytest
44
from httpx import AsyncClient
55

6-
from app.crud.geostore import get_gadm_geostore
6+
from app.crud.geostore import get_gadm_geostore, get_gadm_geostore_id
77
from app.errors import RecordNotFoundError
88

99

1010
@pytest.mark.asyncio
1111
async def test_get_gadm_geostore_generates_correct_sql_for_country_lookup(
12-
async_client: AsyncClient
12+
async_client: AsyncClient
1313
):
1414
provider = "gadm"
1515
version = "4.1"
@@ -45,7 +45,7 @@ async def test_get_gadm_geostore_generates_correct_sql_for_country_lookup(
4545

4646
@pytest.mark.asyncio
4747
async def test_get_gadm_geostore_generates_correct_sql_for_region_lookup(
48-
async_client: AsyncClient
48+
async_client: AsyncClient
4949
):
5050
provider = "gadm"
5151
version = "4.1"
@@ -80,10 +80,9 @@ async def test_get_gadm_geostore_generates_correct_sql_for_region_lookup(
8080
assert actual_sql == expected_sql
8181

8282

83-
8483
@pytest.mark.asyncio
8584
async def test_get_gadm_geostore_generates_correct_sql_for_subregion_lookup(
86-
async_client: AsyncClient
85+
async_client: AsyncClient
8786
):
8887
provider = "gadm"
8988
version = "4.1"
@@ -117,3 +116,109 @@ async def test_get_gadm_geostore_generates_correct_sql_for_subregion_lookup(
117116

118117
assert mock_get_first_row.called is True
119118
assert actual_sql == expected_sql
119+
120+
121+
class TestGadmGeostoreIDLookup:
122+
@pytest.mark.asyncio
123+
async def test_get_gadm_geostore_id_generates_correct_sql_for_country_lookup(
124+
async_client: AsyncClient
125+
):
126+
provider = "gadm"
127+
version = "4.1"
128+
adm_level = 0
129+
country_id = "MEX"
130+
131+
with patch("app.crud.geostore.get_first_row") as mock_get_first_row:
132+
mock_get_first_row.return_value = None
133+
try:
134+
_ = await get_gadm_geostore_id(
135+
provider, version, adm_level, country_id, None, None
136+
)
137+
except RecordNotFoundError:
138+
pass
139+
140+
expected_sql = (
141+
"SELECT gfw_geostore_id "
142+
'\nFROM gadm_administrative_boundaries."v4.1.64" \n'
143+
"WHERE adm_level='0' AND gid_0='MEX'"
144+
)
145+
146+
actual_sql = str(
147+
mock_get_first_row.call_args.args[0].compile(
148+
compile_kwargs={"literal_binds": True}
149+
)
150+
)
151+
152+
assert mock_get_first_row.called is True
153+
assert actual_sql == expected_sql
154+
155+
156+
@pytest.mark.asyncio
157+
async def test_get_gadm_geostore_id_generates_correct_sql_for_region_lookup(
158+
async_client: AsyncClient
159+
):
160+
provider = "gadm"
161+
version = "4.1"
162+
adm_level = 1
163+
country = "MEX"
164+
region = "5"
165+
166+
with patch("app.crud.geostore.get_first_row") as mock_get_first_row:
167+
mock_get_first_row.return_value = None
168+
try:
169+
_ = await get_gadm_geostore_id(
170+
provider, version, adm_level, country, region, None
171+
)
172+
except RecordNotFoundError:
173+
pass
174+
175+
expected_sql = (
176+
"SELECT gfw_geostore_id "
177+
'\nFROM gadm_administrative_boundaries."v4.1.64" \n'
178+
r"WHERE adm_level='1' AND gid_1 LIKE 'MEX.5\__'"
179+
)
180+
181+
actual_sql = str(
182+
mock_get_first_row.call_args.args[0].compile(
183+
compile_kwargs={"literal_binds": True}
184+
)
185+
)
186+
187+
assert mock_get_first_row.called is True
188+
assert actual_sql == expected_sql
189+
190+
191+
@pytest.mark.asyncio
192+
async def test_get_gadm_geostore_id_generates_correct_sql_for_subregion_lookup(
193+
async_client: AsyncClient
194+
):
195+
provider = "gadm"
196+
version = "4.1"
197+
adm_level = 2
198+
country = "MEX"
199+
region = "5"
200+
subregion = "2"
201+
202+
with patch("app.crud.geostore.get_first_row") as mock_get_first_row:
203+
mock_get_first_row.return_value = None
204+
try:
205+
_ = await get_gadm_geostore_id(
206+
provider, version, adm_level, country, region, subregion
207+
)
208+
except RecordNotFoundError:
209+
pass
210+
211+
expected_sql = (
212+
"SELECT gfw_geostore_id "
213+
'\nFROM gadm_administrative_boundaries."v4.1.64" \n'
214+
r"WHERE adm_level='2' AND gid_2 LIKE 'MEX.5.2\__'"
215+
)
216+
217+
actual_sql = str(
218+
mock_get_first_row.call_args.args[0].compile(
219+
compile_kwargs={"literal_binds": True}
220+
)
221+
)
222+
223+
assert mock_get_first_row.called is True
224+
assert actual_sql == expected_sql

0 commit comments

Comments
 (0)