test_sop_v6.py
4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""流程卡:逐条处理(foreach)、子流程不能调自己。"""
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent))
from test_platform import build_basics, env, make_action # noqa: E402,F401
def foreach_spec() -> dict:
return {
"steps": [
{"id": "q", "type": "function", "title": "各网点成交金额", "function": "metric.query",
"args": {"metric": "成交金额", "dimensions": ["网点"]}, "output": "amt", "next": "each"},
{"id": "each", "type": "foreach", "title": "逐个网点标记跟进", "items": "$amt",
"action": "mark_follow", "params": {"reason": "成交 $item.value"}, "object_key": "$item.网点",
"limit": 2, "output": "marked", "next": "end"},
{"id": "end", "type": "end", "title": "完成", "outcome": "已逐个提交"}]}
def test_只发一个动作的逐条处理_规整成循环体(env):
from app.sop import spec
each = next(s for s in spec.normalize(foreach_spec())["steps"] if s["id"] == "each")
assert "action" not in each
assert [b["type"] for b in each["body"]["steps"]] == ["action", "end"]
assert each["body"]["steps"][0]["params"] == {"reason": "成交 $item.value"}
def test_逐条处理_试运行预览_正式运行等每一条都办完再往下走(env):
from app.capability import actions
from app.governance import review
from app.sop import engine, service
b = build_basics(env["ds_id"])
make_action(b["ot"]["id"])
d = service.create("逐行标记", spec=foreach_spec())
assert service.get(d["id"])["validation"]["errors"] == []
run = engine.start(d["id"], {}, mode="dry_run", wait=True)
step = next(s for s in run["steps"] if s["step_id"] == "each")
assert run["status"] == "done" and step["output"]["total"] == 2 # limit 生效
assert step["output"]["ok"] == 2 and actions.list_runs() == [] # 试运行只预览
review.decide(service.submit_publish(d["id"])["id"], True)
live = engine.start(d["id"], {}, mode="live", wait=True)
assert live["status"] == "waiting" # 两条都在等审批,流程停在这一步
each = next(s for s in live["steps"] if s["step_id"] == "each")
assert each["status"] == "waiting_child" and each["output"]["pending"] == 2
runs = actions.list_runs()
assert len(runs) == 2 and all(r["status"] == "pending_approval" for r in runs)
assert engine.list_runs(def_id=d["id"], mode="live")[0]["id"] == live["id"] # 行运行不进运行列表
assert len(engine.list_runs(def_id=d["id"], mode="live")) == 1
# 批一条、驳一条:都有了结果,父流程才继续,并汇总出各条的结局
actions.decide(runs[0]["id"], True, "同意")
assert engine.get_run(live["id"])["status"] == "waiting"
actions.decide(runs[1]["id"], False, "这家不用跟")
done = engine.get_run(live["id"], rows=True)
assert done["status"] == "done"
each = next(s for s in done["steps"] if s["step_id"] == "each")
assert (each["output"]["ok"], each["output"]["rejected"], each["output"]["pending"]) == (1, 1, 0)
assert [r["status"] for r in each["rows"]] == ["done", "rejected"] or \
[r["status"] for r in each["rows"]] == ["rejected", "done"]
assert done["vars"]["marked"]["total"] == 2
def test_逐行处理与子流程的校验(env):
from app.sop import service, spec
bad = spec.normalize({"steps": [
{"id": "each", "type": "foreach", "title": "逐行", "action": "", "next": "end"},
{"id": "end", "type": "end"}]})
errs = " ".join(spec.validate(bad)["errors"])
assert "还没有步骤" in errs and "哪份数据" in errs
nested = spec.normalize({"steps": [
{"id": "each", "type": "foreach", "title": "逐行", "items": "$x", "next": "end", "body": {"steps": [
{"id": "sub", "type": "subprocess", "title": "子流程", "sop": "x", "next": "re"},
{"id": "re", "type": "end"}]}},
{"id": "end", "type": "end"}]})
assert "不能放" in " ".join(spec.validate(nested, check_refs=False)["errors"])
d = service.create("自己调自己", spec={"steps": [{"id": "s", "type": "subprocess", "title": "子", "next": "e"},
{"id": "e", "type": "end"}]})
service.update(d["id"], spec={"steps": [{"id": "s", "type": "subprocess", "title": "子", "sop": d["id"], "next": "e"},
{"id": "e", "type": "end"}]})
assert any("调用了流程卡自己" in e for e in service.get(d["id"])["validation"]["errors"])