fix(sp37-followup): load BACKFILL_SQL from migration file (no drift)

Followup #4 from the SP37 final-state tracker. The previous
BACKFILL_SQL constant in test_migration_0020.py was a hand-copied
duplicate of the migration's UPDATE statement. A future contributor
could edit one without the other and the test would silently
replay a different SQL than production — defeating the regression.

Fix: tests now load the migration file at test time and extract
its UPDATE via the same splitter db_migrate.run() uses (strip
'--' comments, split on ';'). The test can never disagree with
what production runs.

Changes:
  * test_migration_0020.py:
    - Remove the hand-copied BACKFILL_SQL constant
    - Add _migration_0020_path(), _extract_update_statements(),
      and _load_migration_0020_backfill_sql() helpers
    - Replace 3 BACKFILL_SQL references with helper calls
  * test_migration_0020_no_drift.py (new, 3 tests):
    - test_migration_0020_backfill_sql_uses_migration_file
      (asserts the extracted SQL targets the right column + path)
    - test_migration_0020_backfill_sql_is_non_empty_single_statement
    - test_migration_0020_has_exactly_one_update (guardrail against
      future contributors adding a second UPDATE — the extraction
      fails loudly so the test author can decide which is the
      backfill)

Tests: 43/43 pass in 1.46s (full SP37 followup chain).
Imports across test files match the existing pattern (test_store.py
imports from test_store_reconcile.py).
This commit is contained in:
Nora
2026-07-07 12:21:27 -06:00
parent abaf23c122
commit dc5bff617d
2 changed files with 116 additions and 13 deletions
@@ -0,0 +1,66 @@
"""SP37 follow-up #4: detect drift between test BACKFILL_SQL and migration SQL.
Followup #1 of the SP37 final-state tracker. The previous test file
maintained a hand-copied ``BACKFILL_SQL`` constant alongside the
migration file. If a future contributor edited one but not the other,
the test would silently replay a different SQL than production —
defeating the regression test.
The fix: ``test_migration_0020.py`` now reads the migration file at
test time and extracts its UPDATE. This file imports the helper and
pins the invariant — so a future contributor who edits the migration
automatically gets the new SQL replayed in tests, and a contributor
who removes the backfill UPDATE gets a loud extraction failure.
"""
from __future__ import annotations
from test_migration_0020 import (
_extract_update_statements,
_load_migration_0020_backfill_sql,
_migration_0020_path,
)
def test_migration_0020_backfill_sql_uses_migration_file():
"""The backfill SQL used in tests must come from the migration file.
Guards against the previous pattern of a hand-maintained
BACKFILL_SQL constant that could drift from the actual migration.
"""
sql = _load_migration_0020_backfill_sql()
# Sanity check: the SQL targets batches.transaction_set_control_number
# via json_extract on raw_result_json. If a contributor changes
# the column name or the JSON path, this assertion catches it.
assert "UPDATE batches" in sql
assert "SET transaction_set_control_number" in sql
assert "json_extract(raw_result_json" in sql
assert "$.envelope.transaction_set_control_number" in sql
def test_migration_0020_backfill_sql_is_non_empty_single_statement():
"""The helper returns a non-empty SQL string suitable for direct
execution via exec_driver_sql. The existing
``test_migration_0020.py`` tests use this helper to replay the
SQL against representative rows — so any successful replay
exercises the migration's actual UPDATE.
"""
sql = _load_migration_0020_backfill_sql()
assert sql # non-empty
assert ";" not in sql # already stripped by the splitter
def test_migration_0020_has_exactly_one_update():
"""Guardrail: if a future migration adds a second UPDATE, the
extraction fails loudly so the test author can decide which one
is the backfill.
"""
sql = _migration_0020_path().read_text()
updates = [
s for s in _extract_update_statements(sql)
if s.upper().startswith("UPDATE ")
]
assert len(updates) == 1, (
f"migration 0020 should have exactly 1 UPDATE (the backfill); "
f"found {len(updates)}. If you added a second UPDATE, update "
f"_load_migration_0020_backfill_sql() to pick the right one."
)