import os os.environ["WEBHOOK_SECRET"] = "test-secret" os.environ["DATABASE_URL"] = "sqlite+aiosqlite://" os.environ["APP_ENV"] = "test" import asyncio from typing import AsyncGenerator from unittest.mock import AsyncMock, MagicMock import pytest import pytest_asyncio from httpx import ASGITransport, AsyncClient from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from app.config import Settings, get_settings from app.database import get_db from app.models.models import Base get_settings.cache_clear() from app.main import app # noqa: E402 @pytest.fixture(scope="session") def event_loop(): loop = asyncio.new_event_loop() yield loop loop.close() @pytest_asyncio.fixture(scope="session") async def test_engine(): engine = create_async_engine("sqlite+aiosqlite://", echo=False) async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) yield engine await engine.dispose() @pytest_asyncio.fixture async def test_session(test_engine) -> AsyncGenerator[AsyncSession, None]: TestSession = async_sessionmaker(test_engine, expire_on_commit=False) async with TestSession() as session: yield session @pytest_asyncio.fixture async def client(test_session: AsyncSession) -> AsyncGenerator: async def override_get_db() -> AsyncGenerator[AsyncSession, None]: yield test_session app.dependency_overrides[get_db] = override_get_db transport = ASGITransport(app=app) async with AsyncClient(transport=transport, base_url="http://test") as ac: yield ac app.dependency_overrides.clear() @pytest.fixture def mock_max_api(): mock = AsyncMock() mock.send_message = AsyncMock(return_value={"ok": True}) mock.send_message_with_keyboard = AsyncMock(return_value={"ok": True}) mock.send_file = AsyncMock(return_value={"ok": True}) mock.get_file_url = AsyncMock(return_value="https://example.com/file") mock.send_contact_request = AsyncMock(return_value={"ok": True}) mock.answer_callback = AsyncMock(return_value={"ok": True}) mock.get_me = AsyncMock(return_value={"ok": True, "bot": "test"}) mock.subscribe_webhook = AsyncMock(return_value={"ok": True}) mock.verify_webhook_secret = MagicMock(return_value=True) mock.verify_contact_hash = MagicMock(return_value=True) return mock @pytest.fixture def bot(): return None