71 lines
1.8 KiB
Python
71 lines
1.8 KiB
Python
import asyncio
|
|
from collections.abc import AsyncGenerator
|
|
|
|
import pytest
|
|
from httpx import ASGITransport, AsyncClient
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.asyncio import (
|
|
AsyncSession,
|
|
async_sessionmaker,
|
|
create_async_engine,
|
|
)
|
|
|
|
from proxy_pool.app import create_app
|
|
from proxy_pool.config import DatabaseSettings, Settings, RedisSettings
|
|
from proxy_pool.db.base import Base
|
|
|
|
|
|
@pytest.fixture
|
|
async def client() -> AsyncGenerator[AsyncClient, None]:
|
|
app = create_app()
|
|
async with app.router.lifespan_context(app):
|
|
transport = ASGITransport(app=app)
|
|
async with AsyncClient(
|
|
transport=transport,
|
|
base_url="http://test",
|
|
) as client:
|
|
yield client
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop():
|
|
loop = asyncio.new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_settings() -> Settings:
|
|
import os
|
|
return Settings(
|
|
secret_key=os.environ.get("SECRET_KEY", "test-secret"),
|
|
log_level=os.environ.get("LOG_LEVEL", "DEBUG"),
|
|
db=DatabaseSettings(
|
|
url=os.environ.get(
|
|
"DB_URL",
|
|
"postgresql+asyncpg://proxypool:proxypool@localhost:5432/proxypool",
|
|
),
|
|
),
|
|
redis=RedisSettings(
|
|
url=os.environ.get("REDIS_URL", "redis://localhost:6379/1"),
|
|
),
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
async def engine(test_settings: Settings):
|
|
eng = create_async_engine(test_settings.db.url)
|
|
yield eng
|
|
await eng.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
async def db_session(engine) -> AsyncGenerator[AsyncSession, None]:
|
|
async with engine.connect() as conn:
|
|
transaction = await conn.begin()
|
|
session = AsyncSession(bind=conn, expire_on_commit=False)
|
|
|
|
yield session
|
|
|
|
await session.close()
|
|
await transaction.rollback() |