From f8e22567ab4c9c09641291bca7b2ba3f4874bd0a Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Nov 2024 13:36:11 -0500 Subject: [PATCH 1/5] test cleanup --- .../tests/extensions/test_aggregation.py | 88 +++++++------------ 1 file changed, 30 insertions(+), 58 deletions(-) diff --git a/stac_fastapi/tests/extensions/test_aggregation.py b/stac_fastapi/tests/extensions/test_aggregation.py index 94a5c247..91791c72 100644 --- a/stac_fastapi/tests/extensions/test_aggregation.py +++ b/stac_fastapi/tests/extensions/test_aggregation.py @@ -7,7 +7,7 @@ @pytest.mark.asyncio -async def test_aggregation_extension_landing_page_link(app_client, ctx): +async def test_aggregation_extension_landing_page_link(app_client): """Test if the `aggregations` and `aggregate` links are included in the landing page""" resp = await app_client.get("/") assert resp.status_code == 200 @@ -20,23 +20,15 @@ async def test_aggregation_extension_landing_page_link(app_client, ctx): @pytest.mark.asyncio -async def test_aggregation_extension_collection_link(app_client, ctx, load_test_data): +async def test_aggregation_extension_collection_link(app_client, ctx): """Test if the `aggregations` and `aggregate` links are included in the collection links""" - test_collection = load_test_data("test_collection.json") - test_collection["id"] = "test" - resp = await app_client.post("/collections", json=test_collection) - assert resp.status_code == 201 - - resp = await app_client.get(f"/collections/{test_collection['id']}") + resp = await app_client.get(f"/collections/{ctx.collection['id']}") resp_json = resp.json() keys = [link["rel"] for link in resp_json["links"]] assert "aggregations" in keys assert "aggregate" in keys - resp = await app_client.delete(f"/collections/{test_collection['id']}") - assert resp.status_code == 204 - @pytest.mark.asyncio async def test_get_catalog_aggregations(app_client): @@ -57,49 +49,31 @@ async def test_post_catalog_aggregations(app_client): @pytest.mark.asyncio -async def test_get_collection_aggregations(app_client, ctx, load_test_data): +async def test_get_collection_aggregations(app_client, ctx): # there's one item that can match, so one of these queries should match it and the other shouldn't - test_collection = load_test_data("test_collection.json") - test_collection["id"] = "test" - - resp = await app_client.post("/collections", json=test_collection) - assert resp.status_code == 201 - - resp = await app_client.get(f"/collections/{test_collection['id']}/aggregations") + resp = await app_client.get(f"/collections/{ctx.collection['id']}/aggregations") assert resp.status_code == 200 assert len(resp.json()["aggregations"]) == 15 rj = resp.json() href_self = urlparse( next(link["href"] for link in rj["links"] if link["rel"] == "self") ) - assert href_self.path == f"/collections/{test_collection['id']}/aggregations" - - resp = await app_client.delete(f"/collections/{test_collection['id']}") - assert resp.status_code == 204 + assert href_self.path == f"/collections/{ctx.collection['id']}/aggregations" @pytest.mark.asyncio -async def test_post_collection_aggregations(app_client, ctx, load_test_data): +async def test_post_collection_aggregations(app_client, ctx): # there's one item that can match, so one of these queries should match it and the other shouldn't - test_collection = load_test_data("test_collection.json") - test_collection["id"] = "test" - - resp = await app_client.post("/collections", json=test_collection) - assert resp.status_code == 201 - - resp = await app_client.post(f"/collections/{test_collection['id']}/aggregations") + resp = await app_client.post(f"/collections/{ctx.collection['id']}/aggregations") assert resp.status_code == 200 assert len(resp.json()["aggregations"]) == 15 rj = resp.json() href_self = urlparse( next(link["href"] for link in rj["links"] if link["rel"] == "self") ) - assert href_self.path == f"/collections/{test_collection['id']}/aggregations" - - resp = await app_client.delete(f"/collections/{test_collection['id']}") - assert resp.status_code == 204 + assert href_self.path == f"/collections/{ctx.collection['id']}/aggregations" @pytest.mark.asyncio @@ -119,7 +93,7 @@ async def test_aggregate_search_point_does_not_intersect(app_client, ctx): @pytest.mark.asyncio -async def test_get_collection_aggregate_no_collection(app_client, ctx, load_test_data): +async def test_get_collection_aggregate_no_collection(app_client): resp = await app_client.get( "/collections/not-a-collection/aggregate?aggregations=total_count" @@ -128,33 +102,31 @@ async def test_get_collection_aggregate_no_collection(app_client, ctx, load_test @pytest.mark.asyncio -async def test_get_collection_aggregate(app_client, ctx, load_test_data): - test_collection = load_test_data("test_collection.json") +async def test_get_collection_aggregate(app_client, ctx): resp = await app_client.get( - f"/collections/{test_collection['id']}/aggregate?aggregations=total_count" + f"/collections/{ctx.collection['id']}/aggregate?aggregations=total_count" ) assert resp.status_code == 200 assert resp.json()["aggregations"][0]["value"] == 1 @pytest.mark.asyncio -async def test_post_collection_aggregate(app_client, ctx, load_test_data): - test_collection = load_test_data("test_collection.json") +async def test_post_collection_aggregate(app_client, ctx): params = { "aggregations": ["total_count"], } resp = await app_client.post( - f"/collections/{test_collection['id']}/aggregate", json=params + f"/collections/{ctx.collection['id']}/aggregate", json=params ) assert resp.status_code == 200 assert resp.json()["aggregations"][0]["value"] == 1 @pytest.mark.asyncio -async def test_aggregate_datetime_out_of_range(app_client, ctx): +async def test_aggregate_datetime_out_of_range(app_client): params = { "datetime": "2023-07-14T02:05:01.324Z/2024-02-28T23:13:08.000Z", "aggregations": ["total_count"], @@ -165,7 +137,7 @@ async def test_aggregate_datetime_out_of_range(app_client, ctx): @pytest.mark.asyncio -async def test_aggregate_datetime_in_range(app_client, ctx): +async def test_aggregate_datetime_in_range(app_client): params = { "datetime": "2020-02-11T12:30:22Z/2020-02-13T12:30:22Z", "aggregations": ["total_count"], @@ -201,7 +173,7 @@ async def test_aggregate_filter_extension_neq_post(app_client, ctx): @pytest.mark.asyncio -async def test_aggregate_extension_gte_get(app_client, ctx): +async def test_aggregate_extension_gte_get(app_client): # there's one item that can match, so one of these queries should match it and the other shouldn't resp = await app_client.get( '/aggregate?aggregations=total_count&filter-lang=cql2-json&filter={"op":"<=","args":[{"property": "properties.proj:epsg"},32756]}' @@ -396,7 +368,7 @@ async def test_aggregate_filter_extension_in_no_list(app_client, ctx): @pytest.mark.asyncio -async def test_aggregate_datetime_non_interval(app_client, ctx): +async def test_aggregate_datetime_non_interval(app_client): dt_formats = [ "2020-02-12T12:30:22+00:00", "2020-02-12T12:30:22.00Z", @@ -413,7 +385,7 @@ async def test_aggregate_datetime_non_interval(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_total_count(app_client, ctx): +async def test_post_aggregate_total_count(app_client): params = { "aggregations": ["total_count"], @@ -426,7 +398,7 @@ async def test_post_aggregate_total_count(app_client, ctx): @pytest.mark.asyncio -async def test_get_aggregate_total_count(app_client, ctx): +async def test_get_aggregate_total_count(app_client): resp = await app_client.get("/aggregate?aggregations=total_count") @@ -435,7 +407,7 @@ async def test_get_aggregate_total_count(app_client, ctx): @pytest.mark.asyncio -async def test_get_aggregate_datetime_max(app_client, ctx): +async def test_get_aggregate_datetime_max(app_client): resp = await app_client.get("/aggregate?aggregations=datetime_max") @@ -447,7 +419,7 @@ async def test_get_aggregate_datetime_max(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_datetime_max(app_client, ctx): +async def test_post_aggregate_datetime_max(app_client): params = { "aggregations": ["datetime_max"], @@ -463,7 +435,7 @@ async def test_post_aggregate_datetime_max(app_client, ctx): @pytest.mark.asyncio -async def test_get_aggregate_datetime_min(app_client, ctx): +async def test_get_aggregate_datetime_min(app_client): resp = await app_client.get("/aggregate?aggregations=datetime_min") @@ -475,7 +447,7 @@ async def test_get_aggregate_datetime_min(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_datetime_min(app_client, ctx): +async def test_post_aggregate_datetime_min(app_client): params = { "aggregations": ["datetime_min"], @@ -491,7 +463,7 @@ async def test_post_aggregate_datetime_min(app_client, ctx): @pytest.mark.asyncio -async def test_get_aggregate_datetime_frequency(app_client, ctx): +async def test_get_aggregate_datetime_frequency(app_client): resp = await app_client.get("/aggregate?aggregations=datetime_frequency") @@ -504,7 +476,7 @@ async def test_get_aggregate_datetime_frequency(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_datetime_frequency(app_client, ctx): +async def test_post_aggregate_datetime_frequency(app_client): params = { "aggregations": ["datetime_frequency"], @@ -521,7 +493,7 @@ async def test_post_aggregate_datetime_frequency(app_client, ctx): @pytest.mark.asyncio -async def test_get_aggregate_collection_frequency(app_client, ctx): +async def test_get_aggregate_collection_frequency(app_client): resp = await app_client.get("/aggregate?aggregations=collection_frequency") @@ -531,7 +503,7 @@ async def test_get_aggregate_collection_frequency(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_collection_frequency(app_client, ctx): +async def test_post_aggregate_collection_frequency(app_client): params = { "aggregations": ["collection_frequency"], @@ -572,7 +544,7 @@ async def test_post_aggregate_attribute_frequency(app_client, ctx): @pytest.mark.asyncio -async def test_post_aggregate_unsupported_aggregation(app_client, ctx): +async def test_post_aggregate_unsupported_aggregation(app_client): params = { "aggregations": ["this_is_not_an_aggregation"], @@ -597,7 +569,7 @@ async def test_post_aggregate_unsupported_collection_aggregation(app_client, ctx @pytest.mark.asyncio -async def test_get_aggregate_precision_outside_range(app_client, ctx): +async def test_get_aggregate_precision_outside_range(app_client): resp = await app_client.get( "/aggregate?aggregations=centroid_geohash_grid_frequency¢roid_geohash_grid_frequency_precision=55" From 45cd558053a0d7984741c29678fc2564ab341a6c Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Nov 2024 19:45:40 -0500 Subject: [PATCH 2/5] replace load_data --- stac_fastapi/tests/extensions/test_filter.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/stac_fastapi/tests/extensions/test_filter.py b/stac_fastapi/tests/extensions/test_filter.py index 8f4fa5ee..f92127ee 100644 --- a/stac_fastapi/tests/extensions/test_filter.py +++ b/stac_fastapi/tests/extensions/test_filter.py @@ -20,22 +20,14 @@ async def test_filter_extension_landing_page_link(app_client, ctx): @pytest.mark.asyncio -async def test_filter_extension_collection_link(app_client, load_test_data): +async def test_filter_extension_collection_link(app_client, ctx): """Test creation and deletion of a collection""" - test_collection = load_test_data("test_collection.json") - test_collection["id"] = "test" - resp = await app_client.post("/collections", json=test_collection) - assert resp.status_code == 201 - - resp = await app_client.get(f"/collections/{test_collection['id']}") + resp = await app_client.get(f"/collections/{ctx.collection['id']}") resp_json = resp.json() keys = [link["rel"] for link in resp_json["links"]] assert "queryables" in keys - resp = await app_client.delete(f"/collections/{test_collection['id']}") - assert resp.status_code == 204 - @pytest.mark.asyncio async def test_search_filters_post(app_client, ctx): From aa4dfbf1ebff8c1af17acd9b703d3998559c22ef Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Nov 2024 21:45:29 -0500 Subject: [PATCH 3/5] es indices() empty list --- .../stac_fastapi/elasticsearch/database_logic.py | 3 +-- stac_fastapi/tests/extensions/test_aggregation.py | 6 ++---- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py index da6d6880..43c3cb6a 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py @@ -168,7 +168,7 @@ def indices(collection_ids: Optional[List[str]]) -> str: Returns: A string of comma-separated index names. If `collection_ids` is None, returns the default indices. """ - if collection_ids is None: + if collection_ids is None or collection_ids == []: return ITEM_INDICES else: return ",".join([index_by_collection_id(c) for c in collection_ids]) @@ -764,7 +764,6 @@ def _fill_aggregation_parameters(name: str, agg: dict) -> dict: for k, v in self.aggregation_mapping.items() if k in aggregations } - index_param = indices(collection_ids) search_task = asyncio.create_task( self.client.search( diff --git a/stac_fastapi/tests/extensions/test_aggregation.py b/stac_fastapi/tests/extensions/test_aggregation.py index 91791c72..705eaa81 100644 --- a/stac_fastapi/tests/extensions/test_aggregation.py +++ b/stac_fastapi/tests/extensions/test_aggregation.py @@ -173,7 +173,7 @@ async def test_aggregate_filter_extension_neq_post(app_client, ctx): @pytest.mark.asyncio -async def test_aggregate_extension_gte_get(app_client): +async def test_aggregate_extension_gte_get(app_client, ctx): # there's one item that can match, so one of these queries should match it and the other shouldn't resp = await app_client.get( '/aggregate?aggregations=total_count&filter-lang=cql2-json&filter={"op":"<=","args":[{"property": "properties.proj:epsg"},32756]}' @@ -387,9 +387,7 @@ async def test_aggregate_datetime_non_interval(app_client): @pytest.mark.asyncio async def test_post_aggregate_total_count(app_client): - params = { - "aggregations": ["total_count"], - } + params = {"aggregations": ["total_count"]} resp = await app_client.post("/aggregate", json=params) From 19f6caf98da0eb8150d17a4378060e720ab08a18 Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Nov 2024 21:49:38 -0500 Subject: [PATCH 4/5] re-add ctx --- stac_fastapi/tests/extensions/test_aggregation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/stac_fastapi/tests/extensions/test_aggregation.py b/stac_fastapi/tests/extensions/test_aggregation.py index 705eaa81..f8c04a70 100644 --- a/stac_fastapi/tests/extensions/test_aggregation.py +++ b/stac_fastapi/tests/extensions/test_aggregation.py @@ -461,7 +461,7 @@ async def test_post_aggregate_datetime_min(app_client): @pytest.mark.asyncio -async def test_get_aggregate_datetime_frequency(app_client): +async def test_get_aggregate_datetime_frequency(app_client, ctx): resp = await app_client.get("/aggregate?aggregations=datetime_frequency") @@ -474,7 +474,7 @@ async def test_get_aggregate_datetime_frequency(app_client): @pytest.mark.asyncio -async def test_post_aggregate_datetime_frequency(app_client): +async def test_post_aggregate_datetime_frequency(app_client, ctx): params = { "aggregations": ["datetime_frequency"], @@ -491,7 +491,7 @@ async def test_post_aggregate_datetime_frequency(app_client): @pytest.mark.asyncio -async def test_get_aggregate_collection_frequency(app_client): +async def test_get_aggregate_collection_frequency(app_client, ctx): resp = await app_client.get("/aggregate?aggregations=collection_frequency") @@ -501,7 +501,7 @@ async def test_get_aggregate_collection_frequency(app_client): @pytest.mark.asyncio -async def test_post_aggregate_collection_frequency(app_client): +async def test_post_aggregate_collection_frequency(app_client, ctx): params = { "aggregations": ["collection_frequency"], From 81ab81e0aa8fa5089be65fd06941d12b339f636b Mon Sep 17 00:00:00 2001 From: James Date: Sat, 23 Nov 2024 21:53:02 -0500 Subject: [PATCH 5/5] readd ctx --- stac_fastapi/tests/extensions/test_aggregation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stac_fastapi/tests/extensions/test_aggregation.py b/stac_fastapi/tests/extensions/test_aggregation.py index f8c04a70..eb515592 100644 --- a/stac_fastapi/tests/extensions/test_aggregation.py +++ b/stac_fastapi/tests/extensions/test_aggregation.py @@ -445,7 +445,7 @@ async def test_get_aggregate_datetime_min(app_client): @pytest.mark.asyncio -async def test_post_aggregate_datetime_min(app_client): +async def test_post_aggregate_datetime_min(app_client, ctx): params = { "aggregations": ["datetime_min"],