Files
fastAPI/tests/test_attachment_api.py
2026-08-12 13:42:53 +08:00

87 lines
2.6 KiB
Python

from fastapi.testclient import TestClient
from sqlalchemy.orm import Session
from app.models.attachment import AttachmentCategory
def _seed_categories(db: Session) -> list[int]:
cats = [
AttachmentCategory(name="TEST_CERT", sort_order=1, is_active=True),
AttachmentCategory(name="TEST_EXTRA", sort_order=2, is_active=True),
AttachmentCategory(name="TEST_OFF", sort_order=3, is_active=False),
]
for c in cats:
db.add(c)
db.commit()
for c in cats:
db.refresh(c)
return [c.id for c in cats]
def test_categories_lists_active_only(client: TestClient, db: Session):
_seed_categories(db)
resp = client.get("/CargoTrace/attachment/categories")
assert resp.status_code == 200
names = [c["name"] for c in resp.json()]
assert "TEST_CERT" in names
assert "TEST_EXTRA" in names
assert "TEST_OFF" not in names
# sorted by sort_order then name
assert names.index("TEST_CERT") < names.index("TEST_EXTRA")
def test_status_unknown_zongpai_is_undetermined(client: TestClient):
resp = client.get(
"/CargoTrace/attachment/status", params={"zongpai_no": "26BW0011"}
)
assert resp.status_code == 200
data = resp.json()
assert data["zongpai_no"] == "26BW0011"
assert data["determination"] == "undetermined"
assert data["items"] == []
assert data["all_complete"] is False
def test_status_invalid_zongpai(client: TestClient):
resp = client.get(
"/CargoTrace/attachment/status", params={"zongpai_no": "BAD"}
)
assert resp.status_code == 400
assert resp.json()["error_code"] == "INVALID_ZONGPAI"
def test_status_reflects_config_and_boxed(client: TestClient, db: Session):
cat_ids = _seed_categories(db)
cert_id = cat_ids[0]
# Plan: 检验证书 expected=80
client.put(
"/CargoTrace/attachment/config",
json={
"zongpai_no": "26BW0011",
"determination": "has",
"items": [{"category_id": cert_id, "expected_qty": 80}],
},
)
# Box 30 of them
client.post(
"/CargoTrace/attachment/box",
json={
"zongpai_no": "26BW0011",
"category_id": cert_id,
"box_no": 921,
"quantity": 30,
},
)
resp = client.get(
"/CargoTrace/attachment/status", params={"zongpai_no": "26BW0011"}
)
assert resp.status_code == 200
data = resp.json()
assert data["determination"] == "has"
assert data["all_complete"] is False
item = data["items"][0]
assert item["category_id"] == cert_id
assert item["expected_qty"] == 80
assert item["boxed_qty"] == 30
assert item["complete"] is False