Files
admin d6e2317016
ABSDEV Framework CI / framework-validation (push) Canceled after 0s
Initial ABSDEV deployment framework development preview
2026-11-26 11:35:28 +00:00

393 lines
8.9 KiB
Python

import copy
import unittest
from core.compatibility import (
CompatibilityError,
compare_migrations,
require_compatible,
provenance_fingerprint,
)
def provenance(revision="a" * 40, migrations=None):
if migrations is None:
migrations = {
"database/migrations/0001_users.php": "a" * 64,
"database/migrations/0002_orders.php": "b" * 64,
}
data = {
"format_version": 1,
"revision": revision,
"migrations": migrations,
"dependencies": {
"composer.json": "c" * 64,
"composer.lock": "d" * 64,
"package.json": "e" * 64,
"package-lock.json": "f" * 64,
},
}
data["fingerprint"] = provenance_fingerprint(data)
return data
class CompatibilityTests(unittest.TestCase):
def setUp(self):
self.previous = provenance()
self.candidate = provenance("b" * 40)
def refresh(self, data):
data["fingerprint"] = provenance_fingerprint(data)
def compare(self):
return compare_migrations(
self.previous,
self.candidate,
)
def test_identical_migrations_compatible(self):
self.assertTrue(
self.compare()["compatible_without_migrations"]
)
def test_same_revision(self):
self.candidate = copy.deepcopy(self.previous)
self.assertTrue(self.compare()["same_revision"])
def test_different_revision(self):
self.assertFalse(self.compare()["same_revision"])
def test_added_migration(self):
name = "database/migrations/0003_products.php"
self.candidate["migrations"][name] = "c" * 64
self.refresh(self.candidate)
result = self.compare()
self.assertEqual(result["added"], [name])
self.assertFalse(
result["compatible_without_migrations"]
)
def test_removed_migration(self):
name = "database/migrations/0002_orders.php"
del self.candidate["migrations"][name]
self.refresh(self.candidate)
result = self.compare()
self.assertEqual(result["removed"], [name])
self.assertFalse(
result["compatible_without_migrations"]
)
def test_modified_migration(self):
name = "database/migrations/0001_users.php"
self.candidate["migrations"][name] = "d" * 64
self.refresh(self.candidate)
result = self.compare()
self.assertEqual(result["modified"], [name])
self.assertFalse(
result["compatible_without_migrations"]
)
def test_renamed_migration(self):
old = "database/migrations/0001_users.php"
new = "database/migrations/0003_users.php"
self.candidate["migrations"][new] = (
self.candidate["migrations"].pop(old)
)
self.refresh(self.candidate)
result = self.compare()
self.assertEqual(result["added"], [new])
self.assertEqual(result["removed"], [old])
self.assertFalse(
result["compatible_without_migrations"]
)
def test_dependency_changes_do_not_change_migrations(self):
self.candidate["dependencies"]["composer.lock"] = (
"1" * 64
)
self.refresh(self.candidate)
self.assertTrue(
self.compare()["compatible_without_migrations"]
)
def test_previous_migration_count(self):
self.assertEqual(
self.compare()["previous_count"],
2,
)
def test_candidate_migration_count(self):
self.assertEqual(
self.compare()["candidate_count"],
2,
)
def test_unchanged_migration_count(self):
self.assertEqual(
self.compare()["unchanged_count"],
2,
)
def test_empty_migrations(self):
self.previous = provenance(migrations={})
self.candidate = provenance(
"b" * 40,
migrations={},
)
self.assertTrue(
self.compare()["compatible_without_migrations"]
)
def test_invalid_fingerprint_rejected(self):
self.candidate["fingerprint"] = "0" * 64
with self.assertRaises(CompatibilityError):
self.compare()
def test_modified_evidence_rejected(self):
self.candidate["migrations"][
"database/migrations/0001_users.php"
] = "0" * 64
with self.assertRaises(CompatibilityError):
self.compare()
def test_missing_revision_rejected(self):
del self.candidate["revision"]
with self.assertRaises(CompatibilityError):
self.compare()
def test_invalid_revision_rejected(self):
self.candidate["revision"] = "invalid"
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_invalid_migration_hash_rejected(self):
self.candidate["migrations"][
"database/migrations/0001_users.php"
] = "invalid"
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_migration_path_traversal_rejected(self):
self.candidate["migrations"][
"database/migrations/../../outside.php"
] = "a" * 64
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_absolute_migration_path_rejected(self):
self.candidate["migrations"][
"/tmp/migration.php"
] = "a" * 64
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_missing_dependencies_rejected(self):
del self.candidate["dependencies"]["composer.lock"]
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_invalid_dependency_hash_rejected(self):
self.candidate["dependencies"]["composer.json"] = (
"invalid"
)
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_unknown_field_rejected(self):
self.candidate["unexpected"] = True
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_invalid_format_version_rejected(self):
self.candidate["format_version"] = 2
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_boolean_format_version_rejected(self):
self.candidate["format_version"] = True
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_require_compatible_success(self):
result = require_compatible(
self.previous,
self.candidate,
)
self.assertTrue(
result["compatible_without_migrations"]
)
def test_require_compatible_rejects_changes(self):
self.candidate["migrations"] = {}
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
require_compatible(
self.previous,
self.candidate,
)
def test_deterministic_report(self):
self.assertEqual(
self.compare(),
self.compare(),
)
def test_no_input_mutation(self):
previous = copy.deepcopy(self.previous)
candidate = copy.deepcopy(self.candidate)
self.compare()
self.assertEqual(self.previous, previous)
self.assertEqual(self.candidate, candidate)
def test_report_binds_provenance_fingerprints(self):
result = self.compare()
self.assertEqual(
result["previous_fingerprint"],
self.previous["fingerprint"],
)
self.assertEqual(
result["candidate_fingerprint"],
self.candidate["fingerprint"],
)
def test_same_revision_different_content_rejected(self):
self.candidate["revision"] = self.previous["revision"]
self.candidate["migrations"][
"database/migrations/0001_users.php"
] = "0" * 64
self.refresh(self.candidate)
with self.assertRaises(CompatibilityError):
self.compare()
def test_reverse_comparison_rejects_rollback(self):
self.candidate["migrations"][
"database/migrations/0003_products.php"
] = "a" * 64
self.refresh(self.candidate)
result = compare_migrations(
self.candidate,
self.previous,
)
self.assertFalse(
result["compatible_without_migrations"]
)
def test_invalid_evidence_type_rejected(self):
self.candidate = None
with self.assertRaises(CompatibilityError):
self.compare()
if __name__ == "__main__":
unittest.main()