# 命令行吃接口 3 信封:合单后的客商、货品、金额都来自报文。 from __future__ import annotations import json from pathlib import Path import pytest from contract_create_node import ContractCreateError, build_spec_from_data, load_input_data from contract_draft import contract_draft_from_dict _OMS_EXAMPLE = ( Path(__file__).resolve().parent.parent / "examples" / "oms_purchase_contract.json" ) _PAGE_DRAFT = ( Path(__file__).resolve().parent.parent / "examples" / "contract_create_input_dry_run.json" ) def test_build_spec_from_oms_envelope_uses_json_fields(monkeypatch): monkeypatch.setenv("CONTRACT_USERNAME", "u") monkeypatch.setenv("CONTRACT_PASSWORD", "p") data = json.loads(_OMS_EXAMPLE.read_text(encoding="utf-8")) spec = build_spec_from_data(data) ctx = spec.flow_context assert ctx is not None assert spec.job_id == "OMS-DW0100046361-2026-10-25" assert spec.supplier_name == "重庆亿昊药业有限公司" assert spec.order_date == "2026-10-25" assert spec.submit_mode == "submit" assert ctx.customer_code == "DW0100046361" assert ctx.customer_name == "重庆亿昊药业有限公司" assert ctx.amount == "300.0000" assert ctx.contract_sign_date == "2026-10-25" assert ctx.contract_start_date == "2026-10-25" assert ctx.contract_end_date == "2027-10-24" assert ctx.payment_days == "30" assert ctx.contract_category == "" assert ctx.settlement_type == "" assert ctx.payment_method == "" assert ctx.is_prepayment == "" assert [line.mdm_code for line in ctx.goods_lines] == [ "FQX0100020661", "FQX0100031787", "FQX0100044037", ] assert [line.unit_price for line in ctx.goods_lines] == [ "10.0000", "10.0000", "10.0000", ] def test_page_draft_json_still_builds(monkeypatch): monkeypatch.setenv("CONTRACT_USERNAME", "u") monkeypatch.setenv("CONTRACT_PASSWORD", "p") data = json.loads(_PAGE_DRAFT.read_text(encoding="utf-8")) spec = build_spec_from_data(data) assert spec.submit_mode == "dry_run" assert spec.flow_context is not None assert spec.flow_context.customer_code == "DW0100046361" assert spec.flow_context.goods_lines[0].goods_code == "FQX0100118047" # JSON 没写的页面字段保持空,不回填账期/采购协议等默认值。 def test_draft_json_omitted_page_fields_stay_empty(): draft = contract_draft_from_dict( { "supplierName": "示例供应商", "orderDate": "2026-10-01", "sourceType": "YYC", } ) assert draft.contract_category == "" assert draft.settlement_type == "" assert draft.contract_nature == "" assert draft.amount_type == "" assert draft.is_prepayment == "" assert draft.payment_method == "" assert draft.payment_days == "" assert draft.contract_status == "" # 空入参直接失败,不回退 examples 里的测试客商。 def test_load_input_data_rejects_empty(): with pytest.raises(ContractCreateError, match="缺少入参 JSON"): load_input_data("")