Files
syops/backend/tests/test_rpa_trigger_consumer.py
T
hugo 65f751daf4 feat(shipping-notice): 收料RPA接入调拨全链路并统一Worker触发目录消费
- 新增 common/trigger_consumer 领取/归档原语,库存与收料 Worker 统一四态目录契约,README 载明契约
- 触发先落单元行再原子写 trigger;假触发器委托真实服务;触发目录不可用单元标 failed
- 回调终态桥接推进调拨来源任务,有推进即 commit 保证持久
- 预检接真实异动文件仓库与 parse_movement_rows,行合成 sheet/row_no 溯源
- 等待回调来源任务超时回收挂每轮排程领取,绝不自动重试
- 三环境补收料共享目录 env,值对齐 §1.9.3 挂载约定
2026-09-18 01:38:29 +08:00

72 lines
2.9 KiB
Python

"""trigger_consumer 公共模块测试。
用途:锁定 trigger 文件四态目录契约(根目录待领取 → processing 处理中 →
processed/failed 终态)的领取与归档行为。
依赖:标准库 tempfile/pathlib、scripts.rpa.common.trigger_consumer。
逻辑:claim 原子性(二次领取返回 None)、归档落位、非法入参拒绝。
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "scripts" / "rpa" / "common"))
from trigger_consumer import archive_trigger, claim_trigger # noqa: E402
def _write_trigger(root: Path, name: str = "batch_1.json") -> Path:
path = root / name
path.write_text('{"schema_version": "x"}', encoding="utf-8")
return path
def test_claim_moves_file_into_processing(tmp_path):
"""首次领取应把 trigger 原子移入 processing/ 并返回新路径。"""
path = _write_trigger(tmp_path)
claimed = claim_trigger(path)
assert claimed == tmp_path / "processing" / path.name
assert claimed.read_text(encoding="utf-8") == '{"schema_version": "x"}'
assert not path.exists()
def test_claim_twice_returns_none(tmp_path):
"""同一 trigger 二次领取必须返回 None(防双消费/重复导出)。"""
path = _write_trigger(tmp_path)
assert claim_trigger(path) is not None
assert claim_trigger(path) is None
def test_archive_processed_and_failed(tmp_path):
"""processing/ 中的文件按 outcome 归档到同名终态目录。"""
path = _write_trigger(tmp_path)
claimed = claim_trigger(path)
done = archive_trigger(claimed, "processed")
assert done == tmp_path / "processed" / path.name and done.exists()
path2 = _write_trigger(tmp_path, "batch_2.json")
claimed2 = claim_trigger(path2)
failed = archive_trigger(claimed2, "failed")
assert failed == tmp_path / "failed" / path2.name and failed.exists()
def test_archive_rejects_bad_outcome_and_unclaimed_path(tmp_path):
"""非法 outcome 与未经领取的路径都必须拒绝,防止归档错位。"""
path = _write_trigger(tmp_path)
with pytest.raises(ValueError):
archive_trigger(path, "processed") # 未领取,不在 processing/ 下
claimed = claim_trigger(_write_trigger(tmp_path, "batch_3.json"))
with pytest.raises(ValueError):
archive_trigger(claimed, "done") # outcome 白名单外
def test_inventory_worker_scans_with_claim(tmp_path):
"""库存 Worker 扫描必须先领取(processing/),不得原地消费根目录文件。"""
root = Path(__file__).resolve().parents[2] / "scripts" / "rpa" / "inventory"
text = (root / "inventory_rpa_worker.py").read_text(encoding="utf-8")
assert "claim_trigger" in text
# 三处 handler 的 failed 目录推导必须基于 processed_dir(根目录),不得再用 trigger_path.parent
assert text.count('trigger_path.parent / "failed"') == 0