"""All tests use disposable databases and a synthetic Telegram token.""" import os import tempfile import sys from pathlib import Path import pytest_asyncio _temporary = tempfile.TemporaryDirectory(prefix="lottery-tests-") os.environ["DATABASE_URL"] = os.getenv("TEST_DATABASE_URL") or ( "sqlite+aiosqlite:///" + (Path(_temporary.name) / "test.db").as_posix() ) os.environ["BOT_TOKEN"] = "123456:TEST_TOKEN_FOR_ISOLATED_TESTS" os.environ["ADMIN_IDS"] = "900001" os.environ["CASHIER_IDS"] = "900002" os.environ["REDIS_URL"] = os.getenv("TEST_REDIS_URL", "") from src.core.database import Base, engine, async_session_maker from src.core import models @pytest_asyncio.fixture(autouse=True) async def database(): if os.getenv("TEST_REDIS_URL"): from redis.asyncio import Redis redis = Redis.from_url(os.environ["TEST_REDIS_URL"]) # Only the synthetic bot's FSM namespace in the disposable test service. keys = [key async for key in redis.scan_iter(match="fsm:123456:*")] if keys: await redis.delete(*keys) await redis.aclose() async with engine.begin() as connection: await connection.run_sync(Base.metadata.drop_all) await connection.run_sync(Base.metadata.create_all) yield async_session_maker if os.getenv("TEST_REDIS_URL") and "main" in sys.modules: await sys.modules["main"].dp.storage.close() await engine.dispose()