Skip to content

Commit bf12228

Browse files
committed
add tests and format
1 parent 6c3ebcc commit bf12228

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

tests/routes/test_items.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,3 +598,52 @@ def test_output_response_type(app):
598598
assert response.headers["content-type"] == "application/ndjson"
599599
body = response.text.splitlines()
600600
assert len(body) == 10
601+
602+
603+
def test_items_sortby(app):
604+
"""Test /items endpoint with sortby options."""
605+
response = app.get("/collections/public.landsat_wrs/items?limit=1")
606+
assert response.status_code == 200
607+
assert response.headers["content-type"] == "application/geo+json"
608+
body = response.json()
609+
assert body["features"][0]["properties"]["ogc_fid"] == 1
610+
assert body["numberMatched"] == 16269
611+
612+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=ogc_fid")
613+
assert response.status_code == 200
614+
assert response.headers["content-type"] == "application/geo+json"
615+
body = response.json()
616+
assert body["features"][0]["properties"]["ogc_fid"] == 1
617+
assert body["numberMatched"] == 16269
618+
619+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=row")
620+
assert response.status_code == 200
621+
body = response.json()
622+
assert body["features"][0]["properties"]["row"] == 1
623+
assert body["numberMatched"] == 16269
624+
625+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=+row")
626+
assert response.status_code == 200
627+
body = response.json()
628+
assert body["features"][0]["properties"]["row"] == 1
629+
630+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=-row")
631+
assert response.status_code == 200
632+
body = response.json()
633+
assert body["features"][0]["properties"]["row"] == 248
634+
635+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=-row,path")
636+
assert response.status_code == 200
637+
body = response.json()
638+
assert body["features"][0]["properties"]["row"] == 248
639+
assert body["features"][0]["properties"]["path"] == 1
640+
641+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=path,-row")
642+
assert response.status_code == 200
643+
body = response.json()
644+
assert body["features"][0]["properties"]["row"] == 248
645+
assert body["features"][0]["properties"]["path"] == 1
646+
647+
# Invalid column name
648+
response = app.get("/collections/public.landsat_wrs/items?limit=1&sortby=something")
649+
assert response.status_code == 404

tifeatures/dbmodel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def columns(self, properties: Optional[List[str]] = None) -> List[str]:
9494
"""Return table columns optionally filtered to only include columns from properties."""
9595
cols = [c.name for c in self.properties]
9696
if properties is not None:
97-
if self.id_column is not None and self.id_column not in properties:
97+
if self.id_column not in properties:
9898
properties.append(self.id_column)
9999

100100
geom_col = self.geometry_column()

tifeatures/dependencies.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ def filter_query(
243243
return None
244244

245245

246-
def sortby_query(sortby: Optional[str] = Query(None, description="Sort By Column")):
247-
"""Parse sortby parameter."""
246+
def sortby_query(
247+
sortby: Optional[str] = Query(
248+
None,
249+
description="Column Sort the items by Column (ascending (default) or descending).",
250+
)
251+
):
252+
"""Sortby dependency."""
248253
return sortby

tifeatures/layer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ def _datetime_filter_to_sql(self, interval: List[str], dt_name: str):
265265
def _sortby(self, sortby: Optional[str]):
266266
sorts = []
267267
if sortby:
268-
sortby = sortby.strip()
269-
for s in sortby.split(","):
268+
for s in sortby.strip().split(","):
270269
parts = re.match(
271270
"^(?P<direction>[+-]?)(?P<column>.*)$", s
272271
).groupdict() # type:ignore
272+
273273
direction = parts["direction"]
274274
column = parts["column"].strip()
275275
if self.get_column(column):
@@ -279,9 +279,10 @@ def _sortby(self, sortby: Optional[str]):
279279
sorts.append(logic.V(column))
280280
else:
281281
raise InvalidPropertyName(f"Property {column} does not exist.")
282+
282283
else:
283284
sorts.append(logic.V(self.id_column))
284-
print(sorts)
285+
285286
return clauses.OrderBy(*sorts)
286287

287288
def _features_query(

0 commit comments

Comments
 (0)