am-freescan/scripts/test_apply_scan_only.py
2026-10-22 16:18:37 +08:00

88 lines
4.7 KiB
Python

"""Offline migration and menu topology checks; never connects to MySQL."""
import tempfile
from pathlib import Path
import unittest
from unittest.mock import patch
import bootstrap_db
import bootstrap_cmvr
class ScanMenuTopologyTest(unittest.TestCase):
def test_native_bootstrap_does_not_recreate_retired_pages(self):
identifiers = {menu[0] for menu in bootstrap_cmvr.MENUS}
self.assertTrue(identifiers.isdisjoint({23002, 23003, 23004, 23005}))
reference = bootstrap_cmvr.load_reference()
self.assertTrue(all(item['parentMenuId'] not in {23002, 23003, 23004, 23005}
for item in reference['permissions']))
states = next(dictionary for dictionary in reference['dictionaries'] if dictionary['type'] == 'lab_task_status')
self.assertTrue({'BLOCKED', 'STOP_PENDING'}.issubset({item['value'] for item in states['items']}))
def test_move_keeps_existing_child_ids_permissions_and_components(self):
self.assertEqual((23000, 'execution', 2), bootstrap_cmvr.MENU_REPARENTS[20310])
for identifier, path in [(20210, 'sample-transfer'), (20220, 'device-reservation'),
(20230, 'lab-reservation'), (20240, 'material-issue')]:
self.assertEqual((23010, path), bootstrap_cmvr.MENU_REPARENTS[identifier][:2])
self.assertEqual(23010, bootstrap_cmvr.MENU_REPARENTS[20250][0])
self.assertTrue({20200, 20300, 23002, 23003, 23004, 23005}.issubset(bootstrap_cmvr.RETIRED_MENUS))
def test_dedicated_migration_not_executed_as_reference_seed(self):
with tempfile.TemporaryDirectory() as folder:
directory = Path(folder)
(directory / '001_schema.sql').write_text('CREATE TABLE IF NOT EXISTS `lab_test` (\n`id` bigint\n);')
(directory / '005_scan_only.sql').write_text('ALTER TABLE `lab_test` ADD COLUMN `process_type` varchar(20);')
(directory / '006_resource_scheduling.sql').write_text('ALTER TABLE `lab_test` ADD COLUMN `exclusive_use` bit;')
with patch.object(bootstrap_db, 'SQL_ROOT', directory):
_, seeds = bootstrap_db.load_assets()
self.assertNotIn('005_scan_only.sql', seeds)
self.assertNotIn('006_resource_scheduling.sql', seeds)
class ScanSchemaMigrationTest(unittest.TestCase):
def load(self, source):
from apply_scan_only import load_migration
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / '005_scan_only.sql'
path.write_text(source, encoding='utf-8')
return load_migration(path)
def test_accepts_only_additive_owned_lab_ddl(self):
operations = self.load('''
CREATE TABLE IF NOT EXISTS `lab_scan_unit` (`id` bigint PRIMARY KEY,
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
ALTER TABLE `lab_test_task` ADD COLUMN `process_type` varchar(20) NOT NULL DEFAULT 'LEGACY_FULL';
ALTER TABLE `lab_scan_unit` ADD UNIQUE KEY `uk_scan_unit` (`id`);
''')
self.assertEqual(['table', 'column', 'index'], [operation['kind'] for operation in operations])
self.assertEqual(['lab_scan_unit', 'lab_test_task', 'lab_scan_unit'], [operation['table'] for operation in operations])
def test_rejects_destructive_cross_database_and_non_lab_sql_before_connecting(self):
for source in ['DROP TABLE lab_test_task;', 'DELETE FROM lab_test_task;',
'ALTER TABLE lab_test_task DROP COLUMN id;',
'ALTER TABLE `another`.`lab_test_task` ADD COLUMN x int;',
'CREATE TABLE IF NOT EXISTS system_users (id bigint);',
'ALTER TABLE lab_test_task ADD COLUMN x int, ADD COLUMN y int;',
'CREATE TABLE IF NOT EXISTS lab_copy AS SELECT * FROM another.lab_test_task;']:
with self.subTest(source=source), self.assertRaises(ValueError):
self.load(source)
def test_repeat_apply_skips_existing_columns_and_indexes(self):
from apply_scan_only import apply_schema
operations = self.load('''
ALTER TABLE `lab_test_task` ADD COLUMN `process_type` varchar(20);
ALTER TABLE `lab_test_task` ADD KEY `idx_scan_process` (`process_type`);
''')
class ExistingSchema:
statements = []
def execute(self, sql, parameters=None):
self.statements.append(sql)
def fetchone(self):
return (1,)
cursor = ExistingSchema()
self.assertEqual(0, apply_schema(cursor, operations))
self.assertFalse(any(sql.startswith('ALTER') for sql in cursor.statements))
if __name__ == '__main__':
unittest.main()