|
| 1 | +import json |
| 2 | +from urllib.parse import quote_plus |
| 3 | + |
| 4 | +from starlette.testclient import TestClient |
| 5 | + |
| 6 | +from stac_fastapi.api.app import StacApi |
| 7 | +from stac_fastapi.api.models import create_request_model |
| 8 | +from stac_fastapi.extensions.core import CollectionSearchExtension |
| 9 | +from stac_fastapi.extensions.core.collection_search import ConformanceClasses |
| 10 | +from stac_fastapi.extensions.core.collection_search.request import ( |
| 11 | + CollectionSearchExtensionGetRequest, |
| 12 | +) |
| 13 | +from stac_fastapi.extensions.core.fields.request import FieldsExtensionGetRequest |
| 14 | +from stac_fastapi.extensions.core.filter.request import FilterExtensionGetRequest |
| 15 | +from stac_fastapi.extensions.core.free_text.request import FreeTextExtensionGetRequest |
| 16 | +from stac_fastapi.extensions.core.query.request import QueryExtensionGetRequest |
| 17 | +from stac_fastapi.extensions.core.sort.request import SortExtensionGetRequest |
| 18 | +from stac_fastapi.types.config import ApiSettings |
| 19 | +from stac_fastapi.types.core import BaseCoreClient |
| 20 | + |
| 21 | + |
| 22 | +class DummyCoreClient(BaseCoreClient): |
| 23 | + def all_collections(self, *args, **kwargs): |
| 24 | + _ = kwargs.pop("request", None) |
| 25 | + return kwargs |
| 26 | + |
| 27 | + def get_collection(self, *args, **kwargs): |
| 28 | + raise NotImplementedError |
| 29 | + |
| 30 | + def get_item(self, *args, **kwargs): |
| 31 | + raise NotImplementedError |
| 32 | + |
| 33 | + def get_search(self, *args, **kwargs): |
| 34 | + raise NotImplementedError |
| 35 | + |
| 36 | + def post_search(self, *args, **kwargs): |
| 37 | + return args[0].model_dump() |
| 38 | + |
| 39 | + def item_collection(self, *args, **kwargs): |
| 40 | + raise NotImplementedError |
| 41 | + |
| 42 | + |
| 43 | +def test_collection_search_extension_default(): |
| 44 | + """Test /collections endpoint with collection-search ext.""" |
| 45 | + api = StacApi( |
| 46 | + settings=ApiSettings(), |
| 47 | + client=DummyCoreClient(), |
| 48 | + extensions=[CollectionSearchExtension()], |
| 49 | + collections_get_request_model=CollectionSearchExtensionGetRequest, |
| 50 | + ) |
| 51 | + with TestClient(api.app) as client: |
| 52 | + response = client.get("/conformance") |
| 53 | + assert response.is_success, response.json() |
| 54 | + response_dict = response.json() |
| 55 | + assert ( |
| 56 | + "https://api.stacspec.org/v1.0.0-rc.1/collection-search" |
| 57 | + in response_dict["conformsTo"] |
| 58 | + ) |
| 59 | + assert ( |
| 60 | + "http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query" |
| 61 | + in response_dict["conformsTo"] |
| 62 | + ) |
| 63 | + |
| 64 | + response = client.get("/collections") |
| 65 | + assert response.is_success, response.json() |
| 66 | + response_dict = response.json() |
| 67 | + assert "bbox" in response_dict |
| 68 | + assert "datetime" in response_dict |
| 69 | + assert "limit" in response_dict |
| 70 | + |
| 71 | + response = client.get( |
| 72 | + "/collections", |
| 73 | + params={ |
| 74 | + "datetime": "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z", |
| 75 | + "bbox": "-175.05,-85.05,175.05,85.05", |
| 76 | + "limit": 100, |
| 77 | + }, |
| 78 | + ) |
| 79 | + assert response.is_success, response.json() |
| 80 | + response_dict = response.json() |
| 81 | + assert [-175.05, -85.05, 175.05, 85.05] == response_dict["bbox"] |
| 82 | + assert [ |
| 83 | + "2020-06-13T13:00:00+00:00", |
| 84 | + "2020-06-13T14:00:00+00:00", |
| 85 | + ] == response_dict["datetime"] |
| 86 | + assert 100 == response_dict["limit"] |
| 87 | + |
| 88 | + |
| 89 | +def test_collection_search_extension_models(): |
| 90 | + """Test /collections endpoint with collection-search ext with additional models.""" |
| 91 | + collections_get_request_model = create_request_model( |
| 92 | + model_name="SearchGetRequest", |
| 93 | + base_model=CollectionSearchExtensionGetRequest, |
| 94 | + mixins=[ |
| 95 | + FreeTextExtensionGetRequest, |
| 96 | + FilterExtensionGetRequest, |
| 97 | + QueryExtensionGetRequest, |
| 98 | + SortExtensionGetRequest, |
| 99 | + FieldsExtensionGetRequest, |
| 100 | + ], |
| 101 | + request_type="GET", |
| 102 | + ) |
| 103 | + |
| 104 | + api = StacApi( |
| 105 | + settings=ApiSettings(), |
| 106 | + client=DummyCoreClient(), |
| 107 | + extensions=[ |
| 108 | + CollectionSearchExtension( |
| 109 | + conformance_classes=[ |
| 110 | + ConformanceClasses.COLLECTIONSEARCH, |
| 111 | + ConformanceClasses.BASIS, |
| 112 | + ConformanceClasses.FREETEXT, |
| 113 | + ConformanceClasses.FILTER, |
| 114 | + ConformanceClasses.QUERY, |
| 115 | + ConformanceClasses.SORT, |
| 116 | + ConformanceClasses.FIELDS, |
| 117 | + ] |
| 118 | + ) |
| 119 | + ], |
| 120 | + collections_get_request_model=collections_get_request_model, |
| 121 | + ) |
| 122 | + with TestClient(api.app) as client: |
| 123 | + response = client.get("/conformance") |
| 124 | + assert response.is_success, response.json() |
| 125 | + response_dict = response.json() |
| 126 | + conforms = response_dict["conformsTo"] |
| 127 | + assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search" in conforms |
| 128 | + assert ( |
| 129 | + "http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/simple-query" |
| 130 | + in conforms |
| 131 | + ) |
| 132 | + assert ( |
| 133 | + "https://api.stacspec.org/v1.0.0-rc.1/collection-search#free-text" in conforms |
| 134 | + ) |
| 135 | + assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#filter" in conforms |
| 136 | + assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#query" in conforms |
| 137 | + assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#sort" in conforms |
| 138 | + assert "https://api.stacspec.org/v1.0.0-rc.1/collection-search#fields" in conforms |
| 139 | + |
| 140 | + response = client.get("/collections") |
| 141 | + assert response.is_success, response.json() |
| 142 | + response_dict = response.json() |
| 143 | + assert "bbox" in response_dict |
| 144 | + assert "datetime" in response_dict |
| 145 | + assert "limit" in response_dict |
| 146 | + assert "q" in response_dict |
| 147 | + assert "filter" in response_dict |
| 148 | + assert "query" in response_dict |
| 149 | + assert "sortby" in response_dict |
| 150 | + assert "fields" in response_dict |
| 151 | + |
| 152 | + response = client.get( |
| 153 | + "/collections", |
| 154 | + params={ |
| 155 | + "datetime": "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z", |
| 156 | + "bbox": "-175.05,-85.05,175.05,85.05", |
| 157 | + "limit": 100, |
| 158 | + "q": "EO,Earth Observation", |
| 159 | + "filter": "id='item_id' AND collection='collection_id'", |
| 160 | + "query": quote_plus( |
| 161 | + json.dumps({"eo:cloud_cover": {"gte": 95}}), |
| 162 | + ), |
| 163 | + "sortby": "-gsd,-datetime", |
| 164 | + "fields": "properties.datetime", |
| 165 | + }, |
| 166 | + ) |
| 167 | + assert response.is_success, response.json() |
| 168 | + response_dict = response.json() |
| 169 | + assert [-175.05, -85.05, 175.05, 85.05] == response_dict["bbox"] |
| 170 | + assert [ |
| 171 | + "2020-06-13T13:00:00+00:00", |
| 172 | + "2020-06-13T14:00:00+00:00", |
| 173 | + ] == response_dict["datetime"] |
| 174 | + assert 100 == response_dict["limit"] |
| 175 | + assert ["EO", "Earth Observation"] == response_dict["q"] |
| 176 | + assert "id='item_id' AND collection='collection_id'" == response_dict["filter"] |
| 177 | + assert "filter_crs" in response_dict |
| 178 | + assert "cql2-text" in response_dict["filter_lang"] |
| 179 | + assert "query" in response_dict |
| 180 | + assert ["-gsd", "-datetime"] == response_dict["sortby"] |
| 181 | + assert ["properties.datetime"] == response_dict["fields"] |
0 commit comments