Skip to content

Commit

Permalink
[#375] Update case test for year filter
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Nov 18, 2024
1 parent 6e5bbb5 commit 87679b4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
7 changes: 5 additions & 2 deletions backend/db/crud_case.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy.orm import Session
from sqlalchemy import and_
from sqlalchemy import and_, cast
from sqlalchemy.dialects.postgresql import TEXT
from typing import Optional, List
from typing_extensions import TypedDict
from fastapi import HTTPException, status
Expand Down Expand Up @@ -126,7 +127,9 @@ def get_all_case(
user_ids = [u.id for u in users]
case = case.filter(Case.created_by.in_(user_ids))
if year:
case = case.filter(Case.year.ilike("%{}%".format(year)))
case = case.filter(
cast(Case.year, TEXT).ilike("%{}%".format(f"{year}".strip()))
)
count = case.count()
case = case.order_by(Case.id.desc()).offset(skip).limit(limit).all()
return PaginatedCaseData(count=count, data=case)
Expand Down
47 changes: 47 additions & 0 deletions backend/tests/test_023_case_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,50 @@ async def test_get_case_filtered_by_email(
"total": 2,
"total_page": 1,
}

@pytest.mark.asyncio
async def test_get_case_filtered_by_year(
self, app: FastAPI, session: Session, client: AsyncClient
) -> None:
res = await client.get(
app.url_path_for("case:get_all"),
params={"year": "1992"},
headers={"Authorization": f"Bearer {admin_account.token}"},
)
assert res.status_code == 404
res = await client.get(
app.url_path_for("case:get_all"),
params={"year": "2023"},
headers={"Authorization": f"Bearer {admin_account.token}"},
)
assert res.status_code == 200
res = res.json()
assert res == {
"current": 1,
"data": [
{
"id": 2,
"name": "Bali Coffee Production (Private)",
"country": "Bali",
"focus_commodity": 1,
"diversified_commodities_count": 1,
"year": 2023,
"created_at": res["data"][0]["created_at"],
"created_by": "[email protected]",
"tags": [],
},
{
"id": 1,
"name": "Bali Rice and Corn Production",
"country": "Bali",
"focus_commodity": 2,
"diversified_commodities_count": 2,
"year": 2023,
"created_at": res["data"][1]["created_at"],
"created_by": "[email protected]",
"tags": [2, 1],
},
],
"total": 2,
"total_page": 1,
}

0 comments on commit 87679b4

Please sign in to comment.