Files
FabledScribe/tests/test_integration_db_maintenance.py
T
bvandeusenandClaude Fable 5 bbee0d0db1
CI & Build / Python lint (push) Successful in 3s
CI & Build / Plugin hooks (push) Successful in 8s
CI & Build / integration (push) Successful in 24s
CI & Build / TypeScript typecheck (push) Successful in 32s
CI & Build / Python tests (push) Successful in 54s
CI & Build / Build & push image (push) Successful in 18s
refactor(tests): one definition each for the copied fixtures and fakes (#2825, milestone 296 area 1)
The shape ledger showed the same test scaffolding defined over and over:
_bind_user x12 (byte-identical), _dispose_engine x10 in three wordings,
_no_supersession x3, _make_mock_session x7 in three subsets, a get-or-create
User helper x2 (+3 inlined), and fifteen hand-rolled MagicMock note factories
each re-explaining the same "an auto-MagicMock attribute is truthy" hazard
(note 2109).

Now: conftest.py carries _bind_user / _dispose_engine / _no_supersession as
opt-in fixtures (pytestmark = usefixtures(...) per module, so unit tests pay
nothing), and tests/helpers.py carries make_mock_session(), ensure_user() and
fake_note(**attrs) — the hazard documented once, real values on every
attribute the product reads. Call sites were rewritten by AST so titles with
dashes and commas survived; the three SimpleNamespace _note stand-ins that
only feed a single function stay local.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 11:03:48 -04:00

42 lines
1.6 KiB
Python

"""Real-Postgres integration tests for DB maintenance + health.
These run only in the CI integration lane (a real Postgres service + schema
built by `alembic upgrade head`). They exercise the actual async SQLAlchemy
connection path that unit mocks cannot: the un-awaited
`AsyncConnection.execution_options` regression (which made every VACUUM raise
AttributeError, reporting 0/6) passes the unit suite but fails here.
"""
import pytest
from scribe.services.db_maintenance import (
MAINTENANCE_TABLES,
get_table_health,
run_maintenance,
)
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures("_dispose_engine")]
@pytest.mark.asyncio
async def test_run_maintenance_vacuums_real_tables():
summary = await run_maintenance()
assert summary["tables"], "no tables were vacuumed"
# Every allowlisted table exists after `alembic upgrade head`, so every
# VACUUM (ANALYZE) must succeed. With the un-awaited execution_options bug
# they would ALL fail with AttributeError — this is the guard.
failed = [t for t in summary["tables"] if not t["ok"]]
assert not failed, f"VACUUM failed for: {failed}"
assert {t["table"] for t in summary["tables"]} <= set(MAINTENANCE_TABLES)
@pytest.mark.asyncio
async def test_table_health_reports_real_stats():
health = await get_table_health()
assert health["db_bytes"] > 0
names = {t["table"] for t in health["tables"]}
# Core table built by migrations must show up in pg_stat_user_tables.
assert "notes" in names
for t in health["tables"]:
assert t["dead_pct"] >= 0
assert t["total_bytes"] >= 0