From 4489f67ceb5202ddc0febd3d928d436c2780bde3 Mon Sep 17 00:00:00 2001 From: Marc Lichtman Date: Thu, 24 Oct 2024 12:42:39 -0400 Subject: [PATCH] also allow collectionid to be in the filter --- pcstac/pcstac/client.py | 7 ++- pcstac/tests/resources/test_item.py | 67 ++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/pcstac/pcstac/client.py b/pcstac/pcstac/client.py index db308a1e..5c6d4661 100644 --- a/pcstac/pcstac/client.py +++ b/pcstac/pcstac/client.py @@ -1,3 +1,4 @@ +import json import logging import time from typing import Any, List, Optional, Type @@ -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() diff --git a/pcstac/tests/resources/test_item.py b/pcstac/tests/resources/test_item.py index 14529d10..c7207e63 100644 --- a/pcstac/tests/resources/test_item.py +++ b/pcstac/tests/resources/test_item.py @@ -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""" @@ -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"]}