Skip to content

Commit

Permalink
feat: add sorting on GET /prices (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Dec 18, 2023
1 parent 2156690 commit 7139ec9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
1 change: 1 addition & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 18 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 7139ec9

Please sign in to comment.