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>
167 lines
5.4 KiB
Python
167 lines
5.4 KiB
Python
import os
|
|
import sys
|
|
import tempfile
|
|
|
|
vendor_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "_vendor"))
|
|
if os.path.isdir(vendor_dir) and vendor_dir not in sys.path:
|
|
sys.path.insert(0, vendor_dir)
|
|
|
|
src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src"))
|
|
if os.path.isdir(src_dir) and src_dir not in sys.path:
|
|
sys.path.insert(0, src_dir)
|
|
|
|
import pytest
|
|
from httpx import AsyncClient, ASGITransport
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
from sqlalchemy import select
|
|
|
|
from fastapi import FastAPI, APIRouter
|
|
from inventory.api import inventory_router
|
|
from shared.models.base import Base
|
|
from shared.models.identity import User
|
|
from inventory.models import Customer, Warehouse, Supplier, Product, ProductMaterial, Inventory, MaterialSupplier, SalesOrder, SalesOrderItem
|
|
import moldinsight.models # noqa: F401 # 全量注册:create_all 需含 moldinsight 表(stp_files 等)
|
|
from shared.database.database import get_db_session
|
|
from shared.services.auth_service import get_current_active_user
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def anyio_backend():
|
|
return "asyncio"
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def sqlite_db_path():
|
|
fd, path = tempfile.mkstemp(prefix="gemold_test_", suffix=".db")
|
|
os.close(fd)
|
|
yield path
|
|
try:
|
|
os.remove(path)
|
|
except OSError:
|
|
pass
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
async def async_engine(sqlite_db_path):
|
|
engine = create_async_engine(f"sqlite+aiosqlite:///{sqlite_db_path}", future=True)
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield engine
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def seeded_db(async_engine):
|
|
"""每个测试前清空所有表并重新播种,确保隔离。"""
|
|
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
async with session_factory() as session:
|
|
# 按 FK 依赖逆序清空所有表
|
|
for table in reversed(Base.metadata.sorted_tables):
|
|
await session.execute(table.delete())
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
user = User(
|
|
id=1,
|
|
username="tester",
|
|
email="tester@example.com",
|
|
hashed_password="x",
|
|
full_name="Tester",
|
|
is_active=True,
|
|
)
|
|
customer = Customer(id=1, code="C001", name="客户A", is_active=True)
|
|
supplier = Supplier(id=1, code="S001", name="供应商A", is_active=True)
|
|
warehouse = Warehouse(id=1, code="W001", name="默认仓库", is_active=True, is_default=True)
|
|
|
|
material = Product(
|
|
id=1,
|
|
sku="MAT-001",
|
|
name="钢材",
|
|
unit="kg",
|
|
item_type="material",
|
|
cost_price=10.0,
|
|
sale_price=0,
|
|
min_stock=0,
|
|
max_stock=100000,
|
|
is_active=True,
|
|
)
|
|
finished = Product(
|
|
id=2,
|
|
sku="MOLD-STD",
|
|
name="标准模具",
|
|
unit="套",
|
|
item_type="finished",
|
|
cost_price=0,
|
|
sale_price=1000.0,
|
|
min_stock=0,
|
|
max_stock=0,
|
|
is_active=True,
|
|
)
|
|
# 成品无 BOM,用于测试 BOM 缺失路径
|
|
finished_no_bom = Product(
|
|
id=3,
|
|
sku="MOLD-NB",
|
|
name="无BOM成品",
|
|
unit="套",
|
|
item_type="finished",
|
|
cost_price=0,
|
|
sale_price=500.0,
|
|
min_stock=0,
|
|
max_stock=0,
|
|
is_active=True,
|
|
)
|
|
bom = ProductMaterial(
|
|
id=1,
|
|
finished_product_id=finished.id,
|
|
material_product_id=material.id,
|
|
quantity=2.0,
|
|
loss_rate=0.05,
|
|
)
|
|
inv = Inventory(
|
|
id=1,
|
|
product_id=material.id,
|
|
warehouse_id=warehouse.id,
|
|
quantity=1000,
|
|
locked_quantity=0,
|
|
)
|
|
# 物料-供应商关联(用于采购需求推导测试)
|
|
ms = MaterialSupplier(
|
|
id=1,
|
|
product_id=material.id,
|
|
supplier_id=supplier.id,
|
|
is_primary=True,
|
|
lead_time=7,
|
|
)
|
|
|
|
session.add_all([user, customer, supplier, warehouse, material, finished, finished_no_bom, bom, inv, ms])
|
|
await session.commit()
|
|
|
|
async with session_factory() as session:
|
|
yield session
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def client(async_engine, seeded_db):
|
|
session_factory = async_sessionmaker(async_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
test_app = FastAPI()
|
|
test_app.include_router(inventory_router)
|
|
|
|
async def override_get_db_session():
|
|
async with session_factory() as session:
|
|
yield session
|
|
|
|
async def override_get_current_active_user():
|
|
async with session_factory() as session:
|
|
result = await session.execute(select(User).where(User.username == "tester"))
|
|
return result.scalar_one()
|
|
|
|
test_app.dependency_overrides[get_db_session] = override_get_db_session
|
|
test_app.dependency_overrides[get_current_active_user] = override_get_current_active_user
|
|
|
|
transport = ASGITransport(app=test_app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
test_app.dependency_overrides.clear()
|