Skip to content

Commit

Permalink
also allow collectionid to be in the filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Lichtman committed Oct 24, 2024
1 parent a255829 commit 4489f67
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
7 changes: 6 additions & 1 deletion pcstac/pcstac/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import time
from typing import Any, List, Optional, Type
Expand Down Expand Up @@ -216,7 +217,11 @@ async def _fetch() -> ItemCollection:
return item_collection

# Block searches that don't specify a collection
if search_request.collections is None and "collection=" not in str(request.url):
if (
search_request.collections is None
and "collection=" not in str(request.url)
and '{"property": "collection"}' not in json.dumps(search_request.filter)
):
raise HTTPException(status_code=422, detail="collection is required")

search_json = search_request.model_dump_json()
Expand Down
67 changes: 66 additions & 1 deletion pcstac/tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,69 @@ async def test_item_search_get_filter_extension_cql(app_client):
)


@pytest.mark.asyncio
async def test_search_using_filter_with_collectionid(app_client):
"""Test POST search with JSONB query (cql json filter extension)
that includes a collectionid in the filter and no where else"""
items_resp = await app_client.get("/collections/naip/items")
assert items_resp.status_code == 200

first_item = items_resp.json()["features"][0]

# EPSG is a JSONB key
body = {
"filter": {
"op": "and",
"args": [
{"op": "=", "args": [{"property": "collection"}, "naip"]},
{
"op": "=",
"args": [
{"property": "proj:epsg"},
first_item["properties"]["proj:epsg"],
],
},
],
}
}
resp = await app_client.post("/search", json=body)
resp_json = resp.json()

assert resp.status_code == 200
assert len(resp_json["features"]) == 12
assert (
resp_json["features"][0]["properties"]["proj:epsg"]
== first_item["properties"]["proj:epsg"]
)


@pytest.mark.asyncio
async def test_search_using_filter_without_collectionid(app_client):
"""Test POST search with JSONB query (cql json filter extension)
that includes a collectionid in the filter and no where else"""
items_resp = await app_client.get("/collections/naip/items")
assert items_resp.status_code == 200

first_item = items_resp.json()["features"][0]

# EPSG is a JSONB key
body = {
"filter": {
"args": [
{
"op": "=",
"args": [
{"property": "proj:epsg"},
first_item["properties"]["proj:epsg"],
],
},
],
}
}
resp = await app_client.post("/search", json=body)
assert resp.status_code == 422


@pytest.mark.asyncio
async def test_get_missing_item_collection(app_client):
"""Test reading a collection which does not exist"""
Expand Down Expand Up @@ -638,7 +701,9 @@ async def test_search_get_page_limits(app_client):
assert len(resp_json["features"]) == 12


@pytest.mark.skip(reason="Are these params even valid? they dont match the model")
@pytest.mark.skip(
reason="Are these params even valid? they are not within filter field"
)
@pytest.mark.asyncio
async def test_search_post_page_limits(app_client):
params = {"op": "=", "args": [{"property": "collection"}, "naip"]}
Expand Down

0 comments on commit 4489f67

Please sign in to comment.