all repos — rastro @ 303f75c7f98bcf857e2ef49ec4c4489ce9eb333b

BitTorrent tracker!

tests/conftest.py (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
"""Shared pytest fixtures."""

from __future__ import annotations

import os
from collections.abc import AsyncGenerator
from pathlib import Path

import pytest
import pytest_asyncio
from httpx import ASGITransport, AsyncClient
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine

# Ensure test settings before app import
os.environ.setdefault("APP_NAME", "RastroTest")
os.environ.setdefault("RUN_SCHEDULER", "false")
os.environ.setdefault("ENABLE_IPV6", "false")

from app.config import Settings, get_settings
from app.db import reset_engine
from app.db.models import Base


@pytest.fixture
def settings(tmp_path: Path) -> Settings:
    get_settings.cache_clear()
    reset_engine()
    db = tmp_path / "test.db"
    s = Settings(
        APP_NAME="RastroTest",
        APP_DESCRIPTION="test",
        APP_VERSION="0.1.0",
        PUBLIC_URL="http://test.local",
        DATABASE_URL=f"sqlite+aiosqlite:///{db.as_posix()}",
        RUN_SCHEDULER=False,
        ENABLE_IPV6="false",
    )
    return s


@pytest_asyncio.fixture
async def session(settings: Settings) -> AsyncGenerator[AsyncSession]:
    engine = create_async_engine(settings.database_url, echo=False)
    async with engine.begin() as conn:
        await conn.run_sync(Base.metadata.create_all)
    factory = async_sessionmaker(engine, expire_on_commit=False)
    async with factory() as sess:
        yield sess
    await engine.dispose()


@pytest_asyncio.fixture
async def client(
    settings: Settings, monkeypatch: pytest.MonkeyPatch
) -> AsyncGenerator[AsyncClient]:
    get_settings.cache_clear()
    reset_engine()
    monkeypatch.setenv("DATABASE_URL", settings.database_url)
    monkeypatch.setenv("RUN_SCHEDULER", "false")
    monkeypatch.setenv("APP_NAME", "RastroTest")
    get_settings.cache_clear()

    from app.main import create_app

    application = create_app()
    transport = ASGITransport(app=application)
    async with AsyncClient(transport=transport, base_url="http://test") as ac:
        yield ac
    get_settings.cache_clear()
    reset_engine()