Skip to content

Commit

Permalink
fixed searchid bug and added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Lichtman committed Aug 29, 2024
1 parent 822f8f5 commit 05c4ff0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 11 deletions.
4 changes: 3 additions & 1 deletion pctiler/pctiler/endpoints/pg_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from titiler.core.resources.enums import ImageType
from titiler.pgstac.dependencies import SearchIdParams, TmsTileParams
from titiler.pgstac.factory import MosaicTilerFactory
from titiler.pgstac.extensions import searchInfoExtension

from pccommon.config import get_collection_config
from pccommon.config.collections import MosaicInfo
Expand Down Expand Up @@ -45,9 +46,10 @@ def __init__(self, request: Request):
colormap_dependency=PCColorMapParams,
layer_dependency=AssetsBidxExprParams,
reader_dependency=ReaderParams,
router_prefix=get_settings().mosaic_endpoint_prefix + "/{search_id}",
router_prefix=get_settings().mosaic_endpoint_prefix + "/{search_id}", # reverts /searches back to /mosaic
backend_dependency=BackendParams,
add_statistics=False,
extensions=[searchInfoExtension()]
)


Expand Down
75 changes: 65 additions & 10 deletions pctiler/tests/endpoints/test_pg_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,45 @@

@pytest.fixture
async def register_search(client: AsyncClient) -> REGISTER_TYPE:

cql = {
"filter-lang": "cql2-json",
"filter": {
"op": "and",
"args": [{"op": "=", "args": [{"property": "collection"}, "naip"]}],
},
}
expected_content_hash = "8b989f86a149628eabfde894fb965982"
response = await client.post("/mosaic/register", json=cql)
expected_content_hash = response.json()["searchid"]
resp = response.json()

return (expected_content_hash, resp)


async def test_register_search_with_geom(client: AsyncClient) -> None:
geom = {
"type": "Polygon",
"coordinates": [
[
[-79.09062791441062, 43.08554661560049],
[-79.0629876337021, 43.08554661560049],
[-79.0629876337021, 43.067969831431895],
[-79.09062791441062, 43.067969831431895],
[-79.09062791441062, 43.08554661560049],
]
],
}
cql = {
"filter-lang": "cql2-json",
"filter": {
"op": "and",
"args": [{"op": "=", "args": [{"property": "collection"}, "naip"]},
{"op": "s_intersects", "args": [{"property": "geometry"}, geom]}],
},
}
response = await client.post("/mosaic/register", json=cql)
assert response.status_code == 200
assert response.json()["searchid"] == '2607eab51afd5d626da8d50d9df3bbf0'


@pytest.mark.asyncio
async def test_mosaic_info(client: AsyncClient) -> None:
response = await client.get("/mosaic/info?collection=naip")
Expand All @@ -43,13 +67,10 @@ async def test_register(client: AsyncClient, register_search: REGISTER_TYPE) ->
# and that the search hash remains consistent
assert register_resp["searchid"] == expected_content_hash
# Test that the links have had the {tileMatrixSetId} template string removed
assert len(register_resp["links"]) == 2
assert register_resp["links"][0]["href"].endswith(
f"/mosaic/{expected_content_hash}/tilejson.json"
)
assert register_resp["links"][1]["href"].endswith(
f"/mosaic/{expected_content_hash}/WMTSCapabilities.xml"
)
assert len(register_resp["links"]) == 3
assert register_resp["links"][0]["href"].endswith(f"/mosaic/{expected_content_hash}/info") # gets added by searchInfoExtension
assert register_resp["links"][1]["href"].endswith(f"/mosaic/{expected_content_hash}/tilejson.json")
assert register_resp["links"][2]["href"].endswith(f"/mosaic/{expected_content_hash}/WMTSCapabilities.xml")


@pytest.mark.asyncio
Expand Down Expand Up @@ -101,6 +122,40 @@ async def test_mosaic_tile_routes(
assert response.status_code == 200


async def test_info_path_with_searchid(client: AsyncClient, register_search: REGISTER_TYPE) -> None:
# route source code is here https://github.com/developmentseed/titiler/blob/main/src/titiler/mosaic/titiler/mosaic/factory.py#L157
# the searchId functionality is added by titiler-pgstac
route = "mosaic/{searchId}/info"
expected_content_hash, _ = register_search
formatted_route = route.format(searchId=expected_content_hash)
url = (f"/{formatted_route}?asset_bidx=image%7C1%2C2%2C3&assets=image&collection=naip")
response = await client.get(url)
assert response.status_code == 200


async def test_info_path_with_bad_searchid(client: AsyncClient) -> None:
route = "mosaic/{searchId}/info"
expected_content_hash = "9b989f86a149628eabfde894fb965982" # does not match the one we registered in the fixture
formatted_route = route.format(searchId=expected_content_hash)
url = (f"/{formatted_route}?asset_bidx=image%7C1%2C2%2C3&assets=image&collection=naip")
response = await client.get(url)
assert response.status_code == 404


async def test_bad_searchid(client: AsyncClient) -> None:
route = "mosaic/tiles/{searchId}/{z}/{x}/{y}"
expected_content_hash = "9b989f86a149628eabfde894fb965982" # does not match the one we registered in the fixture
formatted_route = route.format(
searchId=expected_content_hash,
z=16,
x=17218,
y=26838
)
url = (f"/{formatted_route}?asset_bidx=image%7C1%2C2%2C3&assets=image&collection=naip")
response = await client.get(url)
assert response.status_code == 404


@pytest.mark.asyncio
@pytest.mark.parametrize(
"route",
Expand Down
7 changes: 7 additions & 0 deletions scripts/test-mosaic
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

docker compose \
-f docker-compose.yml \
-f docker-compose.dev.yml \
run --rm \
tiler-dev python3 -m pytest pctiler/tests/endpoints/test_pg_mosaic.py

0 comments on commit 05c4ff0

Please sign in to comment.