统一货代发货通知的业务阶段为四个标准阶段:接收解析、订单复核、发货准备、上传完成 设置 ui_state 为前端唯一状态数据源,不再依赖 process_status 驱动业务流程 删除 shipping_notices 表的废弃 process_status 列及对应复合索引,新增数据库迁移脚本 替换全栈代码中所有对 process_status 的引用为生命周期、上传状态等领域字段 更新相关文档、测试用例与前端展示逻辑适配新的状态管理规则
178 lines
7.4 KiB
Python
178 lines
7.4 KiB
Python
"""
|
|
backend/tests/test_shipping_business_numbering.py
|
|
|
|
用途:验证货代业务编号格式、上海日期、两位流水上限及业务单版本继承。
|
|
依赖:ShippingCase/Customer/Sequence、business_numbering_service、customer_identity_gate。
|
|
逻辑:直接调用领域服务,覆盖幂等、01-99、修订共享与强制新建独立编号。
|
|
"""
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ConflictError
|
|
from app.models import ShippingBusinessSequence, ShippingCase, ShippingCustomer, ShippingNotice, ShippingTaskEvent
|
|
from app.services.shipping_notice.business_numbering_service import (
|
|
assign_business_number,
|
|
format_business_no,
|
|
shanghai_business_date,
|
|
)
|
|
from app.services.shipping_notice.customer_identity_gate import confirm_task_customer
|
|
from app.services.shipping_notice.query_service import extract_task_no_from_text, get_task_by_ref
|
|
|
|
|
|
def _seed_case(*, short_code: str = "WYC", task_no: str = "SN-NUMBER-001"):
|
|
"""创建待客户确认的业务单、任务和启用客户。"""
|
|
customer = ShippingCustomer(
|
|
standard_name=f"编号测试客户 {short_code}",
|
|
short_code=short_code,
|
|
status="active",
|
|
)
|
|
case = ShippingCase(case_no=f"CASE-{task_no}", numbering_status="pending")
|
|
db.session.add_all([customer, case])
|
|
db.session.flush()
|
|
task = ShippingNotice(
|
|
task_no=task_no,
|
|
shipping_case_id=case.id,
|
|
customer_resolution_status="pending",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(task)
|
|
db.session.commit()
|
|
return task, case, customer
|
|
|
|
|
|
def test_number_format_and_shanghai_date_boundary(app):
|
|
"""UTC 日期跨日时必须按 Asia/Shanghai 生成 YYMMDD 且不含分隔符。"""
|
|
with app.app_context():
|
|
moment = datetime(2026, 7, 14, 16, 30, tzinfo=timezone.utc)
|
|
business_date = shanghai_business_date(moment)
|
|
assert business_date.isoformat() == "2026-07-15"
|
|
assert format_business_no("WYC", business_date, 1) == "WYC26071501"
|
|
|
|
|
|
def test_confirmation_assigns_idempotent_business_number(app):
|
|
"""重复确认同一客户不得重复占用流水,正式编号保持不变。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
first = confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
first_no = first.shipping_case.business_no
|
|
second = confirm_task_customer(first, customer_id=customer.id, actor_id=None)
|
|
|
|
assert first_no
|
|
assert second.shipping_case.business_no == first_no
|
|
assert ShippingBusinessSequence.query.one().current_value == 1
|
|
assert second.shipping_case.customer_short_code_snapshot == "WYC"
|
|
assert ShippingTaskEvent.query.filter_by(event_type="business_number_assigned").count() == 1
|
|
|
|
|
|
def test_revision_inherits_and_force_case_uses_next_sequence(app):
|
|
"""同一业务单修订继承编号,独立业务单确认同客户时占用下一流水。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
revision = ShippingNotice(
|
|
task_no="SN-NUMBER-REV",
|
|
shipping_case_id=case.id,
|
|
supersedes_task_id=task.id,
|
|
customer_resolution_status="confirmed",
|
|
source_type="manual",
|
|
)
|
|
forced_case = ShippingCase(case_no="CASE-FORCE-NUMBER", numbering_status="pending")
|
|
db.session.add_all([revision, forced_case])
|
|
db.session.flush()
|
|
forced_task = ShippingNotice(
|
|
task_no="SN-NUMBER-FORCED",
|
|
shipping_case_id=forced_case.id,
|
|
customer_resolution_status="pending",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(forced_task)
|
|
db.session.commit()
|
|
|
|
confirm_task_customer(forced_task, customer_id=customer.id, actor_id=None)
|
|
assert revision.shipping_case.business_no == case.business_no
|
|
assert forced_task.shipping_case.business_no[:-2] == case.business_no[:-2]
|
|
assert case.business_no.endswith("01")
|
|
assert forced_task.shipping_case.business_no.endswith("02")
|
|
|
|
|
|
def test_daily_sequence_blocks_one_hundredth_case(app):
|
|
"""客户当日已有 99 笔时,第 100 笔必须阻断且不写业务号。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
business_date = shanghai_business_date()
|
|
db.session.add(ShippingBusinessSequence(
|
|
business_type="shipping_notice",
|
|
customer_id=customer.id,
|
|
business_date=business_date,
|
|
current_value=99,
|
|
))
|
|
db.session.commit()
|
|
case.customer_id = customer.id
|
|
|
|
with pytest.raises(ConflictError) as exc_info:
|
|
assign_business_number(case)
|
|
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_DAILY_LIMIT_REACHED"
|
|
assert case.business_no is None
|
|
assert ShippingBusinessSequence.query.one().current_value == 99
|
|
|
|
|
|
def test_numbering_failure_rolls_back_customer_confirmation(app):
|
|
"""流水耗尽导致确认失败时,业务单客户和任务确认状态不得半落库。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
db.session.add(ShippingBusinessSequence(
|
|
business_type="shipping_notice",
|
|
customer_id=customer.id,
|
|
business_date=shanghai_business_date(),
|
|
current_value=99,
|
|
))
|
|
db.session.commit()
|
|
|
|
with pytest.raises(ConflictError):
|
|
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
db.session.rollback()
|
|
db.session.expire_all()
|
|
persisted_case = db.session.get(ShippingCase, case.id)
|
|
persisted_task = db.session.get(ShippingNotice, task.id)
|
|
assert persisted_case.customer_id is None
|
|
assert persisted_case.business_no is None
|
|
assert persisted_task.customer_resolution_status == "pending"
|
|
|
|
|
|
def test_business_number_query_returns_latest_revision(app):
|
|
"""使用正式业务号查询时应返回同业务单最新修订任务。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
revision = ShippingNotice(
|
|
task_no="SN-NUMBER-QUERY-REV",
|
|
shipping_case_id=case.id,
|
|
supersedes_task_id=task.id,
|
|
document_version=2,
|
|
customer_resolution_status="confirmed",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(revision)
|
|
db.session.commit()
|
|
|
|
resolved = get_task_by_ref(case.business_no)
|
|
assert resolved.id == revision.id
|
|
assert extract_task_no_from_text(f"请继续处理 {case.business_no}") == case.business_no
|
|
|
|
|
|
def test_numbering_rejects_invalid_short_code_even_if_data_bypasses_service(app):
|
|
"""客户简码绕过管理服务写入异常值时,编号服务仍必须阻断。"""
|
|
with app.app_context():
|
|
task, case, customer = _seed_case()
|
|
case.customer_id = customer.id
|
|
customer.short_code = "W1"
|
|
# 数据库 CHECK 已阻断非法落库;no_autoflush 用于单独验证编号服务仍保留纵深校验。
|
|
with db.session.no_autoflush, pytest.raises(ConflictError) as exc_info:
|
|
assign_business_number(case)
|
|
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_SHORT_CODE_INVALID"
|
|
assert case.business_no is None
|
|
db.session.rollback()
|