- tests/unit/modules/test_repository.py:base_repository 在 SQLite 内存库的 CRUD 测试 - tests/integration/api/test_smoke.py:登录链路 / 只读接口 / 未授权拦截(标记 integration,默认跳过) - conftest 提供 sqlite_db(隔离 DB)与 app/client/admin_headers(集成)fixture - pytest.ini 默认跳过 integration,Makefile test-integration 可显式运行 - 修复 base_repository.get_by_id/get_by_ids 对字符串 ID 的兼容(URL 路径参数) - 示范 modules/system/level service 改用 level_repo,CRUD 全链路实测通过 Co-Authored-By: Claude <noreply@anthropic.com>
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
"""pytest 全局配置:确保项目根目录在 sys.path,并提供 SQLite 内存库 fixture。"""
|
|
import os
|
|
import sys
|
|
|
|
import pytest
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
if PROJECT_ROOT not in sys.path:
|
|
sys.path.insert(0, PROJECT_ROOT)
|
|
|
|
|
|
@pytest.fixture
|
|
def sqlite_db():
|
|
"""SQLite 内存库会话 fixture:隔离的 service/repository 测试环境。
|
|
|
|
将独立会话注入 core.database._db_ctx,测试结束后恢复并清理。
|
|
不触碰真实 MySQL/Redis。
|
|
"""
|
|
from core.database import Base, _db_ctx
|
|
|
|
engine = create_engine(
|
|
"sqlite://",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
Base.metadata.create_all(engine)
|
|
session = sessionmaker(bind=engine)()
|
|
|
|
token = _db_ctx.set(session)
|
|
try:
|
|
yield session
|
|
finally:
|
|
_db_ctx.reset(token)
|
|
session.close()
|
|
engine.dispose()
|