import sys import hashlib import tempfile import unittest import xml.etree.ElementTree as ET from pathlib import Path from zipfile import ZipFile ROOT = Path(__file__).resolve().parents[1] sys.path[:0] = [str(ROOT / 'scripts'), str(ROOT / 'verification')] import verify_dsh_formal as fixture import isolated_runtime class DshFormalFixtureTests(unittest.TestCase): def test_seed_is_three_months_two_stages_two_org_groups_and_six_products(self): rows = fixture.fixture_rows() self.assertEqual(len(rows), 36) self.assertEqual({row.month for row in rows}, {'2026-08', '2026-09', '2026-10'}) self.assertEqual({row.stage for row in rows}, {'black', 'bright'}) self.assertEqual({row.department for row in rows}, set(fixture.DEPARTMENTS)) self.assertEqual({row.group for row in rows}, set(fixture.GROUPS)) self.assertEqual({row.product for row in rows}, set(fixture.PRODUCTS)) for month in fixture.PERIODS: for stage in ('black', 'bright'): selected = [row for row in rows if row.month == month and row.stage == stage] self.assertEqual({row.product for row in selected}, set(fixture.PRODUCTS)) def test_missing_detail_is_explicit_and_formal_rollup_stays_complete(self): rows = fixture.fixture_rows() missing = [row for row in rows if row.month == '2026-09' and row.stage == 'black' and row.product == '合成产品06'] self.assertEqual(len(missing), 1) self.assertIn('责废尺寸重量', missing[0].raw) self.assertIsNone(missing[0].raw['责废尺寸重量']) self.assertGreater(missing[0].components['black_responsibility_scrap'], 0) self.assertEqual(fixture.fixture_totals(rows, 'black', '2026-10')['rows'], 6) self.assertEqual(fixture.fixture_totals(rows, 'bright', '2026-10')['rows'], 6) def test_generated_archive_is_a_hashed_synthetic_xlsx_with_source_fields(self): rows = [row for row in fixture.fixture_rows() if row.stage == 'black' and row.month == '2026-09'] with tempfile.TemporaryDirectory() as temporary: path = Path(temporary) / 'synthetic.xlsx' digest, size = fixture._write_workbook(path, rows) self.assertEqual(digest, hashlib.sha256(path.read_bytes()).hexdigest()) self.assertEqual(size, path.stat().st_size) with ZipFile(path) as archive: self.assertTrue({'[Content_Types].xml', '_rels/.rels', 'xl/workbook.xml', 'xl/worksheets/sheet1.xml'}.issubset(set(archive.namelist()))) worksheet = ET.fromstring(archive.read('xl/worksheets/sheet1.xml')) cells = [element.text for element in worksheet.iter() if element.tag.endswith('}t')] self.assertIn('责废尺寸重量', cells) self.assertIn('责废缺角重量', cells) def test_source_physical_rows_restart_for_each_source_workbook(self): values = list(fixture.source_physical_rows(fixture.fixture_rows())) first_file = [row_no for row, row_no in values if row.stage == 'black' and row.month == '2026-08'] next_file = [row_no for row, row_no in values if row.stage == 'bright' and row.month == '2026-08'] self.assertEqual(first_file, list(range(2, 8))) self.assertEqual(next_file, list(range(2, 8))) def test_private_sql_literals_are_hex_encoded(self): self.assertEqual(fixture._literal("模型'; DROP DATABASE nope;"), 'CONVERT(0x' + "模型'; DROP DATABASE nope;".encode().hex() + ' USING utf8mb4)') self.assertIn('CAST(CONVERT(0x', fixture._json_sql({'来源': '合成值'})) def test_isolated_runtime_accepts_only_scoped_engine_and_protocol_overrides(self): signature = isolated_runtime.isolated_api.__wrapped__.__code__.co_varnames self.assertIn('env_overrides', signature) self.assertIn('auth_credentials', signature) source = (ROOT / 'verification/isolated_runtime.py').read_text(encoding='utf-8') self.assertIn("'QUALITY_ARCHIVE_ROOT'", source) self.assertIn("'SERVER_SERVLET_SESSION_COOKIE_NAME'", source) self.assertTrue(callable(isolated_runtime.sql)) self.assertTrue(callable(isolated_runtime.clone_database)) if __name__ == '__main__': unittest.main()