110 lines
4.7 KiB
Python
110 lines
4.7 KiB
Python
|
|
"""批次 3(D1)回归:advanced_router 拆分 + Pydantic 请求模型契约测试。
|
|||
|
|
|
|||
|
|
- 拆分后全部原端点路径保持不变(design / cost / machining / export 四个子路由)
|
|||
|
|
- 请求体校验统一 422(原 request.json() 手动解析的 400/静默默认值退役)
|
|||
|
|
- 纯 Python 计算端点(optimize-layout)经 to_thread 仍返回原响应形态
|
|||
|
|
"""
|
|||
|
|
import pytest
|
|||
|
|
from fastapi import FastAPI
|
|||
|
|
from httpx import AsyncClient, ASGITransport
|
|||
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
|
|||
|
|
|
|||
|
|
# design/export 路由的导入链含 processing_service / cad_exporter(OCC)
|
|||
|
|
pytest.importorskip("OCC")
|
|||
|
|
|
|||
|
|
from moldinsight.api.design_router import router as design_router
|
|||
|
|
from moldinsight.api.cost_router import router as cost_router
|
|||
|
|
from moldinsight.api.machining_router import router as machining_router
|
|||
|
|
from moldinsight.api.export_router import router as export_router
|
|||
|
|
from shared.database.database import get_db_session
|
|||
|
|
from shared.services.auth_service import get_current_active_user
|
|||
|
|
from shared.models.identity import User
|
|||
|
|
|
|||
|
|
EXPECTED_PATHS = {
|
|||
|
|
"/optimize-layout", "/design-cooling", "/design-gating", "/design-mold-system",
|
|||
|
|
"/detect-undercuts", "/cost-estimate", "/design-cam", "/check-collision",
|
|||
|
|
"/optimize-toolpath", "/design-electrodes", "/simulate-machining",
|
|||
|
|
"/export-mold", "/export-download/{filepath:path}", "/export-recommendations",
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def _build_app(async_engine):
|
|||
|
|
test_app = FastAPI()
|
|||
|
|
for r in (design_router, cost_router, machining_router, export_router):
|
|||
|
|
test_app.include_router(r)
|
|||
|
|
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|||
|
|
|
|||
|
|
async def override_get_db_session():
|
|||
|
|
async with session_factory() as session:
|
|||
|
|
yield session
|
|||
|
|
|
|||
|
|
# 生产 get_db_session 在依赖解析期即建连(is_connected→connect),
|
|||
|
|
# 裸测试应用必须覆写,否则任何带鉴权链的请求都会先连真实 PG
|
|||
|
|
test_app.dependency_overrides[get_db_session] = override_get_db_session
|
|||
|
|
return test_app
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.fixture
|
|||
|
|
async def api_client(async_engine):
|
|||
|
|
test_app = _build_app(async_engine)
|
|||
|
|
test_app.dependency_overrides[get_current_active_user] = lambda: User(id=1, username="tester")
|
|||
|
|
transport = ASGITransport(app=test_app)
|
|||
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|||
|
|
yield ac
|
|||
|
|
test_app.dependency_overrides.clear()
|
|||
|
|
|
|||
|
|
|
|||
|
|
def test_all_original_paths_registered():
|
|||
|
|
"""拆分不丢端点:原 advanced_router 的全部路径必须仍可注册。"""
|
|||
|
|
test_app = FastAPI()
|
|||
|
|
for r in (design_router, cost_router, machining_router, export_router):
|
|||
|
|
test_app.include_router(r)
|
|||
|
|
paths = {route.path for route in test_app.routes}
|
|||
|
|
missing = EXPECTED_PATHS - paths
|
|||
|
|
assert not missing, f"拆分后丢失端点: {missing}"
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_endpoints_require_auth(async_engine):
|
|||
|
|
"""拆分不得丢掉鉴权:未带 token 访问设计/导出端点必须 401。"""
|
|||
|
|
test_app = _build_app(async_engine) # 只覆写 db,不覆写鉴权
|
|||
|
|
transport = ASGITransport(app=test_app)
|
|||
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|||
|
|
for path in ("/optimize-layout", "/cost-estimate", "/export-mold", "/design-cam"):
|
|||
|
|
resp = await ac.post(path, json={})
|
|||
|
|
assert resp.status_code == 401, f"{path} 未鉴权: {resp.status_code}"
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_optimize_layout_rejects_invalid_cavity_count(api_client):
|
|||
|
|
"""原 400「1-64」校验迁移为 Pydantic 422。"""
|
|||
|
|
resp = await api_client.post("/optimize-layout", json={"cavity_count": 0})
|
|||
|
|
assert resp.status_code == 422
|
|||
|
|
resp = await api_client.post("/optimize-layout", json={"cavity_count": 65})
|
|||
|
|
assert resp.status_code == 422
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_task_id_endpoints_reject_missing_task_id(api_client):
|
|||
|
|
"""task_id 类端点缺参统一 422(原 detect-undercuts 400 / export-mold 404 语义收敛)。"""
|
|||
|
|
for path in ("/detect-undercuts", "/cost-estimate", "/export-mold"):
|
|||
|
|
resp = await api_client.post(path, json={})
|
|||
|
|
assert resp.status_code == 422, f"{path} 缺 task_id 未返回 422"
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_optimize_layout_default_body_succeeds(api_client):
|
|||
|
|
"""纯 Python 计算端点经 to_thread 正常返回原响应形态。"""
|
|||
|
|
resp = await api_client.post("/optimize-layout", json={"cavity_count": 4})
|
|||
|
|
assert resp.status_code == 200
|
|||
|
|
body = resp.json()
|
|||
|
|
assert body["status"] == "success"
|
|||
|
|
assert "data" in body
|
|||
|
|
|
|||
|
|
|
|||
|
|
@pytest.mark.asyncio
|
|||
|
|
async def test_simulate_machining_default_body_succeeds(api_client):
|
|||
|
|
resp = await api_client.post("/simulate-machining", json={})
|
|||
|
|
assert resp.status_code == 200
|
|||
|
|
assert resp.json()["status"] == "success"
|