From 6eba939c4ae1b03082f520c583352b3b00743108 Mon Sep 17 00:00:00 2001 From: Tom Christian Date: Thu, 20 Jun 2024 10:53:27 -0700 Subject: [PATCH] fix(#711): changed default filter language --- CHANGES.md | 4 + .../extensions/core/filter/request.py | 2 +- stac_fastapi/extensions/tests/test_filter.py | 75 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 stac_fastapi/extensions/tests/test_filter.py diff --git a/CHANGES.md b/CHANGES.md index 49920eb9..3d63fea6 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,10 @@ ## [Unreleased] - TBD +### Fixed + +* Updated default filter language in filter extension's POST search request model to match the extension's documentation [#711](https://github.com/stac-utils/stac-fastapi/issues/711) + ## [3.0.0a3] - 2024-06-13 ### Added diff --git a/stac_fastapi/extensions/stac_fastapi/extensions/core/filter/request.py b/stac_fastapi/extensions/stac_fastapi/extensions/core/filter/request.py index dde01530..35a17bf3 100644 --- a/stac_fastapi/extensions/stac_fastapi/extensions/core/filter/request.py +++ b/stac_fastapi/extensions/stac_fastapi/extensions/core/filter/request.py @@ -24,4 +24,4 @@ class FilterExtensionPostRequest(BaseModel): filter: Optional[Dict[str, Any]] = None filter_crs: Optional[str] = Field(alias="filter-crs", default=None) - filter_lang: Optional[FilterLang] = Field(alias="filter-lang", default="cql-json") + filter_lang: Optional[FilterLang] = Field(alias="filter-lang", default="cql2-json") diff --git a/stac_fastapi/extensions/tests/test_filter.py b/stac_fastapi/extensions/tests/test_filter.py new file mode 100644 index 00000000..ca72dc51 --- /dev/null +++ b/stac_fastapi/extensions/tests/test_filter.py @@ -0,0 +1,75 @@ +from typing import Iterator + +import pytest +from starlette.testclient import TestClient + +from stac_fastapi.api.app import StacApi +from stac_fastapi.api.models import create_get_request_model, create_post_request_model +from stac_fastapi.extensions.core import FilterExtension +from stac_fastapi.types.config import ApiSettings +from stac_fastapi.types.core import BaseCoreClient + + +class DummyCoreClient(BaseCoreClient): + def all_collections(self, *args, **kwargs): + raise NotImplementedError + + def get_collection(self, *args, **kwargs): + raise NotImplementedError + + def get_item(self, *args, **kwargs): + raise NotImplementedError + + def get_search(self, *args, **kwargs): + raise NotImplementedError + + def post_search(self, *args, **kwargs): + return args[0].model_dump() + + def item_collection(self, *args, **kwargs): + raise NotImplementedError + + +@pytest.fixture +def client() -> Iterator[TestClient]: + settings = ApiSettings() + extensions = [FilterExtension()] + api = StacApi( + settings=settings, + client=DummyCoreClient(), + extensions=extensions, + search_get_request_model=create_get_request_model(extensions), + search_post_request_model=create_post_request_model(extensions), + ) + with TestClient(api.app) as client: + yield client + + +def test_search_filter_post_filter_lang_default(client: TestClient): + """Test search POST endpoint with filter ext.""" + response = client.post( + "/search", + json={ + "collections": ["test"], + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, + }, + ) + assert response.is_success, response.json() + response_dict = response.json() + assert response_dict["filter_lang"] == "cql2-json" + + +def test_search_filter_post_filter_lang_non_default(client: TestClient): + """Test search POST endpoint with filter ext.""" + filter_lang_value = "cql2-text" + response = client.post( + "/search", + json={ + "collections": ["test"], + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, + "filter-lang": filter_lang_value, + }, + ) + assert response.is_success, response.json() + response_dict = response.json() + assert response_dict["filter_lang"] == filter_lang_value