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

Fix bad href substitution on pagination usage #14

Merged
merged 5 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
26 changes: 26 additions & 0 deletions tests/resources/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,32 @@ 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"]
assert links[0]["rel"] == "next"
assert links[0]["href"].startswith("http://testserver/search?")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't rely on link ordering, we should find the link by rel. E.g.

Suggested change
resp_json = resp.json()
links = resp_json["links"]
assert links[0]["rel"] == "next"
assert links[0]["href"].startswith("http://testserver/search?")
resp_json = resp.json()
link = next(l for l in resp_json["links"] if l["rel"] == "next)
assert link["href"].startswith("http://testserver/search?")


resp = app_client.get(links[0]["href"])
resp_json = resp.json()
links = resp_json["links"]
assert links[0]["rel"] == "next"
assert links[1]["rel"] == "previous"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, we should not rely on link ordering, but instead fetch by rel.



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