112 lines
3.4 KiB
Python
112 lines
3.4 KiB
Python
from app.services.shipping_notice.extract import extract_notice_fields
|
|
from app.services.shipping_notice.notice_lines import (
|
|
normalize_notice_line,
|
|
parse_notice_table_rows,
|
|
parse_notice_table_text,
|
|
validate_notice_lines,
|
|
)
|
|
from app.services.shipping_notice.reservation_id import (
|
|
looks_like_reservation_id,
|
|
normalize_reservation_id,
|
|
)
|
|
|
|
|
|
def test_normalize_reservation_id_from_excel_float():
|
|
assert normalize_reservation_id(1000140351.0) == "1000140351"
|
|
assert normalize_reservation_id("1000140351.0") == "1000140351"
|
|
assert looks_like_reservation_id("1000140351")
|
|
|
|
|
|
def test_parse_notice_table_numeric_reservation_id():
|
|
text = """
|
|
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
|
|
10-40594-001\t1000140351\t2\t15.5\t31.0
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
|
|
|
|
def test_infer_reservation_id_without_header():
|
|
headers = ["AWS Sku", "Ref", "Qty"]
|
|
rows = [["10-40594-001", 1000140351, 2]]
|
|
lines = parse_notice_table_rows(headers, rows)
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
|
|
|
|
def test_parse_notice_table_with_all_fields():
|
|
text = """
|
|
AWS Sku\tReservation ID\tQty\tUnit Price\tAmount
|
|
10-40594-001\tAWS-311\t2\t15.5\t31.0
|
|
10-40594-002\tORD-3002\t1\t20\t20
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert len(lines) == 2
|
|
assert lines[0]["sku"] == "10-40594-001"
|
|
assert lines[0]["reservation_id"] == "AWS-311"
|
|
assert lines[0]["qty"] == 2
|
|
assert lines[0]["unit_price"] == 15.5
|
|
assert lines[0]["amount"] == 31.0
|
|
|
|
|
|
def test_normalize_notice_line_aliases():
|
|
line = normalize_notice_line(
|
|
{
|
|
"part_number": "A-001",
|
|
"quantity": "3",
|
|
"com_id": "COM-99",
|
|
"price": "12.5",
|
|
}
|
|
)
|
|
assert line == {
|
|
"sku": "A-001",
|
|
"qty": 3.0,
|
|
"uom": "EA",
|
|
"reservation_id": "COM-99",
|
|
"unit_price": 12.5,
|
|
"amount": 37.5,
|
|
}
|
|
|
|
|
|
def test_extract_notice_fields_structured():
|
|
structured = {
|
|
"customer_name": "ACME",
|
|
"lines": [
|
|
{
|
|
"sku": "10-40594-001",
|
|
"qty": 2,
|
|
"reservation_id": "AWS-311",
|
|
"unit_price": 10,
|
|
"amount": 20,
|
|
}
|
|
],
|
|
"confidence": 0.9,
|
|
}
|
|
parsed, confidence, warnings = extract_notice_fields(raw_text="", structured=structured)
|
|
assert parsed["lines"][0]["reservation_id"] == "AWS-311"
|
|
assert parsed["lines"][0]["amount"] == 20
|
|
assert confidence >= 0.8
|
|
assert not any("未解析到有效订单行" in w for w in warnings)
|
|
|
|
|
|
def test_validate_notice_lines_missing_reservation():
|
|
warnings = validate_notice_lines([{"sku": "A001", "qty": 1, "uom": "EA"}])
|
|
assert any("Reservation ID" in w for w in warnings)
|
|
|
|
|
|
def test_heuristic_table_skips_title_row():
|
|
from app.services.shipping_notice.extract import _heuristic_extract
|
|
|
|
text = """
|
|
Shipping Notice
|
|
AWS Sku\tReservation Order\tOrder Qty\tUnit Price\tLine Amount
|
|
10-40594-001\tORD-3002\t4\t5\t20
|
|
"""
|
|
parsed, confidence, _ = _heuristic_extract(text)
|
|
assert len(parsed["lines"]) == 1
|
|
assert parsed["lines"][0]["sku"] == "10-40594-001"
|
|
assert parsed["lines"][0]["reservation_id"] == "ORD-3002"
|
|
assert parsed["lines"][0]["qty"] == 4
|
|
assert parsed["lines"][0]["unit_price"] == 5
|
|
assert parsed["lines"][0]["amount"] == 20
|
|
assert confidence >= 0.5
|