51 lines
1.8 KiB
Python
51 lines
1.8 KiB
Python
"""Test fixtures for the Temporal demo."""
|
|
|
|
from contextlib import AsyncExitStack
|
|
|
|
import pytest_asyncio
|
|
from httpx import ASGITransport, AsyncClient
|
|
from temporalio.testing import WorkflowEnvironment
|
|
|
|
from app.config.settings import get_settings
|
|
from app.infra.db.session import dispose_database, init_database
|
|
from app.infra.temporal.client import set_temporal_client
|
|
from app.main import create_app
|
|
from app.workers.runner import build_workers
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def api_runtime(tmp_path, monkeypatch):
|
|
"""Provide an API client and in-memory Temporal test environment."""
|
|
|
|
db_path = tmp_path / "test.db"
|
|
monkeypatch.setenv("DATABASE_URL", f"sqlite+aiosqlite:///{db_path.as_posix()}")
|
|
monkeypatch.setenv("AUTO_CREATE_TABLES", "true")
|
|
monkeypatch.setenv("S3_ACCESS_KEY", "test-access")
|
|
monkeypatch.setenv("S3_SECRET_KEY", "test-secret")
|
|
monkeypatch.setenv("S3_BUCKET", "test-bucket")
|
|
monkeypatch.setenv("S3_REGION", "ap-southeast-1")
|
|
monkeypatch.setenv("S3_ENDPOINT", "https://s3.example.com")
|
|
monkeypatch.setenv("S3_CNAME", "images.example.com")
|
|
monkeypatch.setenv("IMAGE_GENERATION_PROVIDER", "mock")
|
|
|
|
get_settings.cache_clear()
|
|
await dispose_database()
|
|
await init_database()
|
|
|
|
app = create_app()
|
|
|
|
async with await WorkflowEnvironment.start_time_skipping() as env:
|
|
set_temporal_client(env.client)
|
|
async with AsyncExitStack() as stack:
|
|
for worker in build_workers(env.client):
|
|
await stack.enter_async_context(worker)
|
|
async with AsyncClient(
|
|
transport=ASGITransport(app=app),
|
|
base_url="http://testserver",
|
|
) as client:
|
|
yield client, env
|
|
|
|
set_temporal_client(None)
|
|
await dispose_database()
|
|
get_settings.cache_clear()
|