Files
gps-denied-onboard/tests/unit/test_ac5_alembic.py
T
Oleksandr Bezdieniezhnykh dde838d2cc [AZ-304] C6 Postgres schema: additive 0002 migration + UUIDv5
Strictly additive Alembic migration on the AZ-263 baseline (data_model
.md § 6.1 / § 6.3): six new tiles columns (tile_uuid UNIQUE,
location_hash, content_sha256, disk_bytes, accessed_at, uploaded_at),
four new btree indices, one UNIQUE expression index over the
COALESCE-zero-uuid natural key, CHECK widening of
ck_tiles_freshness_status to the AZ-263 + AZ-303 vocabulary UNION,
four NULLable bbox columns on sector_classifications, and a new
tile_freshness_rules table seeded with the two default thresholds.

Pinned UUIDv5 namespace (TILE_NAMESPACE_UUID =
5b8d0c2e-1a4f-4b3a-8c9d-e7f6a3b2c1d0) + derive_tile_id /
derive_location_hash helpers cross-coordinated with
satellite-provider. Migration runner apply_migrations(config) drives
Alembic command.upgrade("head") against the AZ-263 env with one
retry on PG SQLSTATE 40001 and structured INFO logs on apply / no-op.

Contract bump tile_metadata_store.md v1.1.0 -> v1.2.0 adds
TileMetadata.location_hash: UUID | None = None (non-breaking).
module-layout.md updated so c6_tile_cache explicitly Owns
db/migrations/**.

Tier-1 tests: UUIDv5 determinism + locked vectors + DSN resolution +
retry mocked DBAPIError -> 1180 passed, 32 skipped. Tier-2 docker
schema tests gated by @pytest.mark.docker run against the existing
docker-compose.test.yml db service.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-12 17:05:41 +03:00

74 lines
2.4 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``). 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) == ["0002_c6_tile_identity_and_lru"], 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"
)