bfb1fc9312
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import asyncio
|
|
import uuid
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
from httpx import ASGITransport, AsyncClient
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
|
|
from app.config import settings
|
|
from app.database import get_db
|
|
from app.main import app
|
|
from app.models import Base
|
|
from app.services.auth import create_access_token
|
|
|
|
TEST_DB_URL = settings.database_url.replace("/meetproto", "/meetproto_test")
|
|
|
|
engine_test = create_async_engine(TEST_DB_URL, echo=False)
|
|
async_session_test = async_sessionmaker(engine_test, expire_on_commit=False)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop():
|
|
loop = asyncio.new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest_asyncio.fixture(autouse=True)
|
|
async def setup_db():
|
|
async with engine_test.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield
|
|
async with engine_test.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
|
|
async def override_get_db():
|
|
async with async_session_test() as session:
|
|
yield session
|
|
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def client():
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://test") as c:
|
|
yield c
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def auth_client(client: AsyncClient):
|
|
resp = await client.post("/api/auth/register", json={
|
|
"username": f"testuser_{uuid.uuid4().hex[:8]}",
|
|
"email": f"test_{uuid.uuid4().hex[:8]}@example.com",
|
|
"password": "testpass123",
|
|
})
|
|
token = (await client.post("/api/auth/login", json={
|
|
"username": resp.json()["username"],
|
|
"password": "testpass123",
|
|
})).json()["access_token"]
|
|
client.headers["Authorization"] = f"Bearer {token}"
|
|
yield client
|