The Office (workspace D-64..D-72) needs a store for Notes, Tasks,
Decisions, Questions and Rejected options. D-68 puts it here, in
core-api's own database, behind its own auth.
Migration 005 lands the whole schema now, by hand (down_revision="004";
the chain has a hash-named revision between 003 and 004, confirmed
before trusting the numbering): book_records (kind, status validated
per kind in the service, scope work/personal, a generated tsvector
search column + GIN index), book_tags, book_record_links, book_attachments
(table only, no file handling yet), and book_events — append-only via a
database trigger that raises on UPDATE and DELETE for every role,
including the owning application.
Routes this round (D-70's core verbs), every one behind
require_caller("office", "user"), writes with identity_required=True,
every change writing a book_events row: add, show, list (the nine
views), find (full-text), change, close (task done/dropped; question
resolved via a `resolves` link into `settled`; decision superseded via
a `supersedes` link into `superseded`), link/unlink, tag/untag, history.
Append, remind, reschedule, undo, purge, redact, pin, check, snooze,
archive/trash/restore, pause/resume, due, next, batch calls and
idempotency keys are explicitly not this round — the schema already
carries what they need.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
121 lines
4.2 KiB
Python
121 lines
4.2 KiB
Python
"""
|
|
Shared test fixtures.
|
|
|
|
Database-backed tests need `TEST_DATABASE_URL` (core-api's gitignored .env).
|
|
They **skip** when it is unset and **refuse** (hard failure, not a skip)
|
|
when it points at `core_api` — that is production, and the dev `.env`'s
|
|
other `POSTGRES_*` values point there too, so this is the one guard that
|
|
stands between a test run and the production database.
|
|
|
|
Running the actual migration against the test database is done through the
|
|
`alembic` CLI in a subprocess rather than in-process, because
|
|
`alembic/env.py` always builds its connection URL from `get_settings()`
|
|
(composed from `POSTGRES_*`), with no override hook of its own — and
|
|
`get_settings()` is `lru_cache`d, so by the time a test runs, other test
|
|
modules have almost certainly already imported `src.main` and cached a
|
|
Settings instance pointed at production. A subprocess gets a clean
|
|
environment and a clean cache; overriding `POSTGRES_HOST`/`USER`/
|
|
`PASSWORD`/`DATABASE` in that child's environment, derived from
|
|
`TEST_DATABASE_URL`, is what actually redirects the migration.
|
|
"""
|
|
import os
|
|
import subprocess
|
|
from pathlib import Path
|
|
from urllib.parse import urlsplit
|
|
|
|
import pytest
|
|
from dotenv import load_dotenv
|
|
|
|
REPO_ROOT = Path(__file__).resolve().parent.parent
|
|
ALEMBIC_BIN = REPO_ROOT / ".venv" / "bin" / "alembic"
|
|
|
|
load_dotenv(REPO_ROOT / ".env")
|
|
|
|
|
|
def _components(url: str) -> dict:
|
|
"""POSTGRES_* overrides for a subprocess, derived from a database URL."""
|
|
parts = urlsplit(url)
|
|
host = parts.hostname or ""
|
|
if parts.port:
|
|
host = f"{host}:{parts.port}"
|
|
return {
|
|
"POSTGRES_HOST": host,
|
|
"POSTGRES_USER": parts.username or "",
|
|
"POSTGRES_PASSWORD": parts.password or "",
|
|
"POSTGRES_DATABASE": parts.path.lstrip("/"),
|
|
}
|
|
|
|
|
|
def run_alembic(url: str, *args: str) -> subprocess.CompletedProcess:
|
|
"""Run `alembic <args>` against `url`, never against the repo's own .env database."""
|
|
env = os.environ.copy()
|
|
env.update(_components(url))
|
|
result = subprocess.run(
|
|
[str(ALEMBIC_BIN), *args],
|
|
cwd=str(REPO_ROOT),
|
|
env=env,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def test_database_url() -> str:
|
|
"""The core_api_test database URL, or skip/refuse per the rules above."""
|
|
url = os.environ.get("TEST_DATABASE_URL")
|
|
if not url:
|
|
pytest.skip("TEST_DATABASE_URL is not set — database tests skipped")
|
|
|
|
db_name = urlsplit(url).path.lstrip("/")
|
|
if db_name == "core_api":
|
|
pytest.fail(
|
|
"TEST_DATABASE_URL resolves to 'core_api' (production) — refusing to use it for tests"
|
|
)
|
|
|
|
return url
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
def book_schema(test_database_url: str) -> str:
|
|
"""Ensure migration 005 (and everything before it) is applied to core_api_test."""
|
|
result = run_alembic(test_database_url, "upgrade", "head")
|
|
if result.returncode != 0:
|
|
pytest.fail(f"alembic upgrade head failed:\n{result.stdout}\n{result.stderr}")
|
|
return test_database_url
|
|
|
|
|
|
@pytest.fixture
|
|
async def book_session(book_schema: str):
|
|
"""An AsyncSession bound to core_api_test, rolled back after each test."""
|
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
|
from sqlalchemy.pool import NullPool
|
|
|
|
asyncpg_url = book_schema.replace("postgresql://", "postgresql+asyncpg://", 1)
|
|
engine = create_async_engine(asyncpg_url, poolclass=NullPool)
|
|
session_factory = async_sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
async with session_factory() as session:
|
|
yield session
|
|
await session.rollback()
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
@pytest.fixture
|
|
async def clean_book_tables(book_session):
|
|
"""
|
|
Book service methods commit internally (same pattern as the Dashboard
|
|
service), so `book_session`'s post-test rollback never runs against
|
|
anything — the data is already durable. TRUNCATE before each test
|
|
instead, which the append-only trigger does not block: it is a
|
|
row-level BEFORE UPDATE OR DELETE trigger, and TRUNCATE fires neither.
|
|
"""
|
|
from sqlalchemy import text
|
|
|
|
await book_session.execute(
|
|
text("TRUNCATE book_events, book_attachments, book_record_links, book_tags, book_records")
|
|
)
|
|
await book_session.commit()
|
|
yield book_session
|