aee0f09db8
- Datafabrik: Dockerfile fix, agentorkestrering fungerar - Vision: Identify-modell, FAISS, OCR alla testade - API: Alla 7 integrationstester passerade - Upplösare: Entitetsupplösning verifierad
98 lines
2.6 KiB
Python
98 lines
2.6 KiB
Python
"""Pytest fixtures."""
|
|
import asyncio
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
import pytest_asyncio
|
|
from httpx import AsyncClient
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
|
|
from app.main import app
|
|
from app.database import get_db, Base
|
|
from app.models import User, Tenant, UserRole, UserStatus
|
|
from app.security import get_password_hash
|
|
|
|
TEST_DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/landvex_test"
|
|
|
|
engine = create_async_engine(TEST_DATABASE_URL, echo=False, future=True)
|
|
AsyncTestingSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def override_get_db():
|
|
async with AsyncTestingSessionLocal() as session:
|
|
yield session
|
|
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session")
|
|
def event_loop():
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest_asyncio.fixture(scope="session", autouse=True)
|
|
async def setup_database():
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def db_session() -> AsyncSession:
|
|
async with AsyncTestingSessionLocal() as session:
|
|
yield session
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client() -> AsyncClient:
|
|
async with AsyncClient(app=app, base_url="http://test") as ac:
|
|
yield ac
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def test_tenant(db_session: AsyncSession) -> Tenant:
|
|
tenant = Tenant(
|
|
id=uuid.uuid4(),
|
|
name="Test Kommun",
|
|
slug="test-kommun",
|
|
)
|
|
db_session.add(tenant)
|
|
await db_session.commit()
|
|
await db_session.refresh(tenant)
|
|
return tenant
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def test_user(db_session: AsyncSession, test_tenant: Tenant) -> User:
|
|
user = User(
|
|
id=uuid.uuid4(),
|
|
email="admin@landvex.se",
|
|
first_name="Admin",
|
|
last_name="User",
|
|
hashed_password=get_password_hash("password123"),
|
|
role=UserRole.ADMIN,
|
|
status=UserStatus.ACTIVE,
|
|
tenant_id=test_tenant.id,
|
|
)
|
|
db_session.add(user)
|
|
await db_session.commit()
|
|
await db_session.refresh(user)
|
|
return user
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def admin_token(client: AsyncClient, test_user: User) -> str:
|
|
response = await client.post("/api/v1/auth/login", json={
|
|
"email": test_user.email,
|
|
"password": "password123",
|
|
})
|
|
data = response.json()
|
|
return data["access_token"]
|