清理废弃的ShippingCase相关代码与依赖,统一使用ShippingNotice直接关联客户数据。调整库存网关依赖规则,支持仅对库存满足或已人工放行的订单行生成发货Excel,实现滚动履约逻辑。
210 lines
8.5 KiB
Python
210 lines
8.5 KiB
Python
"""
|
|
backend/tests/test_shipping_business_numbering.py
|
|
|
|
用途:验证货代业务编号格式、上海日期、两位流水上限及 Notice 独立编号。
|
|
依赖:ShippingNotice/Customer/Sequence、business_numbering_service、customer_identity_gate。
|
|
逻辑:直接调用领域服务,覆盖幂等、01-99 与替代 Notice 独立编号。
|
|
"""
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ConflictError
|
|
from app.models import ShippingBusinessSequence, ShippingCustomer, ShippingNotice, ShippingTaskEvent
|
|
from app.services.shipping_notice.business_numbering_service import (
|
|
assign_notice_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_notice(*, short_code: str = "WYC", task_no: str = "SN-NUMBER-001"):
|
|
"""创建待客户确认的 Notice 和启用客户。"""
|
|
customer = ShippingCustomer(
|
|
standard_name=f"编号测试客户 {short_code}",
|
|
short_code=short_code,
|
|
status="active",
|
|
)
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
task = ShippingNotice(
|
|
task_no=task_no,
|
|
customer_resolution_status="pending",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(task)
|
|
db.session.commit()
|
|
return task, 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, customer = _seed_notice()
|
|
first = confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
first_no = first.business_no
|
|
second = confirm_task_customer(first, customer_id=customer.id, actor_id=None)
|
|
|
|
assert first_no
|
|
assert second.business_no == first_no
|
|
assert ShippingBusinessSequence.query.one().current_value == 1
|
|
assert second.customer_short_code_snapshot == "WYC"
|
|
assert ShippingTaskEvent.query.filter_by(event_type="business_number_assigned").count() == 1
|
|
|
|
|
|
def test_each_notice_gets_a_distinct_business_number_for_same_customer(app):
|
|
"""同一客户 PO 的不同发货通知必须各自占用独立业务流水。"""
|
|
with app.app_context():
|
|
customer = ShippingCustomer(
|
|
standard_name='通知单波次客户', short_code='WVC', status='active',
|
|
)
|
|
db.session.add(customer)
|
|
db.session.flush()
|
|
first = ShippingNotice(task_no='SN-NOTICE-NO-1', customer_resolution_status='confirmed')
|
|
second = ShippingNotice(task_no='SN-NOTICE-NO-2', customer_resolution_status='confirmed')
|
|
db.session.add_all([first, second])
|
|
db.session.flush()
|
|
first.customer_id = customer.id
|
|
second.customer_id = customer.id
|
|
db.session.flush()
|
|
|
|
first_no = assign_notice_business_number(first)
|
|
second_no = assign_notice_business_number(second)
|
|
|
|
assert first_no != second_no
|
|
assert first.business_no == first_no
|
|
assert second.business_no == second_no
|
|
|
|
|
|
def test_notice_confirms_customer_and_assigns_number_without_shipping_case(app):
|
|
"""新 Notice 必须自持客户快照与编号,不能要求 ShippingCase 作为前置容器。"""
|
|
with app.app_context():
|
|
customer = ShippingCustomer(
|
|
standard_name='Notice 自持客户', short_code='NHC', status='active',
|
|
)
|
|
task = ShippingNotice(
|
|
task_no='SN-NOTICE-SELF-CUSTOMER',
|
|
customer_resolution_status='pending',
|
|
source_type='manual',
|
|
)
|
|
db.session.add_all([customer, task])
|
|
db.session.commit()
|
|
|
|
confirmed = confirm_task_customer(task, customer_id=customer.id, actor_id=7)
|
|
|
|
assert confirmed.customer_id == customer.id
|
|
assert confirmed.customer_name_snapshot == 'Notice 自持客户'
|
|
assert confirmed.customer_short_code_snapshot == 'NHC'
|
|
assert confirmed.business_no
|
|
assert confirmed.customer_resolution_status == 'confirmed'
|
|
|
|
|
|
def test_replacement_notice_uses_next_sequence(app):
|
|
"""替代 Notice 保留原号审计并为新 Notice 分配下一业务号。"""
|
|
with app.app_context():
|
|
task, customer = _seed_notice()
|
|
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
revision = ShippingNotice(
|
|
task_no="SN-NUMBER-REV",
|
|
supersedes_task_id=task.id,
|
|
customer_resolution_status="pending",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(revision)
|
|
db.session.commit()
|
|
|
|
confirm_task_customer(revision, customer_id=customer.id, actor_id=None)
|
|
assert revision.business_no[:-2] == task.business_no[:-2]
|
|
assert task.business_no.endswith("01")
|
|
assert revision.business_no.endswith("02")
|
|
|
|
|
|
def test_daily_sequence_blocks_one_hundredth_case(app):
|
|
"""客户当日已有 99 笔时,第 100 笔必须阻断且不写业务号。"""
|
|
with app.app_context():
|
|
task, customer = _seed_notice()
|
|
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()
|
|
task.customer_id = customer.id
|
|
|
|
with pytest.raises(ConflictError) as exc_info:
|
|
assign_notice_business_number(task)
|
|
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_DAILY_LIMIT_REACHED"
|
|
assert task.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, customer = _seed_notice()
|
|
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_task = db.session.get(ShippingNotice, task.id)
|
|
assert persisted_task.customer_id is None
|
|
assert persisted_task.business_no is None
|
|
assert persisted_task.customer_resolution_status == "pending"
|
|
|
|
|
|
def test_business_number_query_returns_exact_notice(app):
|
|
"""使用正式业务号查询时必须返回该 Notice,不能跳到替代版本。"""
|
|
with app.app_context():
|
|
task, customer = _seed_notice()
|
|
confirm_task_customer(task, customer_id=customer.id, actor_id=None)
|
|
revision = ShippingNotice(
|
|
task_no="SN-NUMBER-QUERY-REV",
|
|
supersedes_task_id=task.id,
|
|
document_version=2,
|
|
customer_resolution_status="pending",
|
|
source_type="manual",
|
|
)
|
|
db.session.add(revision)
|
|
db.session.commit()
|
|
|
|
resolved = get_task_by_ref(task.business_no)
|
|
assert resolved.id == task.id
|
|
assert extract_task_no_from_text(f"请继续处理 {task.business_no}") == task.business_no
|
|
|
|
|
|
def test_numbering_rejects_invalid_short_code_even_if_data_bypasses_service(app):
|
|
"""客户简码绕过管理服务写入异常值时,编号服务仍必须阻断。"""
|
|
with app.app_context():
|
|
task, customer = _seed_notice()
|
|
task.customer_id = customer.id
|
|
customer.short_code = "W1"
|
|
# 数据库 CHECK 已阻断非法落库;no_autoflush 用于单独验证编号服务仍保留纵深校验。
|
|
with db.session.no_autoflush, pytest.raises(ConflictError) as exc_info:
|
|
assign_notice_business_number(task)
|
|
assert exc_info.value.code == "SHIPPING.BUSINESS_NUMBER_SHORT_CODE_INVALID"
|
|
assert task.business_no is None
|
|
db.session.rollback()
|