Initial commit: InternalAuditInterprise
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
"""集成测试 fixture:连接本地 PostgreSQL 16,按事务隔离并回滚。
|
||||
|
||||
需要可连接的数据库(DATABASE_URL)。无法连接时跳过整组集成测试。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import OperationalError
|
||||
|
||||
from app.db import get_engine
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def db_available() -> bool:
|
||||
try:
|
||||
with get_engine().connect() as conn:
|
||||
conn.execute(text("SELECT 1"))
|
||||
return True
|
||||
except OperationalError:
|
||||
return False
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def session(db_available):
|
||||
if not db_available:
|
||||
pytest.skip("数据库不可用,跳过集成测试")
|
||||
engine = get_engine()
|
||||
connection = engine.connect()
|
||||
trans = connection.begin()
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
sess = Session(bind=connection)
|
||||
try:
|
||||
yield sess
|
||||
finally:
|
||||
sess.close()
|
||||
if trans.is_active:
|
||||
trans.rollback()
|
||||
connection.close()
|
||||
Reference in New Issue
Block a user