Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
from sqlmodel import SQLModel
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from httpx import AsyncClient, ASGITransport
|
||||
from app.main import app
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def engine():
|
||||
"""Create a single in-memory SQLite engine for the entire test session."""
|
||||
engine = create_async_engine(
|
||||
"sqlite+aiosqlite:///:memory:",
|
||||
echo=False,
|
||||
)
|
||||
# Create tables once for the whole session to avoid overhead
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(SQLModel.metadata.create_all)
|
||||
yield engine
|
||||
await engine.dispose()
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def session_factory(engine):
|
||||
"""Create async session factory."""
|
||||
return sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def db_session(session_factory):
|
||||
"""Create a database session for a test. Rolls back after each test."""
|
||||
async with session_factory() as session:
|
||||
yield session
|
||||
await session.rollback()
|
||||
|
||||
@pytest.fixture
|
||||
def sample_session_data():
|
||||
"""Sample sleep session data for testing."""
|
||||
return {
|
||||
"start_time": "2024-01-15T21:30:00Z",
|
||||
}
|
||||
|
||||
@pytest.fixture
|
||||
async def api_client(engine):
|
||||
"""Provides an AsyncClient for testing API endpoints."""
|
||||
async with AsyncClient(
|
||||
transport=ASGITransport(app=app), base_url="http://test"
|
||||
) as client:
|
||||
yield client
|
||||
Reference in New Issue
Block a user