Skip to content

Commit dc726e4

Browse files
committed
Added tests for getting geostores by ID
1 parent e92968b commit dc726e4

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

app/routes/geostore/geostore.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,16 @@ async def rw_get_view_geostore_by_id(
9393
)
9494
async def get_any_geostore(
9595
*,
96-
geostore_id: UUID | str = Path(..., title="geostore_id"),
96+
geostore_id: str | UUID = Path(..., title="geostore_id"),
9797
x_api_key: Annotated[str | None, Header()] = None,
9898
):
9999
"""Retrieve GeoJSON representation for a given geostore ID of any
100100
dataset. If the provided ID is in UUID style, get from the GFW Data API.
101101
Otherwise, forward request to RW API.
102102
"""
103-
if isinstance(geostore_id, UUID):
103+
# Checking for hyphens to see if the provided ID is a GUID or a UUID
104+
# is a bit iffy. Happy to hear better ideas.
105+
if "-" in geostore_id:
104106
try:
105107
result = await geostore.get_gfw_geostore_from_any_dataset(geostore_id)
106108
return GeostoreResponse(data=result)

tests_v2/unit/app/routes/geostore/test_geostore.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -277,27 +277,58 @@ async def test_add_geostore_gfw_branch(
277277
assert mock_create_gfw_geostore.called is True
278278

279279

280-
# @pytest.mark.asyncio
281-
# async def test_get_geostore_rw_branch(
282-
# async_client: AsyncClient, monkeypatch: MonkeyPatch
283-
# ):
284-
# url = "/geostore/88db597b6bcd096fb80d1542cdc442be"
285-
#
286-
# mock_proxy_get_geostore = AsyncMock(
287-
# return_value=Response(status_code=200),
288-
# spec=geostore.proxy_get_geostore
289-
# )
290-
#
291-
# monkeypatch.setattr(geostore, "proxy_get_geostore", mock_proxy_get_geostore)
292-
#
293-
# response = await async_client.post(
294-
# url,
295-
# json=RWGeostoreIn(**create_rw_geostore_payload).dict(),
296-
# follow_redirects=True
297-
# )
298-
#
299-
# assert response.json() == create_rw_geostore_output
300-
# assert response.status_code == 200
301-
#
302-
#
303-
#
280+
@pytest.mark.asyncio
281+
async def test_get_geostore_rw_branch(
282+
async_client: AsyncClient, monkeypatch: MonkeyPatch
283+
):
284+
url = "/geostore/88db597b6bcd096fb80d1542cdc442be"
285+
286+
mock_proxy_get_geostore = AsyncMock(
287+
return_value=RWGeostoreResponse(**example_geostore_resp),
288+
spec=geostore.proxy_get_geostore,
289+
)
290+
monkeypatch.setattr(geostore, "proxy_get_geostore", mock_proxy_get_geostore)
291+
292+
mock_geostore_obj = MagicMock(spec=Geostore)
293+
mock_get_gfw_geostore_from_any_dataset = AsyncMock(
294+
return_value=mock_geostore_obj,
295+
spec=crud_geostore.get_gfw_geostore_from_any_dataset,
296+
)
297+
monkeypatch.setattr(
298+
crud_geostore,
299+
"get_gfw_geostore_from_any_dataset",
300+
mock_get_gfw_geostore_from_any_dataset,
301+
)
302+
303+
_ = await async_client.get(url)
304+
305+
assert mock_proxy_get_geostore.called is True
306+
assert mock_get_gfw_geostore_from_any_dataset.called is False
307+
308+
309+
@pytest.mark.asyncio
310+
async def test_get_geostore_gfw_branch(
311+
async_client: AsyncClient, monkeypatch: MonkeyPatch
312+
):
313+
url = "/geostore/db2b4428-bad2-fc94-1ea8-041597dc482c"
314+
315+
mock_proxy_get_geostore = AsyncMock(
316+
return_value=RWGeostoreResponse(**example_geostore_resp),
317+
spec=geostore.proxy_get_geostore,
318+
)
319+
monkeypatch.setattr(geostore, "proxy_get_geostore", mock_proxy_get_geostore)
320+
321+
mock_get_gfw_geostore_from_any_dataset = AsyncMock(
322+
return_value=Geostore(**create_gfw_geostore_data),
323+
spec=crud_geostore.get_gfw_geostore_from_any_dataset,
324+
)
325+
monkeypatch.setattr(
326+
crud_geostore,
327+
"get_gfw_geostore_from_any_dataset",
328+
mock_get_gfw_geostore_from_any_dataset,
329+
)
330+
331+
_ = await async_client.get(url)
332+
333+
assert mock_proxy_get_geostore.called is False
334+
assert mock_get_gfw_geostore_from_any_dataset.called is True

0 commit comments

Comments
 (0)