| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- """入库、周四归月与聚合口径的单元测试。"""
- from __future__ import annotations
- import csv
- from datetime import date
- from decimal import Decimal
- from pathlib import Path
- import pytest
- from toolsvc.customs.engine.rules import Rules
- from toolsvc.customs.engine.storage import Store, ingest, week_rows
- RULES_DIR = Path(__file__).resolve().parents[1] / "toolsvc" / "customs" / "rules"
- HEADER = ["I_E_FLAG", "HS_CODE", "YEAR", "WEEK", "MONORSUM", "COUNT", "G_MODEL", "ORIGIN_COUNTRY", "TRADE_AREA_CODE", "TRADE_COUNTRY", "DISTINATE_PORT"]
- @pytest.fixture(scope="module")
- def rules():
- return Rules(RULES_DIR / "数据映射规则.xlsx", RULES_DIR / "进出口数据字段需求20260819.xlsx")
- def write_csv(path: Path, rows: list[list[str]]) -> Path:
- with path.open("w", encoding="utf-8", newline="") as handle:
- writer = csv.writer(handle)
- writer.writerow(HEADER)
- writer.writerows(rows)
- return path
- def config_for(path: Path, out: Path, **overrides) -> dict:
- return {
- "input_csv": str(path),
- "encoding": "utf-8",
- "delimiter": ",",
- "directions": ["I"],
- "cutoff_date": None,
- "start_year": 2025,
- "end_year": 2026,
- "country_code_system": "ISO",
- "confirmed_complete_years": [],
- "allow_partial_year_basket": True,
- **overrides,
- }
- def sample_row(hs="8532241000", year="2025", week="1", amount="100.50", quantity="10", model="4|0|多层|陶瓷|MURATA|GRM188|||"):
- return ["I", hs, year, week, amount, quantity, model, "JPN", "KOR", "JPN", "JPN501"]
- def test_thursday_month_mapping_is_iso_week_based():
- assert date.fromisocalendar(2025, 13, 4) == date(2025, 3, 27)
- rows = {row[1]: row for row in week_rows(2025, 2026) if row[0] == 2025}
- assert rows[13][6] == 3 # 归属 3 月
- assert rows[5][6] == 1 # 2025-W05 周四 1-30,归 1 月
- assert rows[18][6] == 5 # 2025-W18 周四 5-01,归 5 月
- def test_ingest_maps_week_to_month_and_counts(tmp_path, rules):
- path = write_csv(tmp_path / "in.csv", [sample_row(week="1"), sample_row(week="5", amount="200.00")])
- database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
- try:
- assert stats["total"] == 2
- assert stats["included"] == 2
- assert stats["min_month"] == "2025-01"
- store = Store(database)
- total = store.total({"direction": "I"})
- assert total["amount"] == Decimal("300.50")
- assert total["n"] == 2
- months = {row["month"]: row for row in store.aggregate(("month",), {"direction": "I"})}
- assert set(months) == {"2025-01"} # W01 与 W05 的周四都在 1 月
- finally:
- database.close()
- def test_ingest_excludes_out_of_scope_rows(tmp_path, rules):
- rows = [
- sample_row(), # 正常
- sample_row(hs="85322410"), # 8 位 HS
- sample_row(hs="8517700000"), # 不在项目范围
- sample_row(week="60"), # 非法周
- sample_row(year="2031", week="2"), # 晚于截止日期(默认今天)
- ["E", "8532241000", "2025", "1", "10", "1", "4|0|多层|陶瓷|MURATA|GRM188|||", "JPN", "KOR", "JPN", "JPN501"], # 方向不符
- ]
- path = write_csv(tmp_path / "in.csv", rows)
- database, stats = ingest(config_for(path, tmp_path, cutoff_date="2026-12-31"), rules, tmp_path)
- try:
- assert stats["total"] == 6
- assert stats["included"] == 1 # 只有第一行在范围内;2031 年仍晚于 2026-12-31
- assert stats["excluded"] == 5
- finally:
- database.close()
- def test_ingest_records_numeric_alerts_without_dropping_rows(tmp_path, rules):
- path = write_csv(tmp_path / "in.csv", [sample_row(amount="0"), sample_row(amount="-5", quantity="abc")])
- database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
- try:
- assert stats["included"] == 2
- assert stats["alerts"] >= 2
- issues = database.execute("SELECT field,reason FROM issues").fetchall()
- assert any("零值或负数" in row["reason"] for row in issues)
- assert any("无法识别" in row["reason"] for row in issues)
- finally:
- database.close()
- def test_ingest_rejects_missing_required_column(tmp_path, rules):
- path = tmp_path / "bad.csv"
- path.write_text("I_E_FLAG,HS_CODE\nI,8532241000\n", encoding="utf-8")
- with pytest.raises(ValueError, match="缺少必需字段"):
- ingest(config_for(path, tmp_path), rules, tmp_path)
- def test_store_rejects_unknown_dimension(rules, tmp_path):
- path = write_csv(tmp_path / "in.csv", [sample_row()])
- database, _stats = ingest(config_for(path, tmp_path), rules, tmp_path)
- try:
- with pytest.raises(ValueError):
- list(Store(database).aggregate(("nope",), {}))
- finally:
- database.close()
- def test_chinese_headers_are_aliased(tmp_path, rules):
- path = tmp_path / "cn.csv"
- path.write_text(
- "进出口,HS编码,年份,周,金额,数量,品牌/型号\nI,8532241000,2025,3,88.80,7,4|0|多层|陶瓷|MURATA|GRM188|||\n",
- encoding="utf-8",
- )
- database, stats = ingest(config_for(path, tmp_path), rules, tmp_path)
- try:
- assert stats["included"] == 1
- finally:
- database.close()
|