|
| 1 | +import json |
| 2 | +from typing import Iterator |
| 3 | +from urllib.parse import quote_plus, unquote_plus |
| 4 | + |
| 5 | +import pytest |
| 6 | +from starlette.testclient import TestClient |
| 7 | + |
| 8 | +from stac_fastapi.api.app import StacApi |
| 9 | +from stac_fastapi.api.models import create_get_request_model, create_post_request_model |
| 10 | +from stac_fastapi.extensions.core import QueryExtension |
| 11 | +from stac_fastapi.types.config import ApiSettings |
| 12 | +from stac_fastapi.types.core import BaseCoreClient |
| 13 | + |
| 14 | + |
| 15 | +class DummyCoreClient(BaseCoreClient): |
| 16 | + def all_collections(self, *args, **kwargs): |
| 17 | + raise NotImplementedError |
| 18 | + |
| 19 | + def get_collection(self, *args, **kwargs): |
| 20 | + raise NotImplementedError |
| 21 | + |
| 22 | + def get_item(self, *args, **kwargs): |
| 23 | + raise NotImplementedError |
| 24 | + |
| 25 | + def get_search(self, *args, **kwargs): |
| 26 | + return kwargs.pop("query", None) |
| 27 | + |
| 28 | + def post_search(self, *args, **kwargs): |
| 29 | + return args[0].query |
| 30 | + |
| 31 | + def item_collection(self, *args, **kwargs): |
| 32 | + raise NotImplementedError |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def client() -> Iterator[TestClient]: |
| 37 | + settings = ApiSettings() |
| 38 | + extensions = [QueryExtension()] |
| 39 | + |
| 40 | + api = StacApi( |
| 41 | + settings=settings, |
| 42 | + client=DummyCoreClient(), |
| 43 | + extensions=extensions, |
| 44 | + search_get_request_model=create_get_request_model(extensions), |
| 45 | + search_post_request_model=create_post_request_model(extensions), |
| 46 | + ) |
| 47 | + with TestClient(api.app) as client: |
| 48 | + yield client |
| 49 | + |
| 50 | + |
| 51 | +def test_search_query_get(client: TestClient): |
| 52 | + """Test search GET endpoints with query ext.""" |
| 53 | + response = client.get( |
| 54 | + "/search", |
| 55 | + params={"collections": ["test"]}, |
| 56 | + ) |
| 57 | + assert response.is_success |
| 58 | + assert not response.text |
| 59 | + |
| 60 | + response = client.get( |
| 61 | + "/search", |
| 62 | + params={ |
| 63 | + "collections": ["test"], |
| 64 | + "query": quote_plus( |
| 65 | + json.dumps({"eo:cloud_cover": {"gte": 95}}), |
| 66 | + ), |
| 67 | + }, |
| 68 | + ) |
| 69 | + assert response.is_success, response.json() |
| 70 | + query = json.loads(unquote_plus(response.json())) |
| 71 | + assert query["eo:cloud_cover"] == {"gte": 95} |
| 72 | + |
| 73 | + |
| 74 | +def test_search_query_post(client: TestClient): |
| 75 | + """Test search POST endpoints with query ext.""" |
| 76 | + response = client.post( |
| 77 | + "/search", |
| 78 | + json={ |
| 79 | + "collections": ["test"], |
| 80 | + }, |
| 81 | + ) |
| 82 | + |
| 83 | + assert response.is_success |
| 84 | + assert not response.text |
| 85 | + |
| 86 | + response = client.post( |
| 87 | + "/search", |
| 88 | + json={ |
| 89 | + "collections": ["test"], |
| 90 | + "query": {"eo:cloud_cover": {"gte": 95}}, |
| 91 | + }, |
| 92 | + ) |
| 93 | + |
| 94 | + assert response.is_success, response.json() |
| 95 | + assert response.json()["eo:cloud_cover"] == {"gte": 95} |
0 commit comments