import importlib.util import io import unittest import zipfile from pathlib import Path from xml.sax.saxutils import escape spec = importlib.util.spec_from_file_location("template_audit", Path(__file__).with_name("client-import-template-inspect.py")) audit = importlib.util.module_from_spec(spec) spec.loader.exec_module(audit) def fixture(options=("测试仓|WH-A", "测试仓|WH-B"), rows="A3:A52", target="option_0", hidden=True): ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" rel = "http://schemas.openxmlformats.org/officeDocument/2006/relationships" body = io.BytesIO() with zipfile.ZipFile(body, "w") as z: z.writestr("[Content_Types].xml", '') z.writestr("_rels/.rels", f'') z.writestr("xl/workbook.xml", f'选项数据!$A$1:$A${len(options)}') z.writestr("xl/_rels/workbook.xml.rels", '' + ''.join(f'' for i in (1, 2, 3)) + '') z.writestr("xl/worksheets/sheet1.xml", f'国内收货仓 *{target}') data = ''.join(f'{escape(value)}' for i, value in enumerate(options, 1)) z.writestr("xl/worksheets/sheet2.xml", f'{data}') z.writestr("xl/worksheets/sheet3.xml", f'') return body.getvalue() class TemplateAuditTest(unittest.TestCase): def test_reads_actual_xlsx_names_validation_and_all_options(self): result = audit.inspect_template(fixture(), {"国内收货仓": ["测试仓|WH-A", "测试仓|WH-B"]}) self.assertTrue(result["passed"]) self.assertEqual(result["checks"]["国内收货仓"]["actualCount"], 2) def test_missing_extra_and_duplicate_options_are_reported_without_values(self): result = audit.inspect_template(fixture(), {"国内收货仓": ["测试仓|WH-A", "其他仓|WH-C"]}) self.assertFalse(result["passed"]) self.assertEqual(result["checks"]["国内收货仓"]["missingCount"], 1) self.assertEqual(result["checks"]["国内收货仓"]["extraCount"], 1) self.assertNotIn("WH-A", str(result)) self.assertFalse(audit.compare_options(["A", "A"], ["A"])["passed"]) def test_refuses_short_ranges_unresolved_names_and_exposed_options(self): for options in ({"rows": "A3:A51"}, {"target": "missing"}, {"hidden": False}): with self.assertRaises(AssertionError): audit.inspect_template(fixture(**options), {"国内收货仓": ["测试仓|WH-A", "测试仓|WH-B"]}) def test_duplicate_business_labels_remain_distinct_choices(self): self.assertEqual(audit.labels(["A", "A", "A"], lambda x: x), ["A", "A(同名选项 2)", "A(同名选项 3)"]) if __name__ == "__main__": unittest.main()