""" backend/tests/test_shipping_notice_business_case.py 用途:验证 Notice 建单和替代关系不依赖 ShippingCase。 依赖:app fixture、ShippingNotice、task_workflow。 逻辑:直接调用领域服务并断言 Notice 独立事实,不触发解析和库存副作用。 """ from app import db import pytest from app.core.errors import ConflictError from app.models import ShippingCustomer, ShippingNotice from app.services.shipping_notice.task_workflow import create_task def _task(task_no: str, *, supersedes_task_id: int | None = None) -> ShippingNotice: """构造最小 Notice,供替代关系断言使用。""" return ShippingNotice( task_no=task_no, supersedes_task_id=supersedes_task_id, source_type='manual', ) def _active_customer(*, short_code: str = 'CASE') -> ShippingCustomer: """创建启用标准客户,供建 Task 前客户门禁使用。""" customer = ShippingCustomer(standard_name=f'Test Customer {short_code}', short_code=short_code, status='active') db.session.add(customer) db.session.flush() return customer def test_notices_are_independent_without_business_case(app): """不同 Notice 无需中间聚合实体,业务事实由自身保存。""" with app.app_context(): first = _task('SN-NOTICE-INDEPENDENT-1') second = _task('SN-NOTICE-INDEPENDENT-2') db.session.add_all([first, second]) db.session.commit() assert first.id != second.id assert first.business_no is None assert second.business_no is None def test_replacement_links_to_parent_notice(app): """替代 Notice 仅链接原 Notice,不继承或依赖 Case。""" with app.app_context(): parent = _task('SN-NOTICE-PARENT-1') db.session.add(parent) db.session.flush() replacement = _task('SN-NOTICE-REPLACEMENT-1', supersedes_task_id=parent.id) db.session.add(replacement) db.session.commit() assert replacement.supersedes_task_id == parent.id def test_create_task_assigns_notice_customer_and_serializes_it(app): """真实单票建单直接写分立的客户识别结果,不依赖 ShippingCase;OT 建单本身不阻断。""" from app.models import ShippingOrderTask, User from app.services.shipping_notice.customer_resolution_service import customer_resolution_facts from app.services.shipping_notice.query_service import serialize_task_for_tool from tests.conftest import confirm_test_customer_resolution with app.app_context(): customer = _active_customer(short_code='CRT') user = User(username='create-task-crt', email='create-task-crt@example.test', password_hash='x') db.session.add(user) db.session.flush() result = create_task({ 'source_type': 'direct_pdf', 'raw_content': 'customer notice', 'user_id': user.id, }) task = ShippingOrderTask.query.filter_by(task_no=result['task_id']).one() # [FIX] 客户确认由独立识别结果承载;建单不再阻塞(见 create_task 迁移注释)。 confirm_test_customer_resolution(task, customer=customer) facts = customer_resolution_facts(task) assert facts['status'] == 'confirmed' assert facts['customer_id'] == customer.id assert serialize_task_for_tool(task)['customer_id'] == customer.id def test_create_task_defers_customer_confirmation(app): """未确认客户时不再阻断建单;客户确认交由订单复核/客户门禁处理。""" from app.models import ShippingOrderTask, User from app.services.shipping_notice.customer_resolution_service import customer_resolution_facts with app.app_context(): user = User(username='create-task-nocust', email='create-task-nocust@example.test', password_hash='x') db.session.add(user) db.session.flush() result = create_task({'source_type': 'direct_pdf', 'raw_content': 'customer notice', 'user_id': user.id}) task = ShippingOrderTask.query.filter_by(task_no=result['task_id']).one() assert task is not None assert customer_resolution_facts(task)['status'] != 'confirmed'