From ba7a1999bac8ba98f5bd6783ea3670f7dec108b1 Mon Sep 17 00:00:00 2001 From: Luca Fabbri Date: Tue, 30 May 2023 15:12:27 +0200 Subject: [PATCH] Fix bad href substitution on pagination usage (#14) * Fix bad href substitution on pagination usage This closes #13 * Added test coverage for #13 * Made search pagination test not relying on order of results * Update tests/resources/test_item.py Co-authored-by: Pete Gadomski * Update tests/resources/test_item.py Co-authored-by: Pete Gadomski --------- Co-authored-by: Pete Gadomski --- CHANGES.md | 4 ++++ stac_fastapi/sqlalchemy/core.py | 6 +++--- tests/resources/test_item.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 70a7129..ba68a16 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -9,6 +9,10 @@ As a part of this release, this repository was extracted from the main * Default branch to **main** ([#544](https://github.com/stac-utils/stac-fastapi/pull/544)) +### Fixed + +* Fix bad links generated by search for pagination ([#14](https://github.com/stac-utils/stac-fastapi-sqlalchemy/pull/14/files)) + ## [2.4.4] - 2023-03-09 ### Added diff --git a/stac_fastapi/sqlalchemy/core.py b/stac_fastapi/sqlalchemy/core.py index b56df8d..15b15ee 100644 --- a/stac_fastapi/sqlalchemy/core.py +++ b/stac_fastapi/sqlalchemy/core.py @@ -310,7 +310,7 @@ def get_search( if link["body"] and link["merge"]: query_params.update(link["body"]) link["method"] = "GET" - link["href"] = f"{link['body']}?{urlencode(query_params)}" + link["href"] = f"{link['href']}?{urlencode(query_params)}" link["body"] = None link["merge"] = False page_links.append(link) @@ -422,9 +422,9 @@ def post_search( # Query fields if search_request.query: - for (field_name, expr) in search_request.query.items(): + for field_name, expr in search_request.query.items(): field = self.item_table.get_field(field_name) - for (op, value) in expr.items(): + for op, value in expr.items(): if op == Operator.gte: query = query.filter(operator.ge(field, value)) elif op == Operator.lte: diff --git a/tests/resources/test_item.py b/tests/resources/test_item.py index 65484a0..27ecfc0 100644 --- a/tests/resources/test_item.py +++ b/tests/resources/test_item.py @@ -685,6 +685,34 @@ def test_item_search_get_query_extension(app_client, load_test_data): ) +def test_item_search_pagination(app_client, load_test_data): + """Test format of pagination links on a GET search""" + test_item = load_test_data("test_item.json") + for x in range(20): + test_item["id"] = f"test_item_{x}" + resp = app_client.post( + f"/collections/{test_item['collection']}/items", json=test_item + ) + assert resp.status_code == 200 + + params = {"limit": 5} + resp = app_client.get("/search", params=params) + assert resp.status_code == 200 + + resp_json = resp.json() + links = resp_json["links"] + next_link = next(link for link in links if link["rel"] == "next") + assert next_link["href"].startswith("http://testserver/search?") + + resp = app_client.get(links[0]["href"]) + resp_json = resp.json() + links = resp_json["links"] + next_link = next(link for link in links if link["rel"] == "next") + prev_link = next(link for link in links if link["rel"] == "previous") + assert next_link["href"].startswith("http://testserver/search?") + assert prev_link["href"].startswith("http://testserver/search?") + + def test_get_missing_item_collection(app_client): """Test reading a collection which does not exist""" resp = app_client.get("/collections/invalid-collection/items")