Spaces:
Running
Running
Download backend/tests/conftest.py from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 1.77 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/tests/conftest.py
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/backend/tests/conftest.py
-
curl -L -o conftest.py https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/backend/tests/conftest.py
1.77 kB
| """Test fixtures and configuration.""" | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlmodel import SQLModel | |
| from typing import AsyncGenerator | |
| from app.main import app | |
| from app.core.database import get_session | |
| from app.core.config import settings | |
| # Test database URL (use SQLite for tests) | |
| TEST_DATABASE_URL = "sqlite+aiosqlite:///:memory:" | |
| def test_app(): | |
| """Get test FastAPI application.""" | |
| return app | |
| def client(test_app): | |
| """Get test client.""" | |
| return TestClient(test_app) | |
| async def async_engine(): | |
| """Create async engine for tests.""" | |
| engine = create_async_engine( | |
| TEST_DATABASE_URL, | |
| echo=False, | |
| future=True, | |
| ) | |
| async with engine.begin() as conn: | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| yield engine | |
| async with engine.begin() as conn: | |
| await conn.run_sync(SQLModel.metadata.drop_all) | |
| await engine.dispose() | |
| async def async_session(async_engine) -> AsyncGenerator[AsyncSession, None]: | |
| """Get async session for tests.""" | |
| async_session_maker = sessionmaker( | |
| async_engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| ) | |
| async with async_session_maker() as session: | |
| yield session | |
| def override_get_session(async_session): | |
| """Override database session dependency.""" | |
| async def _override_get_session(): | |
| yield async_session | |
| app.dependency_overrides[get_session] = _override_get_session | |
| yield | |
| app.dependency_overrides.clear() | |