test: add integration tests for location API
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
29
tests/conftest.py
Normal file
29
tests/conftest.py
Normal file
@@ -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()
|
||||||
57
tests/test_location_api.py
Normal file
57
tests/test_location_api.py
Normal file
@@ -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"
|
||||||
Reference in New Issue
Block a user