Files
gps-denied-onboard/tests/unit/test_ac5_alembic.py
T
Oleksandr Bezdieniezhnykh a06b107fc3 [AZ-320] Add C11 IdempotentRetryTileUploader decorator
Wraps HttpTileUploader (AZ-319) with two bounded retry budgets:

- In-call (per-batch) — re-invokes inner on PARTIAL outcome up to
  `max_in_call_retries` times with capped exponential backoff
  (`min(base ** attempt_number, cap)`). On exhaustion: surfaces an
  operator hint via `next_retry_at_s = now + backoff_cap_s`.
- Per-tile (cross-call) — atomically increments c6's
  `tiles.upload_attempts` counter for every rejection; once a tile
  hits `max_per_tile_attempts` it is forward-only transitioned to
  `voting_status = upload_giveup` (excluded from `pending_uploads`).
  Each transition emits FDR `kind="c11.upload.giveup"` plus an
  ERROR log.

C6 contract changes (AZ-303 v1.3.0):
- VotingStatus.UPLOAD_GIVEUP added (forward-only from PENDING/TRUSTED).
- TileMetadataStore.increment_upload_attempts(tile_id) -> int added
  with NotImplementedError default for backwards-compat.
- Migration 0003_c11_upload_attempts: additive column +
  widened ck_tiles_voting_status (preserves IS NULL clause).

C11 wiring:
- C11RetryConfig + disable_retry_decorator on C11Config.
- build_tile_uploader wraps in decorator by default; bypass flag
  returns the bare HttpTileUploader. New `clock` keyword.

Cross-component isolation honoured (AZ-507): the decorator declares
`_RetryMetadataStoreLike` Protocol cut over c6's TileMetadataStore
and references `UPLOAD_GIVEUP` via a local string constant — no c6
imports.

Tests: 13 decorator + 1 conformance + 2 factory bypass + AC-6 enum
update + alembic head bump + AZ-272 schema fixture. 238 passed across
c11/c6/fdr suites; pre-existing perf microbenches unrelated.

Code review: PASS_WITH_WARNINGS (5 Low/Informational findings,
docs-level or downstream-CI-blocked). See
_docs/03_implementation/reviews/batch_41_review.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-13 08:48:53 +03:00

77 lines
2.6 KiB
Python

"""AC-5: alembic head is `0001_initial` and schema matches data_model.md § 2.
The migration is verified by inspecting the head revision and the upgrade
script's table/column declarations. We do NOT spin up Postgres here — that's
covered by integration tests; this is a Tier-1 unit check that the migration
metadata is correctly wired.
"""
from __future__ import annotations
import os
import re
from pathlib import Path
import pytest
from alembic import script as alembic_script
from alembic.config import Config
REPO_ROOT = Path(__file__).resolve().parents[2]
MIGRATION_BODY = (REPO_ROOT / "db" / "migrations" / "versions" / "0001_initial.py").read_text()
def test_head_revision_matches_latest_migration() -> None:
"""Asserts the Alembic head tracks the latest migration on disk.
AZ-263 originally pinned this to ``0001_initial``; AZ-304 advanced the head
to ``0002_c6_tile_identity_and_lru`` (additive on AZ-263 — see
``_docs/02_tasks/todo/AZ-304_c6_postgres_schema.md``). AZ-320 added
``0003_c11_upload_attempts`` (additive — adds the
``tiles.upload_attempts`` counter and widens the
``ck_tiles_voting_status`` constraint to admit ``upload_giveup``).
Future migrations update this assertion in lockstep with the new head.
"""
# Arrange
cwd = os.getcwd()
os.chdir(REPO_ROOT)
try:
cfg = Config(str(REPO_ROOT / "alembic.ini"))
sc = alembic_script.ScriptDirectory.from_config(cfg)
# Act
heads = sc.get_heads()
finally:
os.chdir(cwd)
# Assert
assert list(heads) == ["0003_c11_upload_attempts"], f"unexpected heads: {heads}"
@pytest.mark.parametrize(
"table",
[
"tiles",
"flights",
"sector_classifications",
"manifests",
"engine_cache_entries",
],
)
def test_initial_migration_declares_table(table: str) -> None:
# Assert — tolerate multi-line `op.create_table(\n "<table>"` formatting.
pattern = re.compile(
r"create_table\(\s*['\"]" + re.escape(table) + r"['\"]",
re.DOTALL,
)
assert pattern.search(MIGRATION_BODY), f"0001_initial.py missing create_table for `{table}`"
def test_tiles_table_has_canonical_source_check() -> None:
"""Canonical onboard-only additive columns from data_model.md § 2.1.1."""
# Assert
assert "googlemaps" in MIGRATION_BODY and "onboard_ingest" in MIGRATION_BODY, (
"tiles.source CHECK constraint must allow ('googlemaps', 'onboard_ingest')"
)
assert "tile_quality_metadata" in MIGRATION_BODY, (
"tiles must include the onboard-only `tile_quality_metadata` jsonb column"
)