Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sorting on GET /prices #90

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def get_prices_query(filters: PriceFilter | None = None):
query = select(Price)
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 @@ -213,6 +213,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 @@ -252,7 +256,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
Loading