""" tests/test_shipping_notice_creation_effective.py 用途:验证正式通知行物化携带冻结后修正值,且整单建通知排除已结案行。 依赖:pytest、shipping_notice_creation_service、notice_line_service、行级履约决定模型。 逻辑:库存门禁通过(或行豁免)后建 Notice,断言 NoticeLine 的料号/数量来自有效视图, 结案行既不进门禁目标集也不物化为通知行。 """ from __future__ import annotations import pytest from app import db from app.core.errors import ConflictError from app.models import ( ShippingCustomer, ShippingNoticeLine, ShippingNoticeLineOnhandMatch, ) from app.services.shipping_notice.shipping_notice_creation_service import ( create_shipping_notice_after_inventory_gate, ) from app.services.shipping_notice.task_order_field_editor import replace_current_line_field_value from tests.test_effective_order_view import _seed_frozen_order from tests.test_line_closure_fulfillment import _seed_close_decision def _seed_customer(snapshot): """给快照挂一个 active 客户(建 Notice 门禁必填)。""" customer = ShippingCustomer(standard_name='有效视图客户', short_code='EFFC', status='active') db.session.add(customer) db.session.flush() snapshot.customer_id = customer.id db.session.flush() def _seed_match_row(order, snap_line, *, match_result='matched', shortage='0'): """给冻结行补一条当前匹配事实(matched+ready 表示门禁可过)。""" db.session.add(ShippingNoticeLineOnhandMatch( task_id=order.id, snapshot_line_id=snap_line.id, line_no=snap_line.line_no, line_index=snap_line.position, match_result=match_result, onhand_status='ready', required_qty='10', available_qty='10', shortage_qty=shortage, reservation_id=snap_line.reservation_id, part_number=snap_line.part_number, is_current=True, )) db.session.flush() def test_notice_lines_carry_corrected_part_number(app): """修正后的料号必须进入正式通知行(通知行会被 Excel/货代消费)。""" with app.app_context(): fixture = _seed_frozen_order() _seed_customer(fixture.snapshot) replace_current_line_field_value( task=fixture.order, position=0, field_code='part_number', value='D4-A08AR12000002', ) _seed_match_row(fixture.order, fixture.snap_lines[0]) # 注:brief 原文为整单调用,但夹具第二行无匹配事实会触发门禁 ConflictError(经控制器确认), # 改为仅选中第 0 行——与 Step 2 预期断言差异(单行)一致。 notice = create_shipping_notice_after_inventory_gate( fixture.order, snapshot_line_ids={int(fixture.snap_lines[0].id)}, ) rows = ShippingNoticeLine.query.filter_by(notice_id=notice.id).all() # 断言:通知行料号是修正值,而不是快照冻结的带尾巴旧值。 assert [row.part_number for row in rows] == ['D4-A08AR12000002'] def test_notice_creation_skips_closed_line(app): """一行 matched + 一行结案:整单建通知成功且通知行不含结案行。""" with app.app_context(): fixture = _seed_frozen_order() _seed_customer(fixture.snapshot) _seed_match_row(fixture.order, fixture.snap_lines[0]) _seed_match_row( fixture.order, fixture.snap_lines[1], match_result='not_found', shortage='10', ) _seed_close_decision(fixture, 1) notice = create_shipping_notice_after_inventory_gate(fixture.order) rows = ShippingNoticeLine.query.filter_by(notice_id=notice.id).all() # 断言:只有第 0 行进入通知;结案行既过门禁又被物化排除。 assert [row.snapshot_line_id for row in rows] == [int(fixture.snap_lines[0].id)] def test_whole_order_notice_rejected_when_all_lines_closed(app): """全部行结案=无可履约内容:整单建 Notice 必须按门禁未通过拒绝,防止建空通知。""" with app.app_context(): fixture = _seed_frozen_order() _seed_customer(fixture.snapshot) _seed_match_row(fixture.order, fixture.snap_lines[0]) _seed_match_row( fixture.order, fixture.snap_lines[1], match_result='not_found', shortage='10', ) _seed_close_decision(fixture, 1, version=1) _seed_close_decision(fixture, 0, version=1) # 断言:结案剔除后目标行集为空,整单调用走门禁未通过分支(与 match_completed 口径一致)。 with pytest.raises(ConflictError) as exc_info: create_shipping_notice_after_inventory_gate(fixture.order) assert exc_info.value.code == 'SHIPPING.ONHAND_GATE_NOT_PASSED' assert 'SHIPPING.ONHAND_GATE_NOT_PASSED' in repr(exc_info.value)