把 shipping 侧 inventory/receipt 词根迁到 onhand/inbound,29 张表与 枚举/权限/前端/i18n 同步改名;在手与入站报表配置及文件改由 freight_rpa 持有,发货与调拨仅按 source_code 消费;ORM 类加 FreightRpa 前缀, 上传动作改为 freight_rpa_upload。含类名与上传动作改名规格文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
167 lines
6.9 KiB
Python
167 lines
6.9 KiB
Python
"""inventory 只读轮询(A1)单测。
|
||
|
||
覆盖 ensure_all_required_sources_prepared 的 allow_auto_retry 开关:
|
||
1. allow_auto_retry=False(poll_onhand_preparation 查询路径):
|
||
RPA Attempt failed / 从未触发 → 只上报 failed,禁止自动新建 Attempt/Trigger。
|
||
2. allow_auto_retry=False + queued → 仍视为 pending(轮询语义保持)。
|
||
3. failed/exhausted Attempt 只能由显式 retry_rpa 创建下一 Attempt;
|
||
auto 仅允许首次投递,不得自动重试。
|
||
"""
|
||
|
||
from types import SimpleNamespace
|
||
|
||
import pytest
|
||
|
||
from app import db
|
||
from app.models import ShippingNotice, FreightRpaOnhandTaskAttempt
|
||
from app.services.shipping_notice import per_source_rpa_service
|
||
|
||
|
||
def _seed_attempt(task_no: str, *, status: str, attempt_no: int = 1) -> FreightRpaOnhandTaskAttempt:
|
||
"""在测试库写入一条真实逐源 Attempt,供 _latest_attempt / FOR UPDATE 复检读取。"""
|
||
att = FreightRpaOnhandTaskAttempt(
|
||
task_no=task_no,
|
||
source_code='SRC_A',
|
||
attempt_no=attempt_no,
|
||
source_run_no=f'RUN-{task_no}-{attempt_no}',
|
||
status=status,
|
||
triggered_at=db.session.query(db.func.now()).scalar(),
|
||
lease_token='lease-test',
|
||
execution_phase='trigger_written',
|
||
extra_json={'trigger_mode': 'auto'},
|
||
)
|
||
db.session.add(att)
|
||
db.session.commit()
|
||
return att
|
||
|
||
|
||
@pytest.fixture()
|
||
def poll_task(app):
|
||
"""构造一个极简 ShippingNotice 行,task_no 仅供 Attempt 归属查询使用。"""
|
||
with app.app_context():
|
||
task = ShippingNotice(task_no='SN-POLL-READ-ONLY')
|
||
db.session.add(task)
|
||
db.session.commit()
|
||
yield task
|
||
|
||
|
||
def _patch_common(monkeypatch, *, snapshot_ready=False):
|
||
"""统一 monkeypatch:单候选源 + 快照未就绪;Attempt 走真实表。"""
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'task_frozen_onhand_source_codes', lambda task: ['SRC_A']
|
||
)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, '_snapshot_ready_for_source', lambda *args, **kwargs: snapshot_ready
|
||
)
|
||
|
||
|
||
def test_poll_read_only_reports_failed_without_new_attempt(monkeypatch, app, poll_task):
|
||
"""[断言] 只读轮询遇 failed Attempt:源进入 failed 列表、不新建 Attempt、不写 trigger。"""
|
||
_patch_common(monkeypatch)
|
||
_seed_attempt(poll_task.task_no, status='failed')
|
||
|
||
def _forbidden_batch_writer(*args, **kwargs):
|
||
raise AssertionError('只读轮询不得写入 RPA trigger 文件')
|
||
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'write_inventory_export_batch_trigger', _forbidden_batch_writer
|
||
)
|
||
|
||
result = per_source_rpa_service.ensure_all_required_sources_prepared(
|
||
poll_task, mode='auto', allow_auto_retry=False
|
||
)
|
||
|
||
# 断言:failed 事实被上报,且没有任何新 Attempt 落库
|
||
assert result.failed_source_codes == ['SRC_A']
|
||
assert result.pending_source_codes == []
|
||
assert result.prepared_source_codes == []
|
||
assert FreightRpaOnhandTaskAttempt.query.filter_by(task_no=poll_task.task_no).count() == 1
|
||
|
||
|
||
def test_poll_read_only_keeps_pending_for_active_attempt(monkeypatch, app, poll_task):
|
||
"""[断言] 只读轮询遇 queued Attempt:维持 pending 轮询语义,不新建 Attempt。"""
|
||
_patch_common(monkeypatch)
|
||
_seed_attempt(poll_task.task_no, status='queued')
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'write_inventory_export_batch_trigger',
|
||
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('不得写 trigger')),
|
||
)
|
||
|
||
result = per_source_rpa_service.ensure_all_required_sources_prepared(
|
||
poll_task, mode='auto', allow_auto_retry=False
|
||
)
|
||
|
||
assert result.pending_source_codes == ['SRC_A']
|
||
assert result.failed_source_codes == []
|
||
assert FreightRpaOnhandTaskAttempt.query.filter_by(task_no=poll_task.task_no).count() == 1
|
||
|
||
|
||
def test_poll_read_only_reports_failed_when_never_attempted(monkeypatch, app, poll_task):
|
||
"""[断言] 只读轮询遇从未触发的源:不代为首次触发,按 failed 上报交由用户决策。"""
|
||
_patch_common(monkeypatch)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'write_inventory_export_batch_trigger',
|
||
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('不得写 trigger')),
|
||
)
|
||
|
||
result = per_source_rpa_service.ensure_all_required_sources_prepared(
|
||
poll_task, mode='auto', allow_auto_retry=False
|
||
)
|
||
|
||
assert result.failed_source_codes == ['SRC_A']
|
||
assert FreightRpaOnhandTaskAttempt.query.filter_by(task_no=poll_task.task_no).count() == 0
|
||
|
||
|
||
@pytest.mark.parametrize('status', ['failed', 'exhausted'])
|
||
def test_auto_does_not_retry_terminal_attempt(monkeypatch, app, poll_task, status):
|
||
"""[断言] auto 遇 failed/exhausted Attempt 只上报失败,绝不写入下一次 Attempt/Trigger。"""
|
||
_patch_common(monkeypatch)
|
||
_seed_attempt(poll_task.task_no, status=status)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'write_inventory_export_batch_trigger',
|
||
lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError('auto 不得重试 RPA')),
|
||
)
|
||
|
||
result = per_source_rpa_service.ensure_all_required_sources_prepared(poll_task, mode='auto')
|
||
|
||
assert result.failed_source_codes == ['SRC_A']
|
||
assert result.pending_source_codes == []
|
||
assert FreightRpaOnhandTaskAttempt.query.filter_by(task_no=poll_task.task_no).count() == 1
|
||
|
||
|
||
def test_explicit_retry_rpa_creates_next_attempt(monkeypatch, app, poll_task):
|
||
"""[断言] 只有用户显式 retry_rpa 能把 failed Attempt 投递为下一次 queued Attempt。
|
||
|
||
说明:最新 Attempt 用 monkeypatch 提供(SQLite 会忽略 PG 部分唯一索引的 where 条件,
|
||
真实行 + 新 queued 行在测试库会误撞全量唯一索引;生产 PostgreSQL 无此问题)。"""
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'task_frozen_onhand_source_codes', lambda task: ['SRC_A']
|
||
)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, '_snapshot_ready_for_source', lambda *args, **kwargs: False
|
||
)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service,
|
||
'_latest_attempt',
|
||
lambda *args, **kwargs: SimpleNamespace(
|
||
id=1, status='failed', attempt_no=1, run_no='RUN-FAILED-1',
|
||
error_code=None, error_message=None,
|
||
),
|
||
)
|
||
monkeypatch.setattr(
|
||
'app.services.shipping_notice.onhand_source_rules.task_rpa_inventory_source',
|
||
lambda task: {'source_code': 'SRC_A'},
|
||
)
|
||
monkeypatch.setattr(
|
||
per_source_rpa_service, 'write_inventory_export_batch_trigger',
|
||
lambda run_no, sources, payload=None: 'trigger/batch.json',
|
||
)
|
||
|
||
result = per_source_rpa_service.ensure_all_required_sources_prepared(poll_task, mode='retry_rpa')
|
||
|
||
assert result.pending_source_codes == ['SRC_A']
|
||
attempts = FreightRpaOnhandTaskAttempt.query.filter_by(task_no=poll_task.task_no).all()
|
||
assert len(attempts) == 1
|
||
assert attempts[0].status == 'queued'
|
||
assert attempts[0].attempt_no == 1
|