| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- from __future__ import annotations
- from pathlib import Path
- ROOT = Path(__file__).resolve().parents[1]
- MIGRATION = (
- ROOT
- / "migrations"
- / "versions"
- / "20260723_110_ai_data_rules.py"
- )
- RUNTIME_MIGRATION = (
- ROOT
- / "migrations"
- / "versions"
- / "20260723_120_rule_execution_runtime.py"
- )
- EXPECTED_TABLES = {
- "data_rules",
- "data_rule_versions",
- "rule_generation_runs",
- "data_standards",
- "data_standard_versions",
- "standard_rule_bindings",
- "dataflow_versions",
- "dataflow_component_bindings",
- "rule_execution_plans",
- "rule_artifacts",
- "dataflow_deployments",
- "rule_runs",
- "rule_violation_samples",
- }
- def test_ai_data_rule_migration_defines_all_three_domain_layers():
- source = MIGRATION.read_text(encoding="utf-8")
- assert 'revision = "20260723_110"' in source
- assert 'down_revision = "20260722_110"' in source
- for table in EXPECTED_TABLES:
- assert f"CREATE TABLE public.{table}" in source
- def test_ai_data_rule_migration_enforces_immutable_versions_and_fixed_bindings():
- source = MIGRATION.read_text(encoding="utf-8")
- for expected in (
- "UNIQUE (rule_uid, version_no)",
- "UNIQUE (standard_uid, version_no)",
- "UNIQUE (dataflow_uid, version_no)",
- "standard_version_id UUID NOT NULL",
- "rule_version_id UUID NOT NULL",
- "dataflow_version_id UUID NOT NULL",
- "component_kind VARCHAR(30) NOT NULL",
- "package_hash CHAR(64)",
- "plan_hash CHAR(64)",
- "artifact_digest CHAR(64)",
- "context_hash CHAR(64)",
- "candidate_hash CHAR(64)",
- "workflow_version_id UUID",
- ):
- assert expected in source
- def test_ai_data_rule_migration_is_forward_preserving():
- source = MIGRATION.read_text(encoding="utf-8")
- downgrade = source.split("def downgrade()", 1)[1]
- assert "DROP TABLE" not in downgrade.upper()
- assert "pass" in downgrade
- def test_rule_execution_runtime_migration_adds_pinned_runtime_evidence():
- source = RUNTIME_MIGRATION.read_text(encoding="utf-8")
- assert 'revision = "20260723_120"' in source
- assert 'down_revision = "20260723_110"' in source
- for table in (
- "data_schema_snapshots",
- "dataflow_dataset_bindings",
- "rule_compile_evidence",
- "rule_test_evidence",
- ):
- assert f"CREATE TABLE public.{table}" in source
- for expected in (
- "rule_execution_plan_id UUID NOT NULL",
- "created_by UUID REFERENCES public.users(id)",
- "candidate JSONB",
- "UNIQUE (schema_ref, schema_hash)",
- "UNIQUE (dataflow_deployment_id, logical_ref)",
- ):
- assert expected in source
- def test_rule_execution_runtime_migration_is_forward_preserving():
- source = RUNTIME_MIGRATION.read_text(encoding="utf-8")
- downgrade = source.split("def downgrade()", 1)[1]
- assert "DROP TABLE" not in downgrade.upper()
- assert "pass" in downgrade
|