mirror of
https://github.com/lintsinghua/DeepAudit.git
synced 2026-11-22 05:07:12 +08:00
146 lines
3.6 KiB
Python
146 lines
3.6 KiB
Python
"""Pytest Configuration and Fixtures
|
|
Shared fixtures for all tests.
|
|
"""
|
|
import pytest
|
|
import asyncio
|
|
from typing import AsyncGenerator, Generator
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app
|
|
from app.config import settings
|
|
from db.session import get_db
|
|
from models.user import User, Base
|
|
|
|
|
|
# Test database URL
|
|
TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:"
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop() -> Generator:
|
|
"""Create event loop for async tests"""
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def test_db() -> AsyncGenerator[AsyncSession, None]:
|
|
"""Create test database session"""
|
|
# Create test engine
|
|
engine = create_async_engine(
|
|
TEST_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
|
|
# Create tables
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
# Create session
|
|
async_session = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
async with async_session() as session:
|
|
yield session
|
|
|
|
# Drop tables
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def client(test_db: AsyncSession) -> TestClient:
|
|
"""Create test client with test database"""
|
|
|
|
async def override_get_db():
|
|
yield test_db
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
async def test_user(test_db: AsyncSession) -> User:
|
|
"""Create test user"""
|
|
from core.security import get_password_hash
|
|
|
|
user = User(
|
|
email="test@example.com",
|
|
username="testuser",
|
|
full_name="Test User",
|
|
hashed_password=get_password_hash("testpassword"),
|
|
role="user",
|
|
is_active=True
|
|
)
|
|
|
|
test_db.add(user)
|
|
await test_db.commit()
|
|
await test_db.refresh(user)
|
|
|
|
return user
|
|
|
|
|
|
@pytest.fixture
|
|
async def test_admin(test_db: AsyncSession) -> User:
|
|
"""Create test admin user"""
|
|
from core.security import get_password_hash
|
|
|
|
admin = User(
|
|
email="admin@example.com",
|
|
username="admin",
|
|
full_name="Admin User",
|
|
hashed_password=get_password_hash("adminpassword"),
|
|
role="admin",
|
|
is_active=True
|
|
)
|
|
|
|
test_db.add(admin)
|
|
await test_db.commit()
|
|
await test_db.refresh(admin)
|
|
|
|
return admin
|
|
|
|
|
|
@pytest.fixture
|
|
def auth_headers(test_user: User) -> dict:
|
|
"""Create authentication headers for test user"""
|
|
from core.security import create_access_token
|
|
|
|
token = create_access_token({"sub": str(test_user.id)})
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def admin_headers(test_admin: User) -> dict:
|
|
"""Create authentication headers for admin user"""
|
|
from core.security import create_access_token
|
|
|
|
token = create_access_token({"sub": str(test_admin.id)})
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def mock_llm_response():
|
|
"""Mock LLM response"""
|
|
return {
|
|
"content": "This is a test response",
|
|
"usage": {
|
|
"prompt_tokens": 10,
|
|
"completion_tokens": 20,
|
|
"total_tokens": 30
|
|
}
|
|
}
|