44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import asyncio
|
|
from collections.abc import AsyncGenerator
|
|
|
|
import pytest
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
from proxy_pool.config import Settings
|
|
from proxy_pool.db.base import Base
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def event_loop():
|
|
loop = asyncio.new_event_loop()
|
|
yield loop
|
|
loop.close()
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_settings() -> Settings:
|
|
return Settings(
|
|
database_url="postgresql+asyncpg://proxypool:proxypool@localhost:5432/proxypool",
|
|
redis_url="redis://localhost:6379/1",
|
|
secret_key="test-secret",
|
|
log_level="DEBUG",
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
async def engine(test_settings: Settings):
|
|
engine = create_async_engine(test_settings.database_url)
|
|
yield engine
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
async def db_session(engine) -> AsyncGenerator[AsyncSession, None]:
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
session_factory = async_sessionmaker(engine, expire_on_commit=False)
|
|
async with session_factory() as session:
|
|
yield session
|
|
await session.rollback()
|