44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Set test env before importing app modules.
|
|
os.environ["POSTGRES_DSN"] = "sqlite+aiosqlite:///./test.db"
|
|
os.environ["AUTO_CREATE_TABLES"] = "false"
|
|
os.environ["REDIS_URL"] = "redis://localhost:6399/15"
|
|
os.environ["SECRET_KEY"] = "test-secret-key-1234567890"
|
|
os.environ["CELERY_TASK_ALWAYS_EAGER"] = "true"
|
|
PROJECT_ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(PROJECT_ROOT))
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
from app.database.base import Base
|
|
from app.database.session import AsyncSessionLocal, engine
|
|
from app.main import app
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
async def reset_db() -> None:
|
|
await engine.dispose()
|
|
test_db_path = PROJECT_ROOT / "test.db"
|
|
if test_db_path.exists():
|
|
test_db_path.unlink()
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
yield
|
|
|
|
|
|
@pytest.fixture
|
|
async def client() -> AsyncClient:
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(transport=transport, base_url="http://testserver") as ac:
|
|
yield ac
|
|
|
|
|
|
@pytest.fixture
|
|
async def db_session():
|
|
async with AsyncSessionLocal() as session:
|
|
yield session
|