From 6a4ea34e241b83e83cb1b8f7e042276c2733ce81 Mon Sep 17 00:00:00 2001 From: Misaka_Company Date: Mon, 11 May 2026 11:16:33 +0800 Subject: [PATCH] test: add integration tests for location API Co-Authored-By: Claude Opus 4.6 --- tests/__init__.py | 0 tests/conftest.py | 29 +++++++++++++++++++ tests/test_location_api.py | 57 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_location_api.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..873ecd8 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,29 @@ +import pytest +from fastapi.testclient import TestClient +from sqlalchemy.orm import Session + +from app.core.database import SessionLocal +from app.main import app +from app.models.finished_goods import FinishedGoodsLocation + + +@pytest.fixture +def client(): + return TestClient(app) + + +@pytest.fixture +def db(): + session = SessionLocal() + yield session + session.close() + + +@pytest.fixture(autouse=True) +def cleanup_test_data(db: Session): + yield + test_zongpai_nos = ["26B999", "26C999", "26BW0999", "26T999"] + db.query(FinishedGoodsLocation).filter( + FinishedGoodsLocation.zongpai_no.in_(test_zongpai_nos) + ).delete(synchronize_session=False) + db.commit() diff --git a/tests/test_location_api.py b/tests/test_location_api.py new file mode 100644 index 0000000..59d1e61 --- /dev/null +++ b/tests/test_location_api.py @@ -0,0 +1,57 @@ +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["detail"]["error_code"] == "DUPLICATE_LOCATION" + assert "existing_location" in data["detail"]["detail"] + + +def test_register_location_invalid_zongpai(client: TestClient): + """无效总排号 — 应返回 422""" + resp = client.post( + "/CargoTrace/location", + json={"zongpai_no": "INVALID", "location_code": "A01-02-03"}, + ) + assert resp.status_code == 422 + + +def test_register_location_invalid_location(client: TestClient): + """无效货位号 — 应返回 422""" + resp = client.post( + "/CargoTrace/location", + json={"zongpai_no": "26T999", "location_code": "bad-location"}, + ) + assert resp.status_code == 422 + + +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"