Files
live-hub-py/tests/test_migration_immutability.py
T

37 lines
1.1 KiB
Python

"""已发布 Alembic 迁移不可被静默修改。"""
from __future__ import annotations
from pathlib import Path
import pytest
from web.backend.migrations import immutability
def test_committed_migration_manifest_matches_files():
immutability.verify_manifest()
def test_manifest_rejects_modified_released_migration(tmp_path: Path):
versions = tmp_path / "versions"
versions.mkdir()
migration = versions / "20260101_0001_example.py"
migration.write_text('revision = "20260101_0001"\n', encoding="utf-8")
manifest = tmp_path / "migration_manifest.json"
immutability.write_manifest(versions_dir=versions, manifest_path=manifest)
migration.write_text(
'revision = "20260101_0001"\n# accidental released change\n',
encoding="utf-8",
)
with pytest.raises(immutability.MigrationManifestError, match="内容被修改"):
immutability.verify_manifest(
versions_dir=versions,
manifest_path=manifest,
)
with pytest.raises(immutability.MigrationManifestError, match="拒绝更新"):
immutability.write_manifest(versions_dir=versions, manifest_path=manifest)