From 101c726690e809cbf7ad5f580e75729eddf9385c Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 23 Oct 2023 12:49:29 +0800 Subject: [PATCH 1/5] fix multipolygon --- tests/data/test_item_multipolygon.json | 65 ++++++-------------------- 1 file changed, 13 insertions(+), 52 deletions(-) diff --git a/tests/data/test_item_multipolygon.json b/tests/data/test_item_multipolygon.json index f5701c3..883a902 100644 --- a/tests/data/test_item_multipolygon.json +++ b/tests/data/test_item_multipolygon.json @@ -12,68 +12,29 @@ "id": "LE07_L2SP_092013_20211007_20211104_02_T2_SR", "description": "Landsat Collection 2 Level-2 Surface Reflectance Product", "bbox": [ - 175.93215804933186, - 65.93036549677463, - -178.26673562596073, - 68.07019813171695 + -180.0, 65.93036549677463, 180.0, 68.07019813171695 ], "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ - [ - 180.0, - 67.67956138964027 - ], - [ - 177.4008122028755, - 68.07019813171695 - ], - [ - 175.93215804933186, - 66.54096344674578 - ], - [ - 180.0, - 65.93733582837588 - ], - [ - 180.0, - 67.67956138964027 - ] + [180.0, 67.67956138964027], + [177.4008122028755, 68.07019813171695], + [175.93215804933186, 66.54096344674578], + [180.0, 65.93733582837588], + [180.0, 67.67956138964027] ] ], [ [ - [ - -180.0, - 65.93733582837588 - ], - [ - -179.95302698810534, - 65.93036549677463 - ], - [ - -178.3207049853914, - 67.36419976494292 - ], - [ - -178.26673562596073, - 67.41036545485302 - ], - [ - -178.27732165481333, - 67.42065687448587 - ], - [ - -180.0, - 67.67956138964027 - ], - [ - -180.0, - 65.93733582837588 - ] + [-180.0, 65.93733582837588], + [-179.95302698810534, 65.93036549677463], + [-178.3207049853914, 67.36419976494292], + [-178.26673562596073, 67.41036545485302], + [-178.27732165481333, 67.42065687448587], + [-180.0, 67.67956138964027], + [-180.0, 65.93733582837588] ] ] ] From dad56c390b081390c46b4e05dfc444d75493d56a Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 23 Oct 2023 12:50:13 +0800 Subject: [PATCH 2/5] fix sortby --- stac_fastapi/sqlalchemy/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stac_fastapi/sqlalchemy/core.py b/stac_fastapi/sqlalchemy/core.py index 15b15ee..eee886e 100644 --- a/stac_fastapi/sqlalchemy/core.py +++ b/stac_fastapi/sqlalchemy/core.py @@ -278,7 +278,7 @@ def get_search( sort_param.append( { "field": sort[1:], - "direction": "asc" if sort[0] == "+" else "desc", + "direction": "desc" if sort[0] == "-" else "asc", } ) base_args["sortby"] = sort_param From b87a646f3e7e6bbd77eade729d832f650da2dcf0 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 23 Oct 2023 12:50:43 +0800 Subject: [PATCH 3/5] add tests --- tests/api/test_api.py | 34 +++++++++++++++++++++++++++++++++- tests/resources/test_item.py | 27 ++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/tests/api/test_api.py b/tests/api/test_api.py index 6fdbb6e..71ee701 100644 --- a/tests/api/test_api.py +++ b/tests/api/test_api.py @@ -225,7 +225,39 @@ def test_app_query_extension_limit_10000( assert resp.status_code == 200 -def test_app_sort_extension(load_test_data, app_client, postgres_transactions): +def test_app_sort_extension_post_asc(load_test_data, app_client, postgres_transactions): + first_item = load_test_data("test_item.json") + item_date = datetime.strptime( + first_item["properties"]["datetime"], "%Y-%m-%dT%H:%M:%SZ" + ) + postgres_transactions.create_item( + first_item["collection"], first_item, request=MockStarletteRequest + ) + + second_item = load_test_data("test_item.json") + second_item["id"] = "another-item" + another_item_date = item_date - timedelta(days=1) + second_item["properties"]["datetime"] = another_item_date.strftime( + "%Y-%m-%dT%H:%M:%SZ" + ) + postgres_transactions.create_item( + second_item["collection"], second_item, request=MockStarletteRequest + ) + + params = { + "collections": [first_item["collection"]], + "sortby": [{"field": "datetime", "direction": "asc"}], + } + resp = app_client.post("/search", json=params) + assert resp.status_code == 200 + resp_json = resp.json() + assert resp_json["features"][1]["id"] == first_item["id"] + assert resp_json["features"][0]["id"] == second_item["id"] + + +def test_app_sort_extension_post_desc( + load_test_data, app_client, postgres_transactions +): first_item = load_test_data("test_item.json") item_date = datetime.strptime( first_item["properties"]["datetime"], "%Y-%m-%dT%H:%M:%SZ" diff --git a/tests/resources/test_item.py b/tests/resources/test_item.py index 27ecfc0..cd7b3f3 100644 --- a/tests/resources/test_item.py +++ b/tests/resources/test_item.py @@ -581,7 +581,32 @@ def test_item_search_temporal_window_get(app_client, load_test_data): assert resp_json["features"][0]["id"] == test_item["id"] -def test_item_search_sort_get(app_client, load_test_data): +def test_item_search_sort_get_asc(app_client, load_test_data): + """Test GET search with sorting (sort extension)""" + first_item = load_test_data("test_item.json") + item_date = rfc3339_str_to_datetime(first_item["properties"]["datetime"]) + resp = app_client.post( + f"/collections/{first_item['collection']}/items", json=first_item + ) + assert resp.status_code == 200 + + second_item = load_test_data("test_item.json") + second_item["id"] = "another-item" + another_item_date = item_date - timedelta(days=1) + second_item["properties"]["datetime"] = datetime_to_str(another_item_date) + resp = app_client.post( + f"/collections/{second_item['collection']}/items", json=second_item + ) + assert resp.status_code == 200 + params = {"collections": [first_item["collection"]], "sortby": "+datetime"} + resp = app_client.get("/search", params=params) + assert resp.status_code == 200 + resp_json = resp.json() + assert resp_json["features"][1]["id"] == first_item["id"] + assert resp_json["features"][0]["id"] == second_item["id"] + + +def test_item_search_sort_get_desc(app_client, load_test_data): """Test GET search with sorting (sort extension)""" first_item = load_test_data("test_item.json") item_date = rfc3339_str_to_datetime(first_item["properties"]["datetime"]) From 2ad1e7d33303c847ad5667085417e9a2a7752579 Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 23 Oct 2023 12:59:22 +0800 Subject: [PATCH 4/5] update changelog --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index ba68a16..4ef9931 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ As a part of this release, this repository was extracted from the main ### Fixed * Fix bad links generated by search for pagination ([#14](https://github.com/stac-utils/stac-fastapi-sqlalchemy/pull/14/files)) +* Fix GET /search sortby ([#46](https://github.com/stac-utils/stac-fastapi/pull/46)) ## [2.4.4] - 2023-03-09 From fb8605bd7d0a47fc798929b7c57a57931745b54c Mon Sep 17 00:00:00 2001 From: jonhealy1 Date: Mon, 23 Oct 2023 13:03:01 +0800 Subject: [PATCH 5/5] fix changelog link --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 4ef9931..80a07d3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,7 +12,7 @@ As a part of this release, this repository was extracted from the main ### Fixed * Fix bad links generated by search for pagination ([#14](https://github.com/stac-utils/stac-fastapi-sqlalchemy/pull/14/files)) -* Fix GET /search sortby ([#46](https://github.com/stac-utils/stac-fastapi/pull/46)) +* Fix GET /search sortby ([#50](https://github.com/stac-utils/stac-fastapi/pull/50)) ## [2.4.4] - 2023-03-09