feat(tests): pytest-asyncio test suite — 119 tests across 3 modules
Infrastructure (T01):
- tests/conftest.py: sync schema setup (psycopg2), per-test table
truncation, async ASGI client with get_session override
- pyproject.toml: [tool.pytest.ini_options] asyncio_mode=auto
- Makefile: make test target with TEST_DATABASE_URL
Core router tests (T02): 19 tests
- domains, topics, workstreams, tasks, decisions + state summary
- Caught real bug: topic router missing duplicate-slug 409 guard (fixed)
TD/EP/Contributions/SBOM tests (T03): 10 tests
- CRUD + status transitions + lifecycle guard + SBOM ingest
MCP smoke tests (T04): 12 tests
- get_state_summary, create_task, update_task_status,
add_progress_event, flag_for_human HTTP shapes
CI gate (T05): make test documented in CLAUDE.md session protocol
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 12:00:06 +01:00
|
|
|
"""
|
|
|
|
|
Shared pytest fixtures for the state-hub API test suite.
|
|
|
|
|
|
|
|
|
|
Uses a real PostgreSQL test database (custodian_test) — never mocked.
|
|
|
|
|
Set TEST_DATABASE_URL to override the default.
|
|
|
|
|
|
|
|
|
|
Schema is created/dropped once per session via psycopg2 (synchronous) to
|
|
|
|
|
avoid asyncpg "another operation is in progress" errors during create_all.
|
|
|
|
|
Tables are truncated between tests for isolation.
|
|
|
|
|
"""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
import pytest_asyncio
|
|
|
|
|
import sqlalchemy
|
|
|
|
|
from httpx import ASGITransport, AsyncClient
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
|
|
|
|
|
|
|
|
# Make api/ importable when running pytest from state-hub/
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
|
|
|
|
|
|
_ASYNC_URL = os.getenv(
|
|
|
|
|
"TEST_DATABASE_URL",
|
|
|
|
|
"postgresql+asyncpg://custodian:changeme@127.0.0.1:5432/custodian_test",
|
|
|
|
|
)
|
|
|
|
|
_SYNC_URL = _ASYNC_URL.replace("+asyncpg", "+psycopg2")
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Schema lifecycle (synchronous — avoids asyncpg concurrent-query errors)
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
|
def _schema():
|
|
|
|
|
"""Create all tables before the session; drop them after."""
|
|
|
|
|
# Import all models so metadata is fully populated
|
|
|
|
|
from api.models import Base # noqa: F401
|
|
|
|
|
|
|
|
|
|
engine = sqlalchemy.create_engine(_SYNC_URL)
|
|
|
|
|
Base.metadata.create_all(engine)
|
|
|
|
|
yield
|
|
|
|
|
Base.metadata.drop_all(engine)
|
|
|
|
|
engine.dispose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def _truncate(_schema):
|
|
|
|
|
"""Truncate all tables after each test for isolation."""
|
|
|
|
|
from api.models import Base
|
perf(api): CUST-WP-0041 — DB indexes, TTL caches, noload on list endpoints
- Migration t7o8p9q0r1s2: indexes on tasks.status, tasks(workstream_id,status),
workstreams.status, sbom_snapshots(repo_id,snapshot_at)
- workplan-index: 30 s TTL cache + ?refresh param (4171 ms → 16 ms on hit)
- /state/summary: 15 s TTL cache, bypassed on Cache-Control: no-cache
- /topics/: noload(workstreams, decisions, progress_events) (2382 ms → 115 ms)
- /domains/: noload(topics, repos, goals) (2252 ms → 39 ms)
- /repos/: noload(goals) (2222 ms → 599 ms first / fast on repeat)
- conftest: reset TTL caches between tests to prevent bleed-through
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 11:12:17 +02:00
|
|
|
import api.routers.state as _state_router
|
|
|
|
|
import api.routers.workstreams as _ws_router
|
2026-06-22 16:27:32 +02:00
|
|
|
from api.services.summary_cache import reset_summary_cache_for_tests
|
perf(api): CUST-WP-0041 — DB indexes, TTL caches, noload on list endpoints
- Migration t7o8p9q0r1s2: indexes on tasks.status, tasks(workstream_id,status),
workstreams.status, sbom_snapshots(repo_id,snapshot_at)
- workplan-index: 30 s TTL cache + ?refresh param (4171 ms → 16 ms on hit)
- /state/summary: 15 s TTL cache, bypassed on Cache-Control: no-cache
- /topics/: noload(workstreams, decisions, progress_events) (2382 ms → 115 ms)
- /domains/: noload(topics, repos, goals) (2252 ms → 39 ms)
- /repos/: noload(goals) (2222 ms → 599 ms first / fast on repeat)
- conftest: reset TTL caches between tests to prevent bleed-through
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 11:12:17 +02:00
|
|
|
|
2026-06-22 16:27:32 +02:00
|
|
|
# Reset in-process caches so stale data from a previous test can't bleed through.
|
|
|
|
|
reset_summary_cache_for_tests()
|
2026-06-06 00:42:00 +02:00
|
|
|
_state_router._OVERVIEW_CACHE = None
|
|
|
|
|
_state_router._OVERVIEW_CACHE_AT = 0.0
|
perf(api): CUST-WP-0041 — DB indexes, TTL caches, noload on list endpoints
- Migration t7o8p9q0r1s2: indexes on tasks.status, tasks(workstream_id,status),
workstreams.status, sbom_snapshots(repo_id,snapshot_at)
- workplan-index: 30 s TTL cache + ?refresh param (4171 ms → 16 ms on hit)
- /state/summary: 15 s TTL cache, bypassed on Cache-Control: no-cache
- /topics/: noload(workstreams, decisions, progress_events) (2382 ms → 115 ms)
- /domains/: noload(topics, repos, goals) (2252 ms → 39 ms)
- /repos/: noload(goals) (2222 ms → 599 ms first / fast on repeat)
- conftest: reset TTL caches between tests to prevent bleed-through
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 11:12:17 +02:00
|
|
|
_ws_router._INDEX_CACHE = None
|
|
|
|
|
_ws_router._INDEX_CACHE_AT = 0.0
|
2026-06-06 00:42:00 +02:00
|
|
|
_ws_router._INDEX_REFRESH_TASK = None
|
|
|
|
|
_ws_router._INDEX_LAST_ERROR = None
|
feat(tests): pytest-asyncio test suite — 119 tests across 3 modules
Infrastructure (T01):
- tests/conftest.py: sync schema setup (psycopg2), per-test table
truncation, async ASGI client with get_session override
- pyproject.toml: [tool.pytest.ini_options] asyncio_mode=auto
- Makefile: make test target with TEST_DATABASE_URL
Core router tests (T02): 19 tests
- domains, topics, workstreams, tasks, decisions + state summary
- Caught real bug: topic router missing duplicate-slug 409 guard (fixed)
TD/EP/Contributions/SBOM tests (T03): 10 tests
- CRUD + status transitions + lifecycle guard + SBOM ingest
MCP smoke tests (T04): 12 tests
- get_state_summary, create_task, update_task_status,
add_progress_event, flag_for_human HTTP shapes
CI gate (T05): make test documented in CLAUDE.md session protocol
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 12:00:06 +01:00
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
engine = sqlalchemy.create_engine(_SYNC_URL)
|
|
|
|
|
with engine.begin() as conn:
|
|
|
|
|
for table in reversed(Base.metadata.sorted_tables):
|
|
|
|
|
conn.execute(table.delete())
|
|
|
|
|
engine.dispose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Async engine (function-scoped, shared via session factory)
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def test_engine(_schema):
|
|
|
|
|
engine = create_async_engine(_ASYNC_URL, echo=False)
|
|
|
|
|
yield engine
|
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# HTTP client
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
|
|
|
async def client(test_engine):
|
|
|
|
|
"""AsyncClient backed by the FastAPI app with get_session overridden."""
|
|
|
|
|
from api.database import get_session
|
|
|
|
|
from api.main import app
|
|
|
|
|
|
|
|
|
|
factory = async_sessionmaker(test_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
|
|
|
|
async def _override():
|
|
|
|
|
async with factory() as session:
|
|
|
|
|
yield session
|
|
|
|
|
|
2026-06-25 13:44:27 +02:00
|
|
|
from api.services import write_idempotency as _write_idempotency
|
|
|
|
|
|
|
|
|
|
old_session_factory = _write_idempotency.async_session_factory
|
|
|
|
|
_write_idempotency.async_session_factory = factory
|
feat(tests): pytest-asyncio test suite — 119 tests across 3 modules
Infrastructure (T01):
- tests/conftest.py: sync schema setup (psycopg2), per-test table
truncation, async ASGI client with get_session override
- pyproject.toml: [tool.pytest.ini_options] asyncio_mode=auto
- Makefile: make test target with TEST_DATABASE_URL
Core router tests (T02): 19 tests
- domains, topics, workstreams, tasks, decisions + state summary
- Caught real bug: topic router missing duplicate-slug 409 guard (fixed)
TD/EP/Contributions/SBOM tests (T03): 10 tests
- CRUD + status transitions + lifecycle guard + SBOM ingest
MCP smoke tests (T04): 12 tests
- get_state_summary, create_task, update_task_status,
add_progress_event, flag_for_human HTTP shapes
CI gate (T05): make test documented in CLAUDE.md session protocol
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-18 12:00:06 +01:00
|
|
|
app.dependency_overrides[get_session] = _override
|
2026-06-25 13:44:27 +02:00
|
|
|
try:
|
|
|
|
|
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
|
|
|
|
|
yield ac
|
|
|
|
|
finally:
|
|
|
|
|
app.dependency_overrides.clear()
|
|
|
|
|
_write_idempotency.async_session_factory = old_session_factory
|
2026-06-22 13:52:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
# Shared entity helpers (workplan-first; legacy workstream names retained)
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
async def create_test_domain(client, slug="infotech", name="Infotech"):
|
|
|
|
|
r = await client.post("/domains/", json={"slug": slug, "name": name})
|
|
|
|
|
assert r.status_code == 201, r.text
|
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_test_topic(client, domain_slug="infotech", slug="testtopic", title="Test Topic"):
|
|
|
|
|
r = await client.post("/topics/", json={
|
|
|
|
|
"slug": slug, "title": title, "domain": domain_slug,
|
|
|
|
|
})
|
|
|
|
|
assert r.status_code == 201, r.text
|
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_test_repo(client, domain_slug="infotech", slug="test-repo", **extra):
|
|
|
|
|
payload = {
|
|
|
|
|
"domain_slug": domain_slug,
|
|
|
|
|
"slug": slug,
|
|
|
|
|
"name": "Test Repo",
|
|
|
|
|
**extra,
|
|
|
|
|
}
|
|
|
|
|
r = await client.post("/repos/", json=payload)
|
|
|
|
|
assert r.status_code == 201, r.text
|
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_test_workplan(
|
|
|
|
|
client,
|
|
|
|
|
repo_id,
|
|
|
|
|
topic_id=None,
|
|
|
|
|
slug="test-wp",
|
|
|
|
|
title="Test Workplan",
|
|
|
|
|
status="active",
|
|
|
|
|
**extra,
|
|
|
|
|
):
|
|
|
|
|
payload = {"repo_id": repo_id, "slug": slug, "title": title, "status": status, **extra}
|
|
|
|
|
if topic_id is not None:
|
|
|
|
|
payload["topic_id"] = topic_id
|
|
|
|
|
r = await client.post("/workplans/", json=payload)
|
|
|
|
|
assert r.status_code == 201, r.text
|
|
|
|
|
return r.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def create_test_workstream(client, topic_id=None, repo_id=None, slug="test-wp", **kwargs):
|
|
|
|
|
"""Legacy helper name — creates a repo-anchored workplan."""
|
|
|
|
|
if repo_id is None:
|
|
|
|
|
domain = await create_test_domain(client)
|
|
|
|
|
if topic_id is None:
|
|
|
|
|
topic = await create_test_topic(client, domain_slug=domain["slug"])
|
|
|
|
|
topic_id = topic["id"]
|
|
|
|
|
repo = await create_test_repo(client, domain_slug=domain["slug"], slug=f"{slug}-repo")
|
|
|
|
|
repo_id = repo["id"]
|
|
|
|
|
return await create_test_workplan(client, repo_id=repo_id, topic_id=topic_id, slug=slug, **kwargs)
|