153 lines
6.1 KiB
Python
153 lines
6.1 KiB
Python
"""
|
|
backend/tests/test_shipping_notice_business_case.py
|
|
|
|
用途:验证货代业务单聚合在普通建单、修订和强制新建场景下的归属规则。
|
|
依赖:app fixture、ShippingTask/ShippingCase、business_case_service。
|
|
逻辑:直接调用领域服务并断言业务单关系,不触发解析和库存副作用。
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ConflictError
|
|
from app.models import ShippingCase, ShippingTask
|
|
from app.services.shipping_notice.business_case_service import resolve_shipping_case_for_new_task
|
|
from app.services.shipping_notice.task_workflow import create_batch_tasks, create_task
|
|
|
|
|
|
def _task(task_no: str, *, case_id: int | None = None, supersedes_task_id: int | None = None) -> ShippingTask:
|
|
"""构造最小货代任务,供业务单关系断言使用。"""
|
|
return ShippingTask(
|
|
task_no=task_no,
|
|
shipping_case_id=case_id,
|
|
supersedes_task_id=supersedes_task_id,
|
|
source_type='manual',
|
|
process_status='created',
|
|
)
|
|
|
|
|
|
def test_normal_and_force_create_use_different_cases(app):
|
|
"""普通建单和强制新建必须各自创建独立业务单。"""
|
|
with app.app_context():
|
|
normal = resolve_shipping_case_for_new_task(created_by_id=None)
|
|
forced = resolve_shipping_case_for_new_task(
|
|
duplicate_resolution_type='force_create',
|
|
created_by_id=None,
|
|
)
|
|
assert normal.id != forced.id
|
|
assert normal.business_no is None
|
|
assert forced.business_no is None
|
|
|
|
|
|
def test_revision_inherits_existing_parent_case(app):
|
|
"""修订任务必须继承父任务已有业务单,不能创建新业务单。"""
|
|
with app.app_context():
|
|
case = resolve_shipping_case_for_new_task(created_by_id=None)
|
|
parent = _task('SN-CASE-PARENT-1', case_id=case.id)
|
|
db.session.add(parent)
|
|
db.session.flush()
|
|
|
|
resolved = resolve_shipping_case_for_new_task(
|
|
supersedes_task_id=parent.id,
|
|
duplicate_resolution_type='revision',
|
|
)
|
|
assert resolved.id == case.id
|
|
assert ShippingCase.query.count() == 1
|
|
|
|
|
|
def test_revision_rejects_parent_without_business_case(app):
|
|
"""修订父任务缺少业务单时必须阻断,禁止隐式补建。"""
|
|
with app.app_context():
|
|
parent = _task('SN-CASE-LEGACY-1')
|
|
db.session.add(parent)
|
|
db.session.flush()
|
|
|
|
with pytest.raises(ConflictError) as exc_info:
|
|
resolve_shipping_case_for_new_task(
|
|
supersedes_task_id=parent.id,
|
|
duplicate_resolution_type='revision',
|
|
)
|
|
assert exc_info.value.code == 'SHIPPING_NOTICE.REVISION_CASE_MISSING'
|
|
assert ShippingCase.query.count() == 0
|
|
|
|
|
|
def test_revision_chain_reuses_root_case(app):
|
|
"""连续修订版本必须沿用同一业务单聚合。"""
|
|
with app.app_context():
|
|
case = resolve_shipping_case_for_new_task(created_by_id=None)
|
|
parent = _task('SN-CASE-CHAIN-1', case_id=case.id)
|
|
db.session.add(parent)
|
|
db.session.flush()
|
|
revision_case = resolve_shipping_case_for_new_task(
|
|
supersedes_task_id=parent.id,
|
|
duplicate_resolution_type='revision',
|
|
)
|
|
revision = _task('SN-CASE-CHAIN-2', case_id=revision_case.id, supersedes_task_id=parent.id)
|
|
db.session.add(revision)
|
|
db.session.flush()
|
|
|
|
next_case = resolve_shipping_case_for_new_task(
|
|
supersedes_task_id=revision.id,
|
|
duplicate_resolution_type='revision',
|
|
)
|
|
assert next_case.id == case.id
|
|
assert ShippingCase.query.count() == 1
|
|
|
|
|
|
def test_create_task_assigns_case_and_serializes_it(app):
|
|
"""真实单票建单路径必须创建业务单并在 Tool 投影中返回关联信息。"""
|
|
with app.app_context():
|
|
result = create_task({'source_type': 'manual', 'raw_content': 'customer notice'})
|
|
task = ShippingTask.query.filter_by(task_no=result['task_id']).one()
|
|
assert task.shipping_case_id is not None
|
|
assert result['data']['shipping_case_id'] == task.shipping_case_id
|
|
assert result['data']['case_no'] == task.shipping_case.case_no
|
|
assert result['data']['business_no'] is None
|
|
|
|
|
|
def test_batch_revision_inherits_parent_case(app):
|
|
"""真实批量建单路径中的 revision 必须继承父任务业务单。"""
|
|
with app.app_context():
|
|
case = resolve_shipping_case_for_new_task(created_by_id=None)
|
|
parent = _task('SN-CASE-BATCH-PARENT', case_id=case.id)
|
|
db.session.add(parent)
|
|
db.session.commit()
|
|
|
|
result = create_batch_tasks({
|
|
'notice_items': [{
|
|
'source_type': 'manual',
|
|
'raw_text': 'revision notice',
|
|
'document_version': 2,
|
|
'supersedes_task_id': parent.id,
|
|
'duplicate_resolution_type': 'revision',
|
|
}],
|
|
'defer_parse': True,
|
|
})
|
|
task = ShippingTask.query.filter_by(task_no=result['data']['tasks'][0]['task_id']).one()
|
|
assert task.shipping_case_id == case.id
|
|
assert task.supersedes_task_id == parent.id
|
|
assert ShippingCase.query.count() == 1
|
|
|
|
|
|
def test_batch_force_create_uses_new_case(app):
|
|
"""真实批量建单路径中的 force_create 必须创建独立业务单。"""
|
|
with app.app_context():
|
|
original_case = resolve_shipping_case_for_new_task(created_by_id=None)
|
|
original = _task('SN-CASE-FORCE-SOURCE', case_id=original_case.id)
|
|
db.session.add(original)
|
|
db.session.commit()
|
|
|
|
result = create_batch_tasks({
|
|
'notice_items': [{
|
|
'source_type': 'manual',
|
|
'raw_text': 'forced notice',
|
|
'duplicate_source_task_id': original.id,
|
|
'duplicate_resolution_type': 'force_create',
|
|
}],
|
|
'defer_parse': True,
|
|
})
|
|
task = ShippingTask.query.filter_by(task_no=result['data']['tasks'][0]['task_id']).one()
|
|
assert task.shipping_case_id != original_case.id
|
|
assert task.duplicate_source_task_id == original.id
|
|
assert ShippingCase.query.count() == 2
|