801e6a050b
- Documentation archivée et réorganisée - Backend: Ajout tests, migrations, library service, rate limiting - Frontend: Suppression Flutter, focus sur interface web HTML/JS - Tailwind CSS ajouté pour le style - Améliorations UX et corrections bugs Generated with [Claude Code](https://claude.com/claude-code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
115 lines
2.9 KiB
Python
115 lines
2.9 KiB
Python
"""Configuration for tests."""
|
|
import asyncio
|
|
import pytest
|
|
from typing import AsyncGenerator, Generator
|
|
from httpx import AsyncClient, ASGITransport
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from app.main import app
|
|
from app.core.database import get_db, Base
|
|
|
|
|
|
# Test database URL (SQLite in-memory)
|
|
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 db_engine():
|
|
"""Create test database engine."""
|
|
engine = create_async_engine(
|
|
TEST_DATABASE_URL,
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
yield engine
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.drop_all)
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def db_session(db_engine) -> AsyncGenerator[AsyncSession, None]:
|
|
"""Create test database session."""
|
|
async_session_maker = async_sessionmaker(
|
|
db_engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
)
|
|
|
|
async with async_session_maker() as session:
|
|
yield session
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
async def client(db_session: AsyncSession) -> AsyncGenerator[AsyncClient, None]:
|
|
"""Create test client."""
|
|
|
|
async def override_get_db():
|
|
yield db_session
|
|
|
|
app.dependency_overrides[get_db] = override_get_db
|
|
|
|
async with AsyncClient(
|
|
transport=ASGITransport(app=app), base_url="http://test"
|
|
) as ac:
|
|
yield ac
|
|
|
|
app.dependency_overrides.clear()
|
|
|
|
|
|
@pytest.fixture
|
|
async def auth_headers(client: AsyncClient) -> dict:
|
|
"""Create authenticated user and return headers."""
|
|
# Register a test user
|
|
register_response = await client.post(
|
|
"/api/v1/auth/register",
|
|
json={
|
|
"email": "test@example.com",
|
|
"username": "testuser",
|
|
"password": "testpass123",
|
|
},
|
|
)
|
|
|
|
assert register_response.status_code == 200
|
|
|
|
# Login to get token
|
|
login_response = await client.post(
|
|
"/api/v1/auth/login",
|
|
json={
|
|
"email": "test@example.com",
|
|
"password": "testpass123",
|
|
},
|
|
)
|
|
|
|
assert login_response.status_code == 200
|
|
data = login_response.json()
|
|
token = data["access_token"]
|
|
|
|
return {"Authorization": f"Bearer {token}"}
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_track_data():
|
|
"""Sample track data for testing."""
|
|
return {
|
|
"title": "Test Track",
|
|
"duration": 180,
|
|
"youtube_id": "test_youtube_id",
|
|
"image_url": "https://example.com/image.jpg",
|
|
}
|