236 lines
7.2 KiB
Python
236 lines
7.2 KiB
Python
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 = "RS") -> None:
|
|
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 test_pensions_schedules_pagination_and_sorting():
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": 1,
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
client = TestClient(app)
|
|
|
|
file_no = f"PF-SCHED-{uuid.uuid4().hex[:8]}"
|
|
_seed_file(client, file_no)
|
|
|
|
# Create three schedules
|
|
client.post("/api/pensions/schedules", json={"file_no": file_no, "vests_on": "2024-01-01", "vests_at": 10})
|
|
client.post("/api/pensions/schedules", json={"file_no": file_no, "vests_on": "2024-02-01", "vests_at": 20})
|
|
client.post("/api/pensions/schedules", json={"file_no": file_no, "vests_on": "2024-03-01", "vests_at": 30})
|
|
|
|
# Sort by vests_on ascending
|
|
r = client.get("/api/pensions/schedules", params={
|
|
"file_no": file_no,
|
|
"sort_by": "vests_on",
|
|
"sort_dir": "asc",
|
|
})
|
|
assert r.status_code == 200
|
|
dates = [row.get("vests_on") for row in r.json()]
|
|
assert dates[:3] == ["2024-01-01", "2024-02-01", "2024-03-01"]
|
|
|
|
# Sort by vests_on descending
|
|
r = client.get("/api/pensions/schedules", params={
|
|
"file_no": file_no,
|
|
"sort_by": "vests_on",
|
|
"sort_dir": "desc",
|
|
})
|
|
assert r.status_code == 200
|
|
dates = [row.get("vests_on") for row in r.json()]
|
|
assert dates[:3] == ["2024-03-01", "2024-02-01", "2024-01-01"]
|
|
|
|
# Pagination with include_total
|
|
r = client.get("/api/pensions/schedules", params={
|
|
"file_no": file_no,
|
|
"sort_by": "vests_on",
|
|
"sort_dir": "asc",
|
|
"limit": 2,
|
|
"skip": 0,
|
|
"include_total": True,
|
|
})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert isinstance(body.get("items"), list)
|
|
assert body["total"] >= 3
|
|
assert [row.get("vests_on") for row in body["items"]] == ["2024-01-01", "2024-02-01"]
|
|
|
|
r = client.get("/api/pensions/schedules", params={
|
|
"file_no": file_no,
|
|
"sort_by": "vests_on",
|
|
"sort_dir": "asc",
|
|
"limit": 2,
|
|
"skip": 2,
|
|
})
|
|
assert r.status_code == 200
|
|
tail = r.json()
|
|
assert len(tail) >= 1
|
|
assert tail[0]["vests_on"] == "2024-03-01"
|
|
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def test_pensions_marriages_pagination_and_sorting():
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": 1,
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
client = TestClient(app)
|
|
|
|
file_no = f"PF-MARR-{uuid.uuid4().hex[:8]}"
|
|
_seed_file(client, file_no)
|
|
|
|
client.post("/api/pensions/marriages", json={
|
|
"file_no": file_no,
|
|
"married_from": "2000-01-01",
|
|
"married_to": "2005-01-01",
|
|
"marital_percent": 10,
|
|
})
|
|
client.post("/api/pensions/marriages", json={
|
|
"file_no": file_no,
|
|
"married_from": "2005-01-01",
|
|
"married_to": "2010-01-01",
|
|
"marital_percent": 20,
|
|
})
|
|
client.post("/api/pensions/marriages", json={
|
|
"file_no": file_no,
|
|
"married_from": "2010-01-01",
|
|
"married_to": "2015-01-01",
|
|
"marital_percent": 30,
|
|
})
|
|
|
|
# Sort by marital_percent desc
|
|
r = client.get("/api/pensions/marriages", params={
|
|
"file_no": file_no,
|
|
"sort_by": "marital_percent",
|
|
"sort_dir": "desc",
|
|
})
|
|
assert r.status_code == 200
|
|
percents = [row.get("marital_percent") for row in r.json()]
|
|
assert percents[:3] == [30, 20, 10]
|
|
|
|
# Pagination
|
|
r = client.get("/api/pensions/marriages", params={
|
|
"file_no": file_no,
|
|
"sort_by": "married_from",
|
|
"sort_dir": "asc",
|
|
"limit": 1,
|
|
"skip": 1,
|
|
"include_total": True,
|
|
})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] >= 3
|
|
assert body["items"][0]["married_from"] == "2005-01-01"
|
|
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def test_pensions_death_benefits_pagination_and_sorting():
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": 1,
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
client = TestClient(app)
|
|
|
|
file_no = f"PF-DEATH-{uuid.uuid4().hex[:8]}"
|
|
_seed_file(client, file_no)
|
|
|
|
client.post("/api/pensions/death-benefits", json={"file_no": file_no, "lump1": 100})
|
|
client.post("/api/pensions/death-benefits", json={"file_no": file_no, "lump1": 300})
|
|
client.post("/api/pensions/death-benefits", json={"file_no": file_no, "lump1": 200})
|
|
|
|
# Sort by lump1 desc
|
|
r = client.get("/api/pensions/death-benefits", params={
|
|
"file_no": file_no,
|
|
"sort_by": "lump1",
|
|
"sort_dir": "desc",
|
|
})
|
|
assert r.status_code == 200
|
|
l1s = [row.get("lump1") for row in r.json()]
|
|
assert l1s[:3] == [300, 200, 100]
|
|
|
|
# Pagination basic
|
|
r = client.get("/api/pensions/death-benefits", params={
|
|
"file_no": file_no,
|
|
"sort_by": "lump1",
|
|
"sort_dir": "asc",
|
|
"limit": 2,
|
|
"skip": 1,
|
|
})
|
|
assert r.status_code == 200
|
|
page = r.json()
|
|
assert [row.get("lump1") for row in page] == [200, 300]
|
|
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|
|
def test_pensions_separations_pagination_and_sorting():
|
|
app.dependency_overrides[get_current_user] = lambda: {
|
|
"id": 1,
|
|
"username": "tester",
|
|
"is_admin": True,
|
|
"is_active": True,
|
|
}
|
|
client = TestClient(app)
|
|
|
|
file_no = f"PF-SEP-{uuid.uuid4().hex[:8]}"
|
|
_seed_file(client, file_no)
|
|
|
|
client.post("/api/pensions/separations", json={"file_no": file_no, "agreement_date": "2024-01-01", "terms": "t1"})
|
|
client.post("/api/pensions/separations", json={"file_no": file_no, "agreement_date": "2024-02-01", "terms": "t2"})
|
|
client.post("/api/pensions/separations", json={"file_no": file_no, "agreement_date": "2024-03-01", "terms": "t3"})
|
|
|
|
# Sort by agreement_date desc
|
|
r = client.get("/api/pensions/separations", params={
|
|
"file_no": file_no,
|
|
"sort_by": "agreement_date",
|
|
"sort_dir": "desc",
|
|
})
|
|
assert r.status_code == 200
|
|
dates = [row.get("agreement_date") for row in r.json()]
|
|
assert dates[:3] == ["2024-03-01", "2024-02-01", "2024-01-01"]
|
|
|
|
# Pagination
|
|
r = client.get("/api/pensions/separations", params={
|
|
"file_no": file_no,
|
|
"sort_by": "agreement_date",
|
|
"sort_dir": "asc",
|
|
"limit": 1,
|
|
"skip": 2,
|
|
"include_total": True,
|
|
})
|
|
assert r.status_code == 200
|
|
body = r.json()
|
|
assert body["total"] >= 3
|
|
assert body["items"][0]["agreement_date"] == "2024-03-01"
|
|
|
|
app.dependency_overrides.pop(get_current_user, None)
|
|
|
|
|