Skip to content

Commit

Permalink
Fix bad href substitution on pagination usage (#14)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
keul and gadomski authored May 30, 2023
1 parent e93cfd3 commit ba7a199
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions stac_fastapi/sqlalchemy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
28 changes: 28 additions & 0 deletions tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit ba7a199

Please sign in to comment.