Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get sortby fix #50

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#50](https://github.com/stac-utils/stac-fastapi/pull/50))

## [2.4.4] - 2023-03-09

Expand Down
2 changes: 1 addition & 1 deletion stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 33 additions & 1 deletion tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
65 changes: 13 additions & 52 deletions tests/data/test_item_multipolygon.json
Original file line number Diff line number Diff line change
Expand Up @@ -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]
]
]
]
Expand Down
27 changes: 26 additions & 1 deletion tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
Loading