|
| 1 | +from typing import Iterator |
| 2 | + |
| 3 | +import pytest |
| 4 | +from starlette.testclient import TestClient |
| 5 | + |
| 6 | +from stac_fastapi.api.app import StacApi |
| 7 | +from stac_fastapi.api.models import create_get_request_model, create_post_request_model |
| 8 | +from stac_fastapi.extensions.core import FilterExtension |
| 9 | +from stac_fastapi.types.config import ApiSettings |
| 10 | +from stac_fastapi.types.core import BaseCoreClient |
| 11 | + |
| 12 | + |
| 13 | +class DummyCoreClient(BaseCoreClient): |
| 14 | + def all_collections(self, *args, **kwargs): |
| 15 | + raise NotImplementedError |
| 16 | + |
| 17 | + def get_collection(self, *args, **kwargs): |
| 18 | + raise NotImplementedError |
| 19 | + |
| 20 | + def get_item(self, *args, **kwargs): |
| 21 | + raise NotImplementedError |
| 22 | + |
| 23 | + def get_search(self, *args, **kwargs): |
| 24 | + raise NotImplementedError |
| 25 | + |
| 26 | + def post_search(self, *args, **kwargs): |
| 27 | + return args[0].model_dump() |
| 28 | + |
| 29 | + def item_collection(self, *args, **kwargs): |
| 30 | + raise NotImplementedError |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def client() -> Iterator[TestClient]: |
| 35 | + settings = ApiSettings() |
| 36 | + extensions = [FilterExtension()] |
| 37 | + api = StacApi( |
| 38 | + settings=settings, |
| 39 | + client=DummyCoreClient(), |
| 40 | + extensions=extensions, |
| 41 | + search_get_request_model=create_get_request_model(extensions), |
| 42 | + search_post_request_model=create_post_request_model(extensions), |
| 43 | + ) |
| 44 | + with TestClient(api.app) as client: |
| 45 | + yield client |
| 46 | + |
| 47 | + |
| 48 | +def test_search_filter_post_filter_lang_default(client: TestClient): |
| 49 | + """Test search POST endpoint with filter ext.""" |
| 50 | + response = client.post( |
| 51 | + "/search", |
| 52 | + json={ |
| 53 | + "collections": ["test"], |
| 54 | + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, |
| 55 | + }, |
| 56 | + ) |
| 57 | + assert response.is_success, response.json() |
| 58 | + response_dict = response.json() |
| 59 | + assert response_dict["filter_lang"] == "cql2-json" |
| 60 | + |
| 61 | + |
| 62 | +def test_search_filter_post_filter_lang_non_default(client: TestClient): |
| 63 | + """Test search POST endpoint with filter ext.""" |
| 64 | + filter_lang_value = "cql2-text" |
| 65 | + response = client.post( |
| 66 | + "/search", |
| 67 | + json={ |
| 68 | + "collections": ["test"], |
| 69 | + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, |
| 70 | + "filter-lang": filter_lang_value, |
| 71 | + }, |
| 72 | + ) |
| 73 | + assert response.is_success, response.json() |
| 74 | + response_dict = response.json() |
| 75 | + assert response_dict["filter_lang"] == filter_lang_value |
0 commit comments