This commit is contained in:
2026-07-30 10:30:50 +08:00
parent cf6d708566
commit 853c478657
85 changed files with 4711 additions and 1052 deletions
+92 -65
View File
@@ -16,10 +16,13 @@ from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, Asyn
from sqlalchemy import select
from fastapi import FastAPI, APIRouter
from api.inventory import inventory_router
from models.database import Base, User, Customer, Warehouse, Supplier, Product, ProductMaterial, Inventory
from database.database import get_db_session
from services.auth_service import get_current_active_user
from inventory.api import inventory_router
from shared.models.database import (
Base, User, Customer, Warehouse, Supplier, Product, ProductMaterial,
Inventory, MaterialSupplier, SalesOrder, SalesOrderItem,
)
from shared.database.database import get_db_session
from shared.services.auth_service import get_current_active_user
@pytest.fixture(scope="session")
@@ -48,69 +51,93 @@ async def async_engine(sqlite_db_path):
@pytest.fixture(scope="function")
async def db_session(async_engine):
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
await session.rollback()
@pytest.fixture(scope="function")
async def seeded_db(db_session: AsyncSession):
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 = 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,
)
db_session.add_all([user, customer, supplier, warehouse, material, finished, bom, inv])
await db_session.commit()
return {"user": user, "customer": customer, "supplier": supplier, "warehouse": warehouse, "material": material, "finished": finished}
@pytest.fixture(scope="function")
@@ -132,7 +159,7 @@ async def client(async_engine, seeded_db):
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, lifespan="off")
transport = ASGITransport(app=test_app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac