0e6b3b1811
按 ROADMAP §3.1 治理批次推进的后端设计审查整改:
- 批次 0(安全):/api/status/{task_id} 补 JWT 鉴权与任务归属校验;
pythonocc_available 真实探测;bcrypt 超 72 字节显式拒绝;
SECRET_KEY/RUSTFS_* 惰性校验,代码侧弱默认移除
- 批次 1(部署正确性):主处理链路改走 RustFS(分派入参 stp_file_id 化,
worker 按 object_key 下载);AUTO_MIGRATE 开关 + 迁移目录 alembic/→migrations/
修复包遮蔽(自动迁移此前从未真正生效);OCC 镜像改 conda 原生执行 +
基础镜像 tag 锁定;compose 关键项改 ${VAR:?} 强制显式配置
- 批次 2(任务一致性):删除 Redis 进程内存回退,PG 为任务状态单一事实源;
批量元数据入库(processing_tasks.batch_id,迁移 a3f8c2d91e47);
型腔失败任务标 failed 不再静默 completed;事务边界收口
(数据本体写 flush-only、失败先回滚再置 failed、进度更新保留即时 commit)
- 批次 3(API 与代码结构):592 行 advanced_router 拆为 design/cost/machining/
export 四子路由,请求体全量 Pydantic 化;ROUTE_MODULES + route_registry
(/api/health 呈现 degraded,DEBUG fail fast);纯计算端点统一 to_thread;
StorageIntegrationService 按职责三拆;MAX_FILE_SIZE 接线生效、
celery 复用 Settings.redis_url;管理员重置密码改 JSON body(端到端断裂修复);
openapi.json 重导出(76 paths)+ 前端 gen:api
- 批次 4(架构演进):共享 ORM 按模块拆分(shared/models/base.py + identity.py、
moldinsight/models/、inventory/models/,删除三条无使用方的跨模块
relationship,跨模块桥接收敛为裸 FK 硬规则,无兼容 facade);
OCC executor 重建补 cancel_futures=True(消除旧队列被慢恢复线程
并行消化的数据竞争);OCC 吞吐方案设计先行
(docs/topics/performance/OCC_THROUGHPUT.md);顺手清偿 D15
(vite.config.ts 未用参数致 npm run build 失败)
测试基线:125 passed, 2 skipped(pytest + sqlite+aiosqlite;归属边界、
路由契约、配置治理、鉴权回归等随批新增)
文档同步:STATUS / TECH_DEBT / ROADMAP / ARCHITECTURE / API_CONTRACT /
OPERATIONS / AGENTS
Co-Authored-By: Claude Code <noreply@anthropic.com>
128 lines
4.9 KiB
Python
128 lines
4.9 KiB
Python
"""批次 2(D7)回归测试:批量任务聚合查询以 PG 为单一事实源。
|
||
|
||
覆盖:
|
||
- GET /api/batch/{batch_id} 按 ProcessingTask.batch_id 聚合(此前依赖 Redis batch key + 进程内存降级)
|
||
- 归属校验:他人批次 403、不存在 404、所有者 200 + 聚合数字正确
|
||
"""
|
||
import pytest
|
||
from fastapi import FastAPI
|
||
from httpx import AsyncClient, ASGITransport
|
||
from sqlalchemy.ext.asyncio import async_sessionmaker, AsyncSession
|
||
|
||
from moldinsight.api.batch_router import router as batch_router
|
||
from shared.database.database import get_db_session
|
||
from shared.models.identity import User
|
||
from moldinsight.models import STPFile, ProcessingTask
|
||
from shared.services.auth_service import get_current_active_user
|
||
|
||
|
||
@pytest.fixture(scope="function")
|
||
async def batch_client(async_engine, seeded_db):
|
||
"""带 batch_router 的测试应用:播种 batch-1(user 1,completed+processing)与 batch-2(user 999)。"""
|
||
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
||
|
||
async with session_factory() as session:
|
||
stp_a = STPFile(
|
||
id=9101, user_id=1, object_key="test/batch-a.step", storage_bucket="moldinsight",
|
||
original_filename="batch-a.step", file_size=128, status="completed",
|
||
)
|
||
stp_b = STPFile(
|
||
id=9102, user_id=1, object_key="test/batch-b.step", storage_bucket="moldinsight",
|
||
original_filename="batch-b.step", file_size=128, status="processing",
|
||
)
|
||
stp_c = STPFile(
|
||
id=9103, user_id=999, object_key="test/batch-c.step", storage_bucket="moldinsight",
|
||
original_filename="batch-c.step", file_size=128, status="completed",
|
||
)
|
||
task_a = ProcessingTask(
|
||
task_id="task-batch-a", stp_file_id=9101, batch_id="batch-1",
|
||
task_type="stp_parsing", status="completed", progress=100, parameters={},
|
||
)
|
||
task_b = ProcessingTask(
|
||
task_id="task-batch-b", stp_file_id=9102, batch_id="batch-1",
|
||
task_type="stp_parsing", status="processing", progress=40,
|
||
current_step="生成模具型腔", parameters={},
|
||
)
|
||
task_c = ProcessingTask(
|
||
task_id="task-batch-c", stp_file_id=9103, batch_id="batch-2",
|
||
task_type="stp_parsing", status="failed", progress=40,
|
||
error_message="处理超时", parameters={},
|
||
)
|
||
session.add_all([stp_a, stp_b, stp_c, task_a, task_b, task_c])
|
||
await session.commit()
|
||
|
||
test_app = FastAPI()
|
||
test_app.include_router(batch_router, prefix="/api")
|
||
|
||
async def override_get_db_session():
|
||
async with session_factory() as session:
|
||
yield session
|
||
|
||
test_app.dependency_overrides[get_db_session] = override_get_db_session
|
||
|
||
transport = ASGITransport(app=test_app)
|
||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||
yield ac, test_app
|
||
|
||
test_app.dependency_overrides.clear()
|
||
|
||
|
||
def _override_user(app: FastAPI, user_id: int):
|
||
app.dependency_overrides[get_current_active_user] = lambda: User(id=user_id, username="tester")
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_batch_status_owner_aggregates_from_pg(batch_client):
|
||
"""所有者查询:聚合数字与逐任务字段来自 PG,而非 Redis。"""
|
||
ac, app = batch_client
|
||
_override_user(app, 1)
|
||
|
||
resp = await ac.get("/api/batch/batch-1")
|
||
assert resp.status_code == 200
|
||
body = resp.json()
|
||
|
||
assert body["batch_id"] == "batch-1"
|
||
assert body["total"] == 2
|
||
assert body["completed"] == 1
|
||
assert body["processing"] == 1
|
||
assert body["failed"] == 0
|
||
assert body["progress_percent"] == 50.0
|
||
|
||
by_id = {t["task_id"]: t for t in body["tasks"]}
|
||
assert by_id["task-batch-a"]["status"] == "completed"
|
||
assert by_id["task-batch-a"]["filename"] == "batch-a.step"
|
||
assert by_id["task-batch-b"]["progress"] == 40
|
||
assert by_id["task-batch-b"]["current_step"] == "生成模具型腔"
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_batch_status_non_owner_is_403(batch_client):
|
||
"""他人批次必须 403(按 STPFile.user_id 校验,无主不等于公共)。"""
|
||
ac, app = batch_client
|
||
_override_user(app, 2)
|
||
|
||
resp = await ac.get("/api/batch/batch-1")
|
||
assert resp.status_code == 403
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_batch_status_unknown_is_404(batch_client):
|
||
ac, app = batch_client
|
||
_override_user(app, 1)
|
||
|
||
resp = await ac.get("/api/batch/batch-missing")
|
||
assert resp.status_code == 404
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_batch_status_includes_error_from_pg(batch_client):
|
||
"""failed 任务的 error_message 经 PG 返回(此前 error 只存在于 Redis task dict)。"""
|
||
ac, app = batch_client
|
||
_override_user(app, 999)
|
||
|
||
resp = await ac.get("/api/batch/batch-2")
|
||
assert resp.status_code == 200
|
||
body = resp.json()
|
||
assert body["failed"] == 1
|
||
assert body["tasks"][0]["error"] == "处理超时"
|