59 lines
2.9 KiB
Python
59 lines
2.9 KiB
Python
"""阶段七:重发版本替代与上传后撤销门禁。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import ConflictError
|
|
from app.models import ShippingFreightCancellation, ShippingNotice, ShippingOrderTask, User
|
|
from app.services.shipping_notice.freight_cancellation_service import complete_freight_cancellation
|
|
from app.services.shipping_notice.task_supersession_service import assert_task_not_supersession_blocked, establish_task_supersession
|
|
|
|
|
|
def _tasks(*, uploaded: bool) -> tuple[ShippingNotice, ShippingNotice, User]:
|
|
user = User(username=f'supersession-{uploaded}', email=f'supersession-{uploaded}@example.test', password_hash='x')
|
|
db.session.add(user); db.session.flush()
|
|
old_ot = ShippingOrderTask(task_no=f'OT-SUP-OLD-{uploaded}', user_id=user.id)
|
|
new_ot = ShippingOrderTask(task_no=f'OT-SUP-NEW-{uploaded}', user_id=user.id)
|
|
db.session.add_all([old_ot, new_ot]); db.session.flush()
|
|
old = ShippingNotice(task_no=f'SN-SUP-OLD-{uploaded}', user_id=user.id, order_task_id=old_ot.id)
|
|
new = ShippingNotice(task_no=f'SN-SUP-NEW-{uploaded}', user_id=user.id, order_task_id=new_ot.id)
|
|
db.session.add_all([old, new]); db.session.flush()
|
|
new.supersedes_task_id = old.id
|
|
db.session.commit()
|
|
return old, new, user
|
|
|
|
|
|
def test_unuploaded_resend_voids_old_task_and_keeps_new_active(app):
|
|
"""关键断言:未上传重发立即使旧版失效,库存入口不能继续消费旧版本。"""
|
|
with app.app_context():
|
|
old, new, user = _tasks(uploaded=False)
|
|
establish_task_supersession(old_task=old, new_task=new, actor_id=user.id, reason='customer resend')
|
|
assert old.lifecycle_status == 'voided'
|
|
assert new.lifecycle_status == 'active'
|
|
|
|
|
|
def test_uploaded_resend_requires_external_cancellation_confirmation_before_new_can_continue(app, monkeypatch):
|
|
"""关键断言:已上传旧版须登记外部撤销确认;不要求回执号或文件。"""
|
|
with app.app_context():
|
|
monkeypatch.setattr(
|
|
'app.services.shipping_notice.upload_attempt_service.has_verified_upload_success',
|
|
lambda _task: True,
|
|
)
|
|
old, new, user = _tasks(uploaded=True)
|
|
establish_task_supersession(old_task=old, new_task=new, actor_id=user.id, reason='customer resend')
|
|
row = ShippingFreightCancellation.query.one()
|
|
assert new.lifecycle_status == 'active'
|
|
assert db.session.get(ShippingOrderTask, new.order_task_id).status == 'blocked'
|
|
with pytest.raises(ConflictError):
|
|
assert_task_not_supersession_blocked(new)
|
|
complete_freight_cancellation(
|
|
cancellation_id=row.id,
|
|
actor_id=user.id,
|
|
note='已在货代平台确认撤销',
|
|
)
|
|
assert row.status == 'cancelled'
|
|
assert old.lifecycle_status == 'voided'
|
|
assert new.lifecycle_status == 'active'
|