import io from datetime import date import uuid from fastapi.testclient import TestClient from app.main import app from app.auth.security import get_current_user def _csv_file(name: str, text: str): return ("files", (name, io.BytesIO(text.encode("utf-8")), "text/csv")) def _seed_file(client: TestClient, file_no: str, owner_id: str = None) -> None: owner_id = owner_id or f"R{uuid.uuid4().hex[:6]}" rolodex_csv = f"Id,Last\n{owner_id},Alpha\n" files_csv = ( "File_No,Id,File_Type,Regarding,Opened,Empl_Num,Status,Rate_Per_Hour\n" f"{file_no},{owner_id},CIVIL,Test,{date.today():%Y-%m-%d},E01,ACTIVE,100\n" ) client.post("/api/import/batch-upload", files=[ _csv_file("ROLODEX.csv", rolodex_csv), _csv_file("FILES.csv", files_csv), ]) def _auth(): app.dependency_overrides[get_current_user] = lambda: { "id": 1, "username": "tester", "is_admin": True, "is_active": True, } def test_pension_detail_nested_collections_support_pagination_and_sorting(): _auth() client = TestClient(app) file_no = f"PF-DET-{uuid.uuid4().hex[:8]}" _seed_file(client, file_no) # Seed related rows client.post("/api/pensions/schedules", json={"file_no": file_no, "version": "01", "frequency": "Monthly", "vests_on": "2024-01-01", "vests_at": 10}) client.post("/api/pensions/schedules", json={"file_no": file_no, "version": "01", "frequency": "Monthly", "vests_on": "2024-02-01", "vests_at": 20}) client.post("/api/pensions/schedules", json={"file_no": file_no, "version": "01", "frequency": "Monthly", "vests_on": "2024-03-01", "vests_at": 30}) client.post("/api/pensions/marriages", json={"file_no": file_no, "version": "01", "married_from": "2001-01-01", "marital_percent": 10}) client.post("/api/pensions/marriages", json={"file_no": file_no, "version": "01", "married_from": "2002-01-01", "marital_percent": 20}) client.post("/api/pensions/death-benefits", json={"file_no": file_no, "version": "01", "lump1": 100}) client.post("/api/pensions/death-benefits", json={"file_no": file_no, "version": "01", "lump1": 200}) client.post("/api/pensions/separations", json={"file_no": file_no, "version": "01", "agreement_date": "2024-02-01", "terms": "X"}) client.post("/api/pensions/separations", json={"file_no": file_no, "version": "01", "agreement_date": "2024-03-01", "terms": "Y"}) # Call detail with pagination and sorting per section resp = client.get( f"/api/pensions/{file_no}/detail", params={ "s_sort_by": "vests_on", "s_sort_dir": "asc", "s_limit": 2, "s_skip": 1, "m_sort_by": "married_from", "m_sort_dir": "desc", "m_limit": 1, "d_sort_by": "lump1", "d_sort_dir": "desc", "sep_sort_by": "agreement_date", "sep_sort_dir": "asc", }, ) assert resp.status_code == 200 body = resp.json() # Validate schedules pagination sched = body["schedules"] assert isinstance(sched["items"], list) assert sched["total"] >= 3 # After skipping first (Jan), next two should start from Feb sched_dates = [row["vests_on"] for row in sched["items"]] assert sched_dates[0] == "2024-02-01" # Marriages sorted desc marr = body["marriages"] assert [row["married_from"] for row in marr["items"]][0] == "2002-01-01" # Death benefits sorted desc by lump1 deaths = body["death_benefits"] assert [row["lump1"] for row in deaths["items"]][:2] == [200, 100] # Separations sorted asc by date seps = body["separations"] assert [row["agreement_date"] for row in seps["items"]][:2] == ["2024-02-01", "2024-03-01"] app.dependency_overrides.pop(get_current_user, None)