226 lines
7.8 KiB
Python
226 lines
7.8 KiB
Python
from app.services.shipping_notice.extract import extract_notice_fields, _heuristic_extract
|
|
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 (
|
|
extract_reservation_id,
|
|
find_reservation_id_in_document,
|
|
looks_like_reservation_id,
|
|
normalize_reservation_id,
|
|
)
|
|
|
|
|
|
def test_extract_notice_text_reads_xfa_pdf_datasets():
|
|
"""XFA 表单型 PO 的订单字段应进入 raw_text,避免只抽页面文本漏明细。"""
|
|
from pathlib import Path
|
|
|
|
from app.services.shipping_notice.notice_upload import extract_notice_text
|
|
|
|
pdf_path = Path(__file__).resolve().parents[2] / "docs" / "prototype" / "4510038672.pdf"
|
|
text, meta = extract_notice_text(pdf_path)
|
|
|
|
assert meta["file_type"] == "pdf"
|
|
assert "pdf xfa datasets extracted" in meta["warnings"]
|
|
assert "HEADER/EBELN: 4510038672" in text
|
|
assert "HEADER/SHIPMENT_TEXT: 1000144510" in text
|
|
assert "TEMP-S000218922" in text
|
|
assert "MPN:D4-A08AR12000001" in text
|
|
assert "EINDT=2026-06-23" in text
|
|
|
|
|
|
def test_extract_reservation_id_from_pr_number():
|
|
assert extract_reservation_id("ADU_S-ENCWIW260528126265@1000140351") == "1000140351"
|
|
assert find_reservation_id_in_document("PR Number: ADU_S-ENCWIW260528126265@1000140351") == "1000140351"
|
|
|
|
|
|
def test_wistron_po_table_with_pr_number_in_document():
|
|
text = """
|
|
Vendor: HOXIN
|
|
PR Number: ADU_S-ENCWIW260528126265@1000140351
|
|
Line No\tMaterial\tVendor Part Number\tDescription\tQuantity\tETA\tUnit Price\tAmount
|
|
10\t504-010213-001\tB3-A08AR11000001\tENCLOSURE\t9.000 PCS\t2026/06/15\t1,607.72 USD\t14,469.48 USD
|
|
"""
|
|
lines = parse_notice_table_text(text)
|
|
assert len(lines) == 1
|
|
assert lines[0]["sku"] == "504-010213-001"
|
|
assert lines[0]["line_no"] == "10"
|
|
assert lines[0]["qty"] == 9.0
|
|
assert lines[0]["unit_price"] == 1607.72
|
|
assert lines[0]["amount"] == 14469.48
|
|
assert lines[0]["reservation_id"] == "1000140351"
|
|
assert lines[0]["required_date"] == "2026-06-15"
|
|
assert lines[0]["vendor_part_number"] == "B3-A08AR11000001"
|
|
assert lines[0]["currency"] == "USD"
|
|
|
|
|
|
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",
|
|
"delivery_date": "2026/06/15",
|
|
"vendorPartNumber": "VND-001",
|
|
"currency": "USD",
|
|
"line_no": "10",
|
|
}
|
|
)
|
|
assert line == {
|
|
"line_no": "10",
|
|
"sku": "A-001",
|
|
"qty": 3.0,
|
|
"uom": "EA",
|
|
"reservation_id": "COM-99",
|
|
"unit_price": 12.5,
|
|
"amount": 37.5,
|
|
"currency": "USD",
|
|
"required_date": "2026-06-15",
|
|
"vendor_part_number": "VND-001",
|
|
}
|
|
|
|
|
|
def test_enrich_notice_with_document_currency():
|
|
from app.services.shipping_notice.notice_lines import enrich_notice_with_document_currency
|
|
|
|
parsed = enrich_notice_with_document_currency(
|
|
{
|
|
"currency": "USD",
|
|
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "currency": "EUR", "uom": "EA"}],
|
|
}
|
|
)
|
|
assert parsed["lines"][0]["currency"] == "USD"
|
|
assert parsed["lines"][1]["currency"] == "EUR"
|
|
|
|
|
|
def test_extract_currency_from_price_string():
|
|
from app.services.shipping_notice.notice_lines import _extract_currency
|
|
|
|
assert _extract_currency("1,607.72 USD") == "USD"
|
|
assert _extract_currency("$15.5") == "USD"
|
|
assert _extract_currency("¥100") == "CNY"
|
|
|
|
|
|
def test_enrich_notice_with_document_required_date():
|
|
from app.services.shipping_notice.notice_lines import enrich_notice_with_document_required_date
|
|
|
|
parsed = enrich_notice_with_document_required_date(
|
|
{
|
|
"required_date": "2026-06-20",
|
|
"lines": [{"sku": "A-001", "qty": 1, "uom": "EA"}, {"sku": "B-002", "qty": 2, "required_date": "2026-06-18", "uom": "EA"}],
|
|
}
|
|
)
|
|
assert parsed["lines"][0]["required_date"] == "2026-06-20"
|
|
assert parsed["lines"][1]["required_date"] == "2026-06-18"
|
|
|
|
|
|
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)
|
|
assert any("阻断后续库存匹配" 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
|
|
|
|
|
|
def test_extract_notice_fields_structured_over_heuristic():
|
|
"""Dify 传入 structured 时应直接使用,不走启发式。"""
|
|
structured = {
|
|
"customer_name": "ACME",
|
|
"lines": [{"sku": "SKU-DIFY", "qty": 3, "uom": "EA"}],
|
|
"confidence": 0.95,
|
|
}
|
|
parsed, confidence, warnings = extract_notice_fields(
|
|
raw_text="ignored SKU x 99",
|
|
structured=structured,
|
|
)
|
|
assert parsed["parse_source"] == "dify"
|
|
assert parsed["lines"][0]["sku"] == "SKU-DIFY"
|
|
assert confidence > 0.8
|
|
assert "parse_fallback_heuristic" not in warnings
|
|
|
|
|
|
def test_extract_notice_fields_heuristic_fallback():
|
|
"""无 structured 时走后端启发式兜底。"""
|
|
text = "AWS Sku\tReservation Order\tOrder Qty\n10-40594-001\tORD-3002\t4"
|
|
parsed, _, warnings = extract_notice_fields(raw_text=text)
|
|
assert parsed["parse_source"] == "heuristic"
|
|
assert parsed["lines"][0]["sku"] == "10-40594-001"
|
|
assert "parse_fallback_heuristic" in warnings
|