Skip to content

Commit ba7a199

Browse files
keulgadomski
andauthored
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 <[email protected]> * Update tests/resources/test_item.py Co-authored-by: Pete Gadomski <[email protected]> --------- Co-authored-by: Pete Gadomski <[email protected]>
1 parent e93cfd3 commit ba7a199

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ As a part of this release, this repository was extracted from the main
99

1010
* Default branch to **main** ([#544](https://github.com/stac-utils/stac-fastapi/pull/544))
1111

12+
### Fixed
13+
14+
* Fix bad links generated by search for pagination ([#14](https://github.com/stac-utils/stac-fastapi-sqlalchemy/pull/14/files))
15+
1216
## [2.4.4] - 2023-03-09
1317

1418
### Added

stac_fastapi/sqlalchemy/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ def get_search(
310310
if link["body"] and link["merge"]:
311311
query_params.update(link["body"])
312312
link["method"] = "GET"
313-
link["href"] = f"{link['body']}?{urlencode(query_params)}"
313+
link["href"] = f"{link['href']}?{urlencode(query_params)}"
314314
link["body"] = None
315315
link["merge"] = False
316316
page_links.append(link)
@@ -422,9 +422,9 @@ def post_search(
422422

423423
# Query fields
424424
if search_request.query:
425-
for (field_name, expr) in search_request.query.items():
425+
for field_name, expr in search_request.query.items():
426426
field = self.item_table.get_field(field_name)
427-
for (op, value) in expr.items():
427+
for op, value in expr.items():
428428
if op == Operator.gte:
429429
query = query.filter(operator.ge(field, value))
430430
elif op == Operator.lte:

tests/resources/test_item.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,34 @@ def test_item_search_get_query_extension(app_client, load_test_data):
685685
)
686686

687687

688+
def test_item_search_pagination(app_client, load_test_data):
689+
"""Test format of pagination links on a GET search"""
690+
test_item = load_test_data("test_item.json")
691+
for x in range(20):
692+
test_item["id"] = f"test_item_{x}"
693+
resp = app_client.post(
694+
f"/collections/{test_item['collection']}/items", json=test_item
695+
)
696+
assert resp.status_code == 200
697+
698+
params = {"limit": 5}
699+
resp = app_client.get("/search", params=params)
700+
assert resp.status_code == 200
701+
702+
resp_json = resp.json()
703+
links = resp_json["links"]
704+
next_link = next(link for link in links if link["rel"] == "next")
705+
assert next_link["href"].startswith("http://testserver/search?")
706+
707+
resp = app_client.get(links[0]["href"])
708+
resp_json = resp.json()
709+
links = resp_json["links"]
710+
next_link = next(link for link in links if link["rel"] == "next")
711+
prev_link = next(link for link in links if link["rel"] == "previous")
712+
assert next_link["href"].startswith("http://testserver/search?")
713+
assert prev_link["href"].startswith("http://testserver/search?")
714+
715+
688716
def test_get_missing_item_collection(app_client):
689717
"""Test reading a collection which does not exist"""
690718
resp = app_client.get("/collections/invalid-collection/items")

0 commit comments

Comments
 (0)