test_storage.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. """入库、周四归月与聚合口径的单元测试。"""
  2. from __future__ import annotations
  3. import csv
  4. from datetime import date
  5. from decimal import Decimal
  6. from pathlib import Path
  7. import pytest
  8. from toolsvc.customs.engine.rules import Rules
  9. from toolsvc.customs.engine.storage import Store, ingest, week_rows
  10. RULES_DIR = Path(__file__).resolve().parents[1] / "toolsvc" / "customs" / "rules"
  11. HEADER = ["I_E_FLAG", "HS_CODE", "YEAR", "WEEK", "MONORSUM", "COUNT", "G_MODEL", "ORIGIN_COUNTRY", "TRADE_AREA_CODE", "TRADE_COUNTRY", "DISTINATE_PORT"]
  12. @pytest.fixture(scope="module")
  13. def rules():
  14. return Rules(RULES_DIR / "数据映射规则.xlsx", RULES_DIR / "进出口数据字段需求20260819.xlsx")
  15. def write_csv(path: Path, rows: list[list[str]]) -> Path:
  16. with path.open("w", encoding="utf-8", newline="") as handle:
  17. writer = csv.writer(handle)
  18. writer.writerow(HEADER)
  19. writer.writerows(rows)
  20. return path
  21. def config_for(path: Path, out: Path, **overrides) -> dict:
  22. return {
  23. "input_csv": str(path),
  24. "encoding": "utf-8",
  25. "delimiter": ",",
  26. "directions": ["I"],
  27. "cutoff_date": None,
  28. "start_year": 2025,
  29. "end_year": 2026,
  30. "country_code_system": "ISO",
  31. "confirmed_complete_years": [],
  32. "allow_partial_year_basket": True,
  33. **overrides,
  34. }
  35. def sample_row(hs="8532241000", year="2025", week="1", amount="100.50", quantity="10", model="4|0|多层|陶瓷|MURATA|GRM188|||"):
  36. return ["I", hs, year, week, amount, quantity, model, "JPN", "KOR", "JPN", "JPN501"]
  37. def test_thursday_month_mapping_is_iso_week_based():
  38. assert date.fromisocalendar(2025, 13, 4) == date(2025, 3, 27)
  39. rows = {row[1]: row for row in week_rows(2025, 2026) if row[0] == 2025}
  40. assert rows[13][6] == 3 # 归属 3 月
  41. assert rows[5][6] == 1 # 2025-W05 周四 1-30,归 1 月
  42. assert rows[18][6] == 5 # 2025-W18 周四 5-01,归 5 月
  43. def test_ingest_maps_week_to_month_and_counts(tmp_path, rules):
  44. path = write_csv(tmp_path / "in.csv", [sample_row(week="1"), sample_row(week="5", amount="200.00")])
  45. database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
  46. try:
  47. assert stats["total"] == 2
  48. assert stats["included"] == 2
  49. assert stats["min_month"] == "2025-01"
  50. store = Store(database)
  51. total = store.total({"direction": "I"})
  52. assert total["amount"] == Decimal("300.50")
  53. assert total["n"] == 2
  54. months = {row["month"]: row for row in store.aggregate(("month",), {"direction": "I"})}
  55. assert set(months) == {"2025-01"} # W01 与 W05 的周四都在 1 月
  56. finally:
  57. database.close()
  58. def test_ingest_excludes_out_of_scope_rows(tmp_path, rules):
  59. rows = [
  60. sample_row(), # 正常
  61. sample_row(hs="85322410"), # 8 位 HS
  62. sample_row(hs="8517700000"), # 不在项目范围
  63. sample_row(week="60"), # 非法周
  64. sample_row(year="2031", week="2"), # 晚于截止日期(默认今天)
  65. ["E", "8532241000", "2025", "1", "10", "1", "4|0|多层|陶瓷|MURATA|GRM188|||", "JPN", "KOR", "JPN", "JPN501"], # 方向不符
  66. ]
  67. path = write_csv(tmp_path / "in.csv", rows)
  68. database, stats = ingest(config_for(path, tmp_path, cutoff_date="2026-12-31"), rules, tmp_path)
  69. try:
  70. assert stats["total"] == 6
  71. assert stats["included"] == 1 # 只有第一行在范围内;2031 年仍晚于 2026-12-31
  72. assert stats["excluded"] == 5
  73. finally:
  74. database.close()
  75. def test_ingest_records_numeric_alerts_without_dropping_rows(tmp_path, rules):
  76. path = write_csv(tmp_path / "in.csv", [sample_row(amount="0"), sample_row(amount="-5", quantity="abc")])
  77. database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
  78. try:
  79. assert stats["included"] == 2
  80. assert stats["alerts"] >= 2
  81. issues = database.execute("SELECT field,reason FROM issues").fetchall()
  82. assert any("零值或负数" in row["reason"] for row in issues)
  83. assert any("无法识别" in row["reason"] for row in issues)
  84. finally:
  85. database.close()
  86. def test_ingest_rejects_missing_required_column(tmp_path, rules):
  87. path = tmp_path / "bad.csv"
  88. path.write_text("I_E_FLAG,HS_CODE\nI,8532241000\n", encoding="utf-8")
  89. with pytest.raises(ValueError, match="缺少必需字段"):
  90. ingest(config_for(path, tmp_path), rules, tmp_path)
  91. def test_store_rejects_unknown_dimension(rules, tmp_path):
  92. path = write_csv(tmp_path / "in.csv", [sample_row()])
  93. database, _stats = ingest(config_for(path, tmp_path), rules, tmp_path)
  94. try:
  95. with pytest.raises(ValueError):
  96. list(Store(database).aggregate(("nope",), {}))
  97. finally:
  98. database.close()
  99. def test_chinese_headers_are_aliased(tmp_path, rules):
  100. path = tmp_path / "cn.csv"
  101. path.write_text(
  102. "进出口,HS编码,年份,周,金额,数量,品牌/型号\nI,8532241000,2025,3,88.80,7,4|0|多层|陶瓷|MURATA|GRM188|||\n",
  103. encoding="utf-8",
  104. )
  105. database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
  106. try:
  107. assert stats["included"] == 1
  108. finally:
  109. database.close()