51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
"""SurugayaOrderDetailParser 商品明细字段映射的回归测试。
|
|
|
|
锁定 ITEM_FIELD_MAP 字段名 bug 的修复:品番/状態/数量/金額 必须正确填充
|
|
product_code/condition/quantity/line_total,而非被 pydantic 静默丢弃。
|
|
"""
|
|
|
|
from surugaya_common.models import SurugayaOrderItem
|
|
from surugaya_common.parsers.surugaya.order_detail import (
|
|
ITEM_FIELD_MAP,
|
|
SurugayaOrderDetailParser,
|
|
)
|
|
|
|
|
|
def test_item_field_map_targets_are_real_model_fields():
|
|
"""ITEM_FIELD_MAP 的每个目标字段都必须是 SurugayaOrderItem 的真实字段。"""
|
|
model_fields = set(SurugayaOrderItem.model_fields)
|
|
for jp_column, en_field in ITEM_FIELD_MAP.items():
|
|
assert en_field in model_fields, f"{jp_column} 映射到非法字段 {en_field}"
|
|
|
|
|
|
def test_normalize_item_row_populates_all_corrected_fields():
|
|
"""修复后:品番/状態/数量/金額 应正确填充,金额/整数字段完成归一化。"""
|
|
parser = SurugayaOrderDetailParser()
|
|
raw_item = {
|
|
"品番": "ABC-123",
|
|
"状態": "中古",
|
|
"枝番": "1",
|
|
"商品タイトル": "测试商品",
|
|
"単価": "1,250円",
|
|
"数量": "2",
|
|
"値引額": "100円",
|
|
"金額": "2,400円",
|
|
"備考": "x",
|
|
}
|
|
|
|
normalized = parser._normalize_mapped_values(raw_item, ITEM_FIELD_MAP)
|
|
item = SurugayaOrderItem(**normalized, raw=raw_item)
|
|
|
|
# 此前被静默丢弃的 4 个字段现在应被正确填充
|
|
assert item.product_code == "ABC-123"
|
|
assert item.condition == "中古"
|
|
assert item.quantity == 2 # INT_FIELDS 归一
|
|
assert item.line_total == 2400 # MONEY_FIELDS 归一,逗号去除
|
|
|
|
# 原本就正确的字段保持不变
|
|
assert item.branch_number == "1"
|
|
assert item.product_title == "测试商品"
|
|
assert item.unit_price == 1250
|
|
assert item.discount_amount == 100
|
|
assert item.remarks == "x"
|