Files
fastAPI/tests/test_location_api.py
2026-05-11 12:38:37 +08:00

61 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from fastapi.testclient import TestClient
def test_register_location_success(client: TestClient):
"""正常上架 — 应返回 200"""
resp = client.post(
"/CargoTrace/location",
json={"zongpai_no": "26B999", "location_code": "A01-02-03"},
)
assert resp.status_code == 200
data = resp.json()
assert data["zongpai_no"] == "26B999"
assert data["location_code"] == "A01-02-03"
def test_register_location_duplicate(client: TestClient):
"""重复上架 — 应返回 409包含统一错误格式"""
client.post(
"/CargoTrace/location",
json={"zongpai_no": "26C999", "location_code": "A01-02-03"},
)
resp = client.post(
"/CargoTrace/location",
json={"zongpai_no": "26C999", "location_code": "A02-01-01"},
)
assert resp.status_code == 409
data = resp.json()
assert data["error_code"] == "DUPLICATE_LOCATION"
assert data["location_code"] == "A01-02-03"
assert "registered_at" in data
def test_register_location_invalid_zongpai(client: TestClient):
"""无效总排号 — 应返回 400"""
resp = client.post(
"/CargoTrace/location",
json={"zongpai_no": "INVALID", "location_code": "A01-02-03"},
)
assert resp.status_code == 400
assert resp.json()["error_code"] == "INVALID_ZONGPAI"
def test_register_location_invalid_location(client: TestClient):
"""无效货位号 — 应返回 400"""
resp = client.post(
"/CargoTrace/location",
json={"zongpai_no": "26T999", "location_code": "bad-location"},
)
assert resp.status_code == 400
assert resp.json()["error_code"] == "INVALID_LOCATION"
def test_register_location_trans_location(client: TestClient):
"""转运特殊货位 — 应返回 200"""
resp = client.post(
"/CargoTrace/location",
json={"zongpai_no": "26BW0999", "location_code": "TRANS-01"},
)
assert resp.status_code == 200
assert resp.json()["location_code"] == "TRANS-01"