Skip to content

Commit 67d1cb9

Browse files
committed
tests
1 parent db3c9ba commit 67d1cb9

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010

1111
### Added
1212

13+
- Added Fields Extension implementation for the `/collections/{collection_id}/aggregations` endpoint.
14+
1315
### Changed
1416

1517
- Changed assets serialization to prevent mapping explosion while allowing asset inforamtion to be indexed. [#341](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/341)

stac_fastapi/tests/api/test_api.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,74 @@ async def test_app_fields_extension_return_all_properties(
228228
assert feature["properties"][expected_prop] == expected_value
229229

230230

231+
@pytest.mark.asyncio
232+
async def test_app_fields_extension_collection_items(app_client, ctx, txn_client):
233+
resp = await app_client.get(
234+
"/collections/test-collection/items",
235+
params={"fields": "+properties.datetime"},
236+
)
237+
assert resp.status_code == 200
238+
resp_json = resp.json()
239+
assert list(resp_json["features"][0]["properties"]) == ["datetime"]
240+
241+
242+
@pytest.mark.asyncio
243+
async def test_app_fields_extension_no_properties_get_collection_items(
244+
app_client, ctx, txn_client
245+
):
246+
resp = await app_client.get(
247+
"/collections/test-collection/items", params={"fields": "-properties"}
248+
)
249+
assert resp.status_code == 200
250+
resp_json = resp.json()
251+
assert "properties" not in resp_json["features"][0]
252+
253+
254+
@pytest.mark.asyncio
255+
async def test_app_fields_extension_no_null_fields_collection_items(
256+
app_client, ctx, txn_client
257+
):
258+
resp = await app_client.get("/collections/test-collection/items")
259+
assert resp.status_code == 200
260+
resp_json = resp.json()
261+
# check if no null fields: https://github.com/stac-utils/stac-fastapi-elasticsearch/issues/166
262+
for feature in resp_json["features"]:
263+
# assert "bbox" not in feature["geometry"]
264+
for link in feature["links"]:
265+
assert all(a not in link or link[a] is not None for a in ("title", "asset"))
266+
for asset in feature["assets"]:
267+
assert all(
268+
a not in asset or asset[a] is not None
269+
for a in ("start_datetime", "created")
270+
)
271+
272+
273+
@pytest.mark.asyncio
274+
async def test_app_fields_extension_return_all_properties_collection_items(
275+
app_client, ctx, txn_client, load_test_data
276+
):
277+
item = load_test_data("test_item.json")
278+
resp = await app_client.get(
279+
"/collections/test-collection/items",
280+
params={"collections": ["test-collection"], "fields": "properties"},
281+
)
282+
assert resp.status_code == 200
283+
resp_json = resp.json()
284+
feature = resp_json["features"][0]
285+
assert len(feature["properties"]) >= len(item["properties"])
286+
for expected_prop, expected_value in item["properties"].items():
287+
if expected_prop in (
288+
"datetime",
289+
"start_datetime",
290+
"end_datetime",
291+
"created",
292+
"updated",
293+
):
294+
assert feature["properties"][expected_prop][0:19] == expected_value[0:19]
295+
else:
296+
assert feature["properties"][expected_prop] == expected_value
297+
298+
231299
@pytest.mark.asyncio
232300
async def test_app_query_extension_gt(app_client, ctx):
233301
params = {"query": {"proj:epsg": {"gt": ctx.item["properties"]["proj:epsg"]}}}

stac_fastapi/tests/conftest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)
2626
from stac_fastapi.core.rate_limit import setup_rate_limit
2727
from stac_fastapi.core.utilities import get_bool_env
28+
from stac_fastapi.extensions.core.fields import FieldsConformanceClasses
2829
from stac_fastapi.sfeos_helpers.aggregation import EsAsyncBaseAggregationClient
2930
from stac_fastapi.sfeos_helpers.mappings import ITEMS_INDEX_PREFIX
3031

@@ -361,9 +362,12 @@ def build_test_app():
361362
aggregation_extension.POST = EsAggregationExtensionPostRequest
362363
aggregation_extension.GET = EsAggregationExtensionGetRequest
363364

365+
fields_extension = FieldsExtension()
366+
fields_extension.conformance_classes.append(FieldsConformanceClasses.ITEMS)
367+
364368
search_extensions = [
369+
fields_extension,
365370
SortExtension(),
366-
FieldsExtension(),
367371
QueryExtension(),
368372
TokenPaginationExtension(),
369373
FilterExtension(),

stac_fastapi/tests/resources/test_item.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,35 @@ async def test_field_extension_exclude_default_includes(app_client, ctx):
869869
assert "gsd" not in resp_json["features"][0]
870870

871871

872+
@pytest.mark.asyncio
873+
async def test_field_extension_get_includes_collection_items(app_client, ctx):
874+
"""Test GET collections/{collection_id}/items with included fields (fields extension)"""
875+
test_item = ctx.item
876+
params = {
877+
"fields": "+properties.proj:epsg,+properties.gsd",
878+
}
879+
resp = await app_client.get(
880+
f"/collections/{test_item['collection']}/items", params=params
881+
)
882+
feat_properties = resp.json()["features"][0]["properties"]
883+
assert not set(feat_properties) - {"proj:epsg", "gsd", "datetime"}
884+
885+
886+
@pytest.mark.asyncio
887+
async def test_field_extension_get_excludes_collection_items(app_client, ctx):
888+
"""Test GET collections/{collection_id}/items with included fields (fields extension)"""
889+
test_item = ctx.item
890+
params = {
891+
"fields": "-properties.proj:epsg,-properties.gsd",
892+
}
893+
resp = await app_client.get(
894+
f"/collections/{test_item['collection']}/items", params=params
895+
)
896+
resp_json = resp.json()
897+
assert "proj:epsg" not in resp_json["features"][0]["properties"].keys()
898+
assert "gsd" not in resp_json["features"][0]["properties"].keys()
899+
900+
872901
@pytest.mark.asyncio
873902
async def test_search_intersects_and_bbox(app_client):
874903
"""Test POST search intersects and bbox are mutually exclusive (core)"""

0 commit comments

Comments
 (0)