test: conftest 适配异步 Database(async URL + asyncio 初始化)
This commit is contained in:
+6
-4
@@ -2,19 +2,21 @@
|
||||
"""共享夹具:临时 SQLite 数据库 + TestClient(auth 与 rbac 测试共用)。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
from app.repositories import Database
|
||||
from app.infrastructure.repositories import Database
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client(tmp_path):
|
||||
"""把 app.state.db 换成指向临时 SQLite 的 Database,保证测试隔离。"""
|
||||
db = Database(db_url=f"sqlite:///{tmp_path}/test.db")
|
||||
db.initialize()
|
||||
db = Database(db_url=f"sqlite+aiosqlite:///{tmp_path}/test.db")
|
||||
asyncio.run(db.initialize())
|
||||
app.state.db = db
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
db.close()
|
||||
asyncio.run(db.close())
|
||||
|
||||
Reference in New Issue
Block a user