test_wood_rule.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import shutil
  2. import sys
  3. import tempfile
  4. import unittest
  5. from pathlib import Path
  6. from openpyxl import load_workbook
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / "scripts"))
  9. import audit
  10. import rule_check
  11. from test_rule_sync import FakeKit, record
  12. WORKBOOK = Path(audit.pick_workbook())
  13. SNAPSHOT = ROOT / "references" / "doc-snapshot.txt"
  14. RULE = """十二、木制/竹制品商检费和LACY ACT申报(固定指令)
  15. 1.材质里面没有木、竹,三合板、胶合板、MDF 等木和竹材质的,直接勾选“否”;
  16. 2.清关编码和报关编码是9503开头的SKU,直接勾选“否”;
  17. 3.用途写是健身用的,直接勾选“否”;
  18. 4.以上都不符合,并且材质包含木、竹、三合板、胶合板、MDF 等木和竹的材质需根据历史记录去勾选木制/竹制品商检费和LACY ACT申报;
  19. """
  20. def current_document():
  21. text = SNAPSHOT.read_text(encoding="utf-8")
  22. if "十二、木制/竹制品商检费" not in text:
  23. text = text.replace("十二、固定指令汇总", RULE + "\n十三、固定指令汇总")
  24. return text
  25. def issues(document, workbook):
  26. result = rule_check.cmd_verify(document, str(workbook), quiet=True)
  27. return {item for _, item, status, _ in result["results"] if status == "❌"}
  28. class WoodRuleTests(unittest.TestCase):
  29. def test_review_is_required_for_every_nonstopped_record(self):
  30. auditor = audit.Auditor(FakeKit(), skip_images=True)
  31. scenarios = [
  32. record(**{"材质(CN)": "塑料", "材质(EN)": "plastic"}),
  33. record(**{"材质(CN)": "竹", "材质(EN)": "bamboo", "用途(CN)": "健身"}),
  34. record(**{"材质(CN)": "木", "材质(EN)": "wood", "清关编码": "9503.00.0090", "报关编码": "9503.00.0000"}),
  35. record(**{"材质(CN)": "木", "材质(EN)": "wood", "报关编码": ""}),
  36. record(**{"材质(CN)": "", "材质(EN)": ""}),
  37. ]
  38. for rec in scenarios:
  39. with self.subTest(fields=rec["fields"]):
  40. result = auditor.run(audit.Doc(rec))
  41. items = [item for item in result["items"] if str(item["seq"]) == "62"]
  42. self.assertEqual(len(items), 1)
  43. self.assertFalse(items[0]["auto"])
  44. self.assertEqual(items[0]["concl"], audit.HOLD)
  45. for expected in ("木", "竹", "9503", "清关编码", "报关编码", "健身", "历史", "商检", "LACY"):
  46. self.assertIn(expected, items[0]["why"])
  47. self.assertIn(items[0]["name"], result["need_human"])
  48. def test_precheck_still_stops_before_new_rule(self):
  49. auditor = audit.Auditor(FakeKit(), skip_images=True)
  50. result = auditor.run(audit.Doc(record(**{"目的国": "加拿大"})))
  51. self.assertTrue(result["stopped"])
  52. self.assertNotIn("62", {str(item["seq"]) for item in result["items"]})
  53. def test_uncovered_rule_cannot_be_reported_as_synced(self):
  54. with tempfile.TemporaryDirectory() as tmp:
  55. stale = Path(tmp) / "stale.xlsx"
  56. shutil.copyfile(WORKBOOK, stale)
  57. wb = load_workbook(stale)
  58. sheet = wb["02-审核判定表"]
  59. for row in range(sheet.max_row, 1, -1):
  60. if str(sheet.cell(row, 1).value) == "62":
  61. sheet.delete_rows(row)
  62. wb.save(stale)
  63. wb.close()
  64. bad = issues(current_document(), stale)
  65. self.assertIn("木竹无材质时选否", bad)
  66. def test_changed_document_and_changed_workbook_are_detected(self):
  67. doc = current_document()
  68. self.assertNotIn("木竹双编码 9503 时选否", issues(doc, WORKBOOK))
  69. self.assertIn("木竹双编码 9503 时选否", issues(doc.replace("报关编码是9503开头", "报关编码是9504开头"), WORKBOOK))
  70. with tempfile.TemporaryDirectory() as tmp:
  71. stale = Path(tmp) / "changed.xlsx"
  72. shutil.copyfile(WORKBOOK, stale)
  73. wb = load_workbook(stale)
  74. for row in wb["02-审核判定表"].iter_rows(min_row=2):
  75. if str(row[0].value) == "62":
  76. row[2].value = str(row[2].value).replace("清关编码和报关编码均以 9503 开头", "任一编码以 9503 开头")
  77. wb.save(stale)
  78. wb.close()
  79. self.assertIn("木竹双编码 9503 时选否", issues(doc, stale))
  80. if __name__ == "__main__":
  81. unittest.main()