hubmc-datagw/tests/conftest.py

55 lines
1.3 KiB
Python

"""Pytest configuration and fixtures."""
import pytest
import asyncio
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
from fastapi.testclient import TestClient
from hubgw.main import create_app
from hubgw.context import AppContext
from hubgw.core.config import AppSettings
@pytest.fixture(scope="session")
def event_loop():
"""Create event loop for async tests."""
loop = asyncio.get_event_loop_policy().new_event_loop()
yield loop
loop.close()
@pytest.fixture
async def test_db():
"""Create test database engine."""
settings = AppSettings()
settings.DB_DSN = "postgresql+asyncpg://test:test@localhost:5432/hubgw_test"
engine = create_async_engine(settings.DB_DSN)
session_factory = async_sessionmaker(engine, expire_on_commit=False)
yield engine, session_factory
await engine.dispose()
@pytest.fixture
async def test_session(test_db):
"""Create test database session."""
engine, session_factory = test_db
async with session_factory() as session:
yield session
@pytest.fixture
def test_client():
"""Create test client."""
app = create_app()
return TestClient(app)
@pytest.fixture
def test_context():
"""Create test context."""
return AppContext()