| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import shutil
- import sys
- import tempfile
- import unittest
- from pathlib import Path
- from openpyxl import load_workbook
- ROOT = Path(__file__).resolve().parents[1]
- sys.path.insert(0, str(ROOT / "scripts"))
- import audit
- import rule_check
- from test_rule_sync import FakeKit, record
- WORKBOOK = Path(audit.pick_workbook())
- SNAPSHOT = ROOT / "references" / "doc-snapshot.txt"
- RULE = """十二、木制/竹制品商检费和LACY ACT申报(固定指令)
- 1.材质里面没有木、竹,三合板、胶合板、MDF 等木和竹材质的,直接勾选“否”;
- 2.清关编码和报关编码是9503开头的SKU,直接勾选“否”;
- 3.用途写是健身用的,直接勾选“否”;
- 4.以上都不符合,并且材质包含木、竹、三合板、胶合板、MDF 等木和竹的材质需根据历史记录去勾选木制/竹制品商检费和LACY ACT申报;
- """
- def current_document():
- text = SNAPSHOT.read_text(encoding="utf-8")
- if "十二、木制/竹制品商检费" not in text:
- text = text.replace("十二、固定指令汇总", RULE + "\n十三、固定指令汇总")
- return text
- def issues(document, workbook):
- result = rule_check.cmd_verify(document, str(workbook), quiet=True)
- return {item for _, item, status, _ in result["results"] if status == "❌"}
- class WoodRuleTests(unittest.TestCase):
- def test_review_is_required_for_every_nonstopped_record(self):
- auditor = audit.Auditor(FakeKit(), skip_images=True)
- scenarios = [
- record(**{"材质(CN)": "塑料", "材质(EN)": "plastic"}),
- record(**{"材质(CN)": "竹", "材质(EN)": "bamboo", "用途(CN)": "健身"}),
- record(**{"材质(CN)": "木", "材质(EN)": "wood", "清关编码": "9503.00.0090", "报关编码": "9503.00.0000"}),
- record(**{"材质(CN)": "木", "材质(EN)": "wood", "报关编码": ""}),
- record(**{"材质(CN)": "", "材质(EN)": ""}),
- ]
- for rec in scenarios:
- with self.subTest(fields=rec["fields"]):
- result = auditor.run(audit.Doc(rec))
- items = [item for item in result["items"] if str(item["seq"]) == "62"]
- self.assertEqual(len(items), 1)
- self.assertFalse(items[0]["auto"])
- self.assertEqual(items[0]["concl"], audit.HOLD)
- for expected in ("木", "竹", "9503", "清关编码", "报关编码", "健身", "历史", "商检", "LACY"):
- self.assertIn(expected, items[0]["why"])
- self.assertIn(items[0]["name"], result["need_human"])
- def test_precheck_still_stops_before_new_rule(self):
- auditor = audit.Auditor(FakeKit(), skip_images=True)
- result = auditor.run(audit.Doc(record(**{"目的国": "加拿大"})))
- self.assertTrue(result["stopped"])
- self.assertNotIn("62", {str(item["seq"]) for item in result["items"]})
- def test_uncovered_rule_cannot_be_reported_as_synced(self):
- with tempfile.TemporaryDirectory() as tmp:
- stale = Path(tmp) / "stale.xlsx"
- shutil.copyfile(WORKBOOK, stale)
- wb = load_workbook(stale)
- sheet = wb["02-审核判定表"]
- for row in range(sheet.max_row, 1, -1):
- if str(sheet.cell(row, 1).value) == "62":
- sheet.delete_rows(row)
- wb.save(stale)
- wb.close()
- bad = issues(current_document(), stale)
- self.assertIn("木竹无材质时选否", bad)
- def test_changed_document_and_changed_workbook_are_detected(self):
- doc = current_document()
- self.assertNotIn("木竹双编码 9503 时选否", issues(doc, WORKBOOK))
- self.assertIn("木竹双编码 9503 时选否", issues(doc.replace("报关编码是9503开头", "报关编码是9504开头"), WORKBOOK))
- with tempfile.TemporaryDirectory() as tmp:
- stale = Path(tmp) / "changed.xlsx"
- shutil.copyfile(WORKBOOK, stale)
- wb = load_workbook(stale)
- for row in wb["02-审核判定表"].iter_rows(min_row=2):
- if str(row[0].value) == "62":
- row[2].value = str(row[2].value).replace("清关编码和报关编码均以 9503 开头", "任一编码以 9503 开头")
- wb.save(stale)
- wb.close()
- self.assertIn("木竹双编码 9503 时选否", issues(doc, stale))
- if __name__ == "__main__":
- unittest.main()
|