签入版本
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""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()
|
||||
Reference in New Issue
Block a user