""" tests/test_line_closure_export.py 用途:验证滚动导出资格对已结案行的剔除(含"有库存的结案行也不导出"边缘场景)。 依赖:pytest、reservation_service、fulfillment_workflow、核验/预约模型、行级履约决定。 逻辑:两行均有可用预约,结案其一后,导出资格集合只保留未结案行。 """ from __future__ import annotations from decimal import Decimal from app import db from app.models import ( ShippingOnhandVerification, ShippingOnhandVerificationLine, ShippingReservation, ) from app.services.shipping_notice.fulfillment_workflow import exportable_snapshot_line_ids from app.services.shipping_notice.reservation_service import current_available_snapshot_line_ids from tests.test_effective_order_view import _seed_frozen_order from tests.test_line_closure_fulfillment import _seed_close_decision def _seed_available_reservations(fixture): """两行各建一条 available 预约 + 一轮 completed 核验。""" verification = ShippingOnhandVerification(task_id=fixture.order.id, status='completed') db.session.add(verification) db.session.flush() for index, snap_line in enumerate(fixture.snap_lines): reservation = ShippingReservation( task_id=fixture.order.id, snapshot_line_id=snap_line.id, reservation_id=f'RSV-CLOSE-{index}', po_number_normalized='PO-CLOSE', part_number=snap_line.part_number, required_qty=Decimal('10'), status='available', ) db.session.add(reservation) db.session.flush() db.session.add(ShippingOnhandVerificationLine( verification_id=verification.id, reservation_id=reservation.id, required_qty=Decimal('10'), available_qty=Decimal('10'), status='available', )) db.session.flush() def test_closed_line_with_inventory_is_not_exportable(app): """结案行即使库存充足、核验 available 也不得进入导出资格集合。""" with app.app_context(): fixture = _seed_frozen_order() _seed_available_reservations(fixture) _seed_close_decision(fixture, 1) _verification, available = current_available_snapshot_line_ids( fixture.order, closed_snapshot_line_ids={int(fixture.snap_lines[1].id)}, ) # 断言:仅未结案的第 0 行可导出。 assert available == {int(fixture.snap_lines[0].id)} def test_exportable_snapshot_line_ids_excludes_closed(app): """滚动导出入口(exportable_snapshot_line_ids)自动剔除结案行。""" with app.app_context(): fixture = _seed_frozen_order() _seed_available_reservations(fixture) _seed_close_decision(fixture, 1) # 断言:导出集合不含结案行(该行预约仍 available,验证 closed 过滤优先)。 assert exportable_snapshot_line_ids(fixture.order) == {int(fixture.snap_lines[0].id)}