hubmc-datagw/tests/conftest.py

47 lines
1.1 KiB
Python
Raw Normal View History

2025-10-11 00:14:16 +02:00
"""Pytest configuration and fixtures."""
import pytest
2025-11-09 00:23:31 +01:00
import pytest_asyncio
2025-10-11 00:14:16 +02:00
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
2025-11-09 00:23:31 +01:00
@pytest_asyncio.fixture
2025-10-11 00:14:16 +02:00
async def test_db():
"""Create test database engine."""
settings = AppSettings()
settings.DB_DSN = "postgresql+asyncpg://test:test@localhost:5432/hubgw_test"
2025-11-09 00:23:31 +01:00
2025-10-11 00:14:16 +02:00
engine = create_async_engine(settings.DB_DSN)
session_factory = async_sessionmaker(engine, expire_on_commit=False)
2025-11-09 00:23:31 +01:00
2025-10-11 00:14:16 +02:00
yield engine, session_factory
2025-11-09 00:23:31 +01:00
2025-10-11 00:14:16 +02:00
await engine.dispose()
2025-11-09 00:23:31 +01:00
@pytest_asyncio.fixture
2025-10-11 00:14:16 +02:00
async def test_session(test_db):
"""Create test database session."""
engine, session_factory = test_db
2025-11-09 00:23:31 +01:00
2025-10-11 00:14:16 +02:00
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()