test(shipping): 更新订单复核库存与通知流程测试

This commit is contained in:
hugo committed 2026-09-04 20:53:06 +08:00
1 parent 8ec028e6a3
commit 265a6e179a
168 files changed
+9420 -4613

No files matched your search

@@ -8,15 +8,22 @@ tests/test_shipping_notice_state_machine.py
from types import SimpleNamespace
from app.models import ShippingNotice
from app.models import ShippingOrderTask
from app.services.shipping_notice.constants import (
CP_EXPORT_READY,
CP_PARSE_REVIEW,
CP_READY_EXPORT,
CP_UPLOADED,
PH_LOW_CONF,
RS_PARSE_REVIEW,
)
from app.services.shipping_notice.state_machine import CheckpointKind, checkpoint_kind_for, resolve_task_ui_state, validate_registry
from app.services.shipping_notice.state_machine import (
CheckpointKind,
checkpoint_kind_for,
resolve_task_ui_state,
resolve_task_ui_state_from_fields,
validate_registry,
)
def test_validate_registry_has_no_errors() -> None:
@@ -34,13 +41,21 @@ def test_checkpoint_kinds_for_stp_stop_points() -> None:
def test_resolve_task_ui_state_parse_review(app) -> None:
"""pending_review + parse_review 应映射为 must_human / parse_review。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-SM-PARSE",
task = ShippingOrderTask(
task_no="OT-SM-PARSE",
user_id=1,
review_status=RS_PARSE_REVIEW,
pending_human_reason=PH_LOW_CONF,
source_type="manual",
)
ui = resolve_task_ui_state(task)
ui = resolve_task_ui_state_from_fields(
review_status=task.review_status,
pending_human_reason=task.pending_human_reason,
customer_resolution_status=None,
order_snapshot_id=None,
match_status=None,
export_file_path=None,
has_dynamic_order_lines=False,
)
assert ui.checkpoint == CP_PARSE_REVIEW
assert ui.checkpoint_kind == CheckpointKind.MUST_HUMAN
@@ -48,32 +63,44 @@ def test_resolve_task_ui_state_parse_review(app) -> None:
def test_resolve_task_ui_state_export_ready(app) -> None:
"""matched 且已有 export_file_path 应为 wait_manual_upload(待人工上传登记)。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-SM-EXPORT",
source_type="manual",
ui = resolve_task_ui_state_from_fields(
review_status=None,
pending_human_reason=None,
customer_resolution_status=None,
order_snapshot_id=1,
match_status=None,
export_file_path="/tmp/shipping-export.xlsx",
export_file_name="shipping-export.xlsx",
has_dynamic_order_lines=False,
)
ui = resolve_task_ui_state(task)
assert ui.checkpoint == CP_EXPORT_READY
assert ui.checkpoint_kind == CheckpointKind.WAIT_MANUAL_UPLOAD
assert not hasattr(ui, 'human_options')
def test_resolve_task_ui_state_uploaded_complete(app) -> None:
def test_resolve_task_ui_state_uploaded_complete(app, monkeypatch) -> None:
"""upload_status=uploaded 应为 complete。"""
with app.app_context():
task = ShippingNotice(
task_no="SN-SM-UPLOADED",
upload_status="uploaded",
source_type="manual",
task = SimpleNamespace(upload_status="uploaded", lifecycle_status="active")
# [FIX] 上传完成由可审计事实 has_verified_upload_success 裁决,不再读取 upload_status 字段。
monkeypatch.setattr(
'app.services.shipping_notice.upload_attempt_service.has_verified_upload_success',
lambda _task: True,
)
ui = resolve_task_ui_state_from_fields(
review_status=None,
pending_human_reason=None,
customer_resolution_status=None,
order_snapshot_id=1,
match_status=None,
export_file_path=None,
has_dynamic_order_lines=False,
upload_req_task=task,
)
ui = resolve_task_ui_state(task)
assert ui.checkpoint == CP_UPLOADED
assert ui.checkpoint_kind == CheckpointKind.COMPLETE
def test_resolve_with_mocked_simple_namespace_attributes() -> None:
def test_resolve_with_mocked_simple_namespace_attributes(monkeypatch) -> None:
"""无 DB 会话时可用简易命名空间验证 uploaded 终态分支。"""
task = SimpleNamespace(
upload_status="uploaded",
@@ -87,6 +114,27 @@ def test_resolve_with_mocked_simple_namespace_attributes() -> None:
current_order_extraction_id=None,
step_payload_json=None,
)
# [FIX] 上传完成由可审计事实 has_verified_upload_success 裁决,不再读取 upload_status 字段。
monkeypatch.setattr(
'app.services.shipping_notice.upload_attempt_service.has_verified_upload_success',
lambda _task: True,
)
ui = resolve_task_ui_state(task) # type: ignore[arg-type]
assert ui.checkpoint_kind == CheckpointKind.COMPLETE
assert ui.checkpoint == CP_UPLOADED
def test_order_task_inventory_checkpoint_uses_structured_facts(monkeypatch) -> None:
"""OT 库存 JSON 为空时,状态机仍须按 Link/File/Match 事实进入导出准备。"""
from app.models import ShippingOrderTask
from app.services.shipping_notice import state_machine
task = ShippingOrderTask(task_no='OT-SM-STRUCTURED', user_id=1, stage='inventory_matching')
monkeypatch.setattr(state_machine, '_order_task_inventory_facts', lambda _task: {
'inventory_ready': True,
'match_complete': True,
'match_sufficient': True,
'has_shortage': False,
})
assert state_machine.resolve_task_ui_state(task).checkpoint == CP_READY_EXPORT