From 7139ec950fd44d42d9d6e3a6816c579ca91a4dde Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Mon, 18 Dec 2023 16:00:12 +0100 Subject: [PATCH] feat: add sorting on GET /prices (#90) --- app/crud.py | 1 + app/schemas.py | 1 + tests/test_api.py | 20 ++++++++++++++++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/crud.py b/app/crud.py index 25cf6fe9..465b952d 100644 --- a/app/crud.py +++ b/app/crud.py @@ -116,6 +116,7 @@ def get_prices_query( query = query.options(joinedload(Price.location)) if filters: query = filters.filter(query) + query = filters.sort(query) return query diff --git a/app/schemas.py b/app/schemas.py index 7a96083f..e6c217ed 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -218,6 +218,7 @@ class PriceFilter(Filter): date__gte: Optional[str] | None = None date__lt: Optional[str] | None = None date__lte: Optional[str] | None = None + order_by: Optional[list[str]] | None = None class Constants(Filter.Constants): model = Price diff --git a/tests/test_api.py b/tests/test_api.py index 39fc7a9f..88d81d80 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -193,7 +193,11 @@ def test_create_price_code_category_exclusive_validation(user): assert response.status_code == 201 # only category_tag: ok PRICE_WITH_ONLY_CATEGORY = PRICE_1.model_copy( - update={"product_code": None, "category_tag": "en:tomatoes"} + update={ + "product_code": None, + "category_tag": "en:tomatoes", + "date": "2023-10-01", + } ) response = client.post( "/api/v1/prices", @@ -254,7 +258,19 @@ def test_get_prices_filters(): assert len(response.json()["items"]) == 0 response = client.get("/api/v1/prices?date=2023-10-31") assert response.status_code == 200 - assert len(response.json()["items"]) == 3 + assert len(response.json()["items"]) == 2 + + +def test_get_prices_orders(): + response = client.get("/api/v1/prices") + assert response.status_code == 200 + assert (response.json()["items"][0]["date"]) == "2023-10-31" + response = client.get("/api/v1/prices?order_by=date") # ASC + assert response.status_code == 200 + assert (response.json()["items"][0]["date"]) == "2023-10-01" + response = client.get("/api/v1/prices?order_by=-date") # DESC + assert response.status_code == 200 + assert (response.json()["items"][0]["date"]) == "2023-10-31" def test_get_proofs(user):