From 8de32f5ab3c022d1771b012847193e2142da07cb Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 19 Dec 2023 15:27:10 +0100 Subject: [PATCH] fix `prev` offset --- CHANGES.md | 7 ++++++- tests/routes/test_items.py | 25 +++++++++++++++++++++++++ tipg/collections.py | 4 +++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 12cff2d8..a0d0fae8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) @@ -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 diff --git a/tests/routes/test_items.py b/tests/routes/test_items.py index 7f9876a1..e8b72a06 100644 --- a/tests/routes/test_items.py +++ b/tests/routes/test_items.py @@ -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.""" diff --git a/tipg/collections.py b/tipg/collections.py index 711364a2..1164ca1b 100644 --- a/tipg/collections.py +++ b/tipg/collections.py @@ -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): @@ -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(