import pytest import pytest_asyncio from httpx import ASGITransport, AsyncClient from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from app.db.base import Base from app.db.session import get_db from app.main import app @pytest_asyncio.fixture(autouse=True) async def setup_test_db(): engine = create_async_engine("sqlite+aiosqlite:///:memory:") async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) session_factory = async_sessionmaker(engine, expire_on_commit=False) async def override_db(): async with session_factory() as s: yield s app.dependency_overrides[get_db] = override_db yield app.dependency_overrides.clear() await engine.dispose() @pytest_asyncio.fixture async def client(): async with AsyncClient( transport=ASGITransport(app=app), base_url="http://test" ) as c: yield c @pytest_asyncio.fixture async def auth_headers(client): r = await client.post( "/api/auth/register", json={ "email": "test@service.ro", "password": "testpass123", "tenant_name": "Test Service", "telefon": "0722000000", }, ) token = r.json()["access_token"] return {"Authorization": f"Bearer {token}"} @pytest_asyncio.fixture async def tenant_id(client): r = await client.post( "/api/auth/register", json={ "email": "tenant@service.ro", "password": "testpass123", "tenant_name": "Tenant Service", "telefon": "0722000001", }, ) return r.json()["tenant_id"]