""" 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): """真实单票建单路径直接写 Notice 客户事实,不创建 ShippingCase。""" with app.app_context(): customer = _active_customer(short_code='CRT') result = create_task({ 'source_type': 'manual', 'raw_content': 'customer notice', 'customer_id': customer.id, }) task = ShippingNotice.query.filter_by(task_no=result['task_id']).one() assert task.customer_resolution_status == 'confirmed' assert task.customer_id == customer.id assert result['data']['customer_id'] == customer.id def test_create_task_rejects_without_customer(app): """未确认客户时禁止创建 Task,避免落入 customer_review。""" with app.app_context(): with pytest.raises(ConflictError) as exc_info: create_task({'source_type': 'manual', 'raw_content': 'customer notice'}) assert exc_info.value.code == 'SHIPPING.CUSTOMER_REQUIRED_BEFORE_TASK'