Skip to content

Commit

Permalink
Merge pull request #151 from developmentseed/patch/fix-previous-offset
Browse files Browse the repository at this point in the history
fix `prev` offset
  • Loading branch information
vincentsarago authored Dec 19, 2023
2 parents 3a7ed4f + 8de32f5 commit 50510a5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).

Note: Minor version `0.X.0` update might break the API, It's recommended to pin `tipg` to minor version: `tipg>=0.1,<0.2`

## [0.5.5] - 2023-12-19

- Fix `prev` offset value

## [0.5.4] - 2023-12-19

- Fix decimal error for streaming responses (author @RemcoMeeuwissen, https://github.com/developmentseed/tipg/pull/148)
Expand Down Expand Up @@ -247,7 +251,8 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

- Initial release

[unreleased]: https://github.com/developmentseed/tipg/compare/0.5.4...HEAD
[unreleased]: https://github.com/developmentseed/tipg/compare/0.5.5...HEAD
[0.5.5]: https://github.com/developmentseed/tipg/compare/0.5.4...0.5.5
[0.5.4]: https://github.com/developmentseed/tipg/compare/0.5.3...0.5.4
[0.5.3]: https://github.com/developmentseed/tipg/compare/0.5.2...0.5.3
[0.5.2]: https://github.com/developmentseed/tipg/compare/0.5.1...0.5.2
Expand Down
25 changes: 25 additions & 0 deletions tests/routes/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ def test_items_limit_and_offset(app):
response = app.get("/collections/public.landsat_wrs/items?limit=10001")
assert response.status_code == 422

response = app.get("/collections/public.landsat_wrs/items?limit=100&offset=100")
assert response.status_code == 200
assert response.headers["content-type"] == "application/geo+json"
body = response.json()
assert len(body["features"]) == 100
assert body["numberMatched"] == 16269
assert body["numberReturned"] == 100
# Next
assert "limit=100&offset=200" in body["links"][2]["href"]
# Prev
assert "limit=100&offset=0" in body["links"][3]["href"]

# offset + limit overflow the total number of items
response = app.get("/collections/public.landsat_wrs/items?limit=100&offset=16200")
assert response.status_code == 200
assert response.headers["content-type"] == "application/geo+json"
body = response.json()
assert len(body["features"]) == 69
assert body["numberMatched"] == 16269
assert body["numberReturned"] == 69
# No NEXT link
assert "next" not in [link["rel"] for link in body["links"]]
# Prev
assert "limit=100&offset=16100" in body["links"][2]["href"]


def test_items_bbox(app):
"""Test /items endpoint with bbox options."""
Expand Down
4 changes: 3 additions & 1 deletion tipg/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ async def features(
function_parameters: Optional[Dict[str, str]] = None,
) -> ItemList:
"""Build and run Pg query."""
limit = limit or features_settings.default_features_limit
offset = offset or 0

function_parameters = function_parameters or {}

if geom and geom.lower() != "none" and not self.get_geometry_column(geom):
Expand Down Expand Up @@ -792,7 +794,7 @@ async def features(
items=features,
matched=matched,
next=offset + returned if matched - returned > offset else None,
prev=max(offset - returned, 0) if offset else None,
prev=max(offset - limit, 0) if offset else None,
)

async def get_tile(
Expand Down

0 comments on commit 50510a5

Please sign in to comment.