test_v7.py
9.91 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""v7:归档/废弃不再让数据消失、画布位置不挡发布、审批优先级由本体里的决策表算。"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
from fastapi import HTTPException
sys.path.insert(0, str(Path(__file__).parent))
from test_platform import build_basics, env, make_action # noqa: E402,F401
def _simple_spec(next_id: str = "end") -> dict:
return {"steps": [
{"id": "q", "type": "function", "title": "取数", "function": "metric.query",
"args": {"metric": "成交金额"}, "output": "amt", "next": next_id},
{"id": "end", "type": "end", "title": "完成", "outcome": "好了"}]}
# ---------------------------------------------------------------- 归档
def test_归档的流程卡还在_筛选得到_能恢复(env):
from app.sop import service
build_basics(env["ds_id"])
d = service.create("要归档的", spec=_simple_spec())
service.archive(d["id"])
assert all(x["id"] != d["id"] for x in service.list_defs()) # 默认列表里不出现
archived = service.list_defs(status="archived")
assert [x["id"] for x in archived] == [d["id"]] # 但筛选得到
back = service.unarchive(d["id"])
assert back["status"] == "draft"
assert any(x["id"] == d["id"] for x in service.list_defs())
with pytest.raises(HTTPException): # 没归档的不能恢复
service.unarchive(d["id"])
def test_被当作子流程引用时_不许直接归档(env):
from app.sop import service
build_basics(env["ds_id"])
child = service.create("子流程", spec=_simple_spec())
service.create("父流程", spec={"steps": [
{"id": "sub", "type": "subprocess", "title": "调子流程", "sop": child["id"], "next": "end"},
{"id": "end", "type": "end", "title": "完成"}]})
assert [r["title"] for r in service.referenced_by(child["id"])] == ["父流程"]
with pytest.raises(HTTPException) as e:
service.archive(child["id"])
assert "父流程" in str(e.value.detail)
def test_已归档的流程卡不能被运行_也不能被当子流程(env):
from app.sop import engine, service, spec as sop_spec
build_basics(env["ds_id"])
child = service.create("子流程", spec=_simple_spec())
service.archive(child["id"])
with pytest.raises(HTTPException) as e:
engine.start(child["id"], {}, mode="dry_run", wait=True)
assert "已归档" in str(e.value.detail)
parent = service.create("父流程", spec={"steps": [
{"id": "sub", "type": "subprocess", "title": "调子流程", "sop": child["id"], "next": "end"},
{"id": "end", "type": "end", "title": "完成"}]})
assert any("已归档" in x for x in service.get(parent["id"])["validation"]["errors"])
def test_归档恢复接口(env):
from fastapi.testclient import TestClient
from app.main import app
from app.sop import service
build_basics(env["ds_id"])
d = service.create("接口归档", spec=_simple_spec())
c = TestClient(app)
assert c.post(f"/api/sop/defs/{d['id']}/archive").status_code == 200
assert c.get("/api/sop/defs", params={"status": "archived"}).json()[0]["id"] == d["id"]
assert c.post(f"/api/sop/defs/{d['id']}/unarchive").status_code == 200
assert c.get(f"/api/sop/defs/{d['id']}").json()["status"] == "draft"
# ---------------------------------------------------------------- 废弃元素
def test_被引用的元素_废弃要先提示_确认后才行(env):
from app.kernel import service
b = build_basics(env["ds_id"])
with pytest.raises(HTTPException) as e:
service.deprecate_concept(b["ot"]["id"]) # 成交金额指标作用于它
assert "引用" in str(e.value.detail)
got = service.deprecate_concept(b["ot"]["id"], force=True)
assert got["status"] == "deprecated"
def test_废弃的元素还在_能恢复成草稿(env):
from app.kernel import service
build_basics(env["ds_id"])
c = service.create_concept("object_type", "临时对象", spec={
"properties": [{"name": "编号", "data_type": "string", "is_key": True}]})
service.deprecate_concept(c["id"])
assert all(x["id"] != c["id"] for x in service.list_concepts())
assert [x["id"] for x in service.list_concepts(status="deprecated")] == [c["id"]]
back = service.update_concept(c["id"], restore=True)
assert back["status"] == "draft"
assert any(x["id"] == c["id"] for x in service.list_concepts())
# ---------------------------------------------------------------- 审批优先级来自决策表
def _decision_action(ot_id: str, table_name: str):
"""建一个带「审批决策函数」的动作:优先级按金额算,而不是按动作的静态风险等级。"""
from app.kernel import service
service.create_concept("function", table_name, spec={
"category": "decision",
"inputs": [{"name": "big", "label": "金额大", "type": "number"}],
"outputs": [{"name": "approver", "label": "审批人"}, {"name": "priority", "label": "优先级"}],
"rows": [{"when": {"big": True}, "then": {"approver": "采购经理", "priority": "P0"}},
{"when": {"big": False}, "then": {"approver": "采购员", "priority": "P3"}}],
"hit_policy": "first", "default": {}})
a = service.create_concept("action", "big_mark", display_name="按金额分级的标记", spec={
"target_type": ot_id,
"params": [{"name": "amount", "label": "金额", "type": "number", "required": True}],
"effects": [{"tool": "object.write", "args": {"object_type": ot_id, "key": "$obj_key",
"property": "跟进状态", "value": "重点跟进"}}],
"approval": {"mode": "manual", "approver_roles": [],
"decision": {"function": table_name,
"inputs": {"big": "= amount >= 50000"}}}})
service.publish("发布决策动作")
return a
def test_审批优先级与审批人由决策表按这一单实际情况算(env):
from app.capability import actions
b = build_basics(env["ds_id"])
_decision_action(b["ot"]["id"], "审批分级")
key = "1"
small = actions.propose("big_mark", {"amount": 3000}, object_key=key, reason="小额")
assert small["priority"] == "P3"
assert small["approvals"][0]["approver_roles"] == ["采购员"]
big = actions.propose("big_mark", {"amount": 90000}, object_key=key, reason="大额")
assert big["priority"] == "P0"
assert big["approvals"][0]["approver_roles"] == ["采购经理"]
# 依据里要留下「凭什么定这个优先级」
assert any("审批分级" in str(e.get("text", "")) or "审批分级" in str(e.get("ref", ""))
for e in big["evidence"])
def test_显式指定的优先级优先于决策表(env):
from app.capability import actions
b = build_basics(env["ds_id"])
_decision_action(b["ot"]["id"], "审批分级2")
got = actions.propose("big_mark", {"amount": 90000}, object_key="1",
reason="人工指定", priority="P2")
assert got["priority"] == "P2"
def test_决策表不可用时_退回动作的风险等级_审批不会卡住(env):
from app.capability import actions
from app.kernel import service
b = build_basics(env["ds_id"])
a = service.create_concept("action", "no_table", display_name="决策表缺失", spec={
"target_type": b["ot"]["id"],
"params": [{"name": "amount", "label": "金额", "type": "number", "required": True}],
"effects": [{"tool": "object.write", "args": {"object_type": b["ot"]["id"], "key": "$obj_key",
"property": "跟进状态", "value": "重点跟进"}}],
"approval": {"mode": "manual", "approver_roles": [],
"decision": {"function": "根本不存在的表",
"inputs": {"big": "= amount >= 1"}}}})
service.publish("发布")
got = actions.propose("no_table", {"amount": 10}, object_key="1", reason="兜底")
assert got["priority"] in ("P1", "P2", "P3") and got["status"] == "pending_approval"
assert a["name"] == "no_table"
# ---------------------------------------------------------------- 流程卡提的审批单要有依据
def test_流程卡提议的动作_带上上游取数作为依据(env):
from app.capability import actions
from app.sop import engine, service
b = build_basics(env["ds_id"])
make_action(b["ot"]["id"])
d = service.create("带依据的流程", spec={"steps": [
{"id": "q", "type": "function", "title": "取数", "function": "metric.query",
"args": {"metric": "成交金额", "dimensions": ["网点"]}, "output": "amt", "next": "act"},
{"id": "act", "type": "action", "title": "标记", "action": "mark_follow",
"params": {"reason": "流程卡发起"}, "object_key": "1", "next": "end"},
{"id": "end", "type": "end", "title": "完成"}]})
from app.governance import review
engine.start(d["id"], {}, mode="dry_run", wait=True)
review.decide(service.submit_publish(d["id"], "发布")["id"], True)
run = engine.start(d["id"], {}, mode="live", wait=True)
step = next(s for s in run["steps"] if s["step_id"] == "act")
ar = actions.get(step["action_run_id"])
assert ar["evidence"], "流程卡提的审批单以前既没有依据也没有优先级"
assert any("流程卡" in str(e.get("text", "")) for e in ar["evidence"])
assert any("amt" in str(e.get("text", "")) for e in ar["evidence"])
# ---------------------------------------------------------------- 角色权限
def test_审批人能处理变更提案_不再是看得见批不了(env):
from app.governance import users
users.invalidate()
a = users.load_actor("approver")
assert a.can("review"), "菜单把「审批与待办」给了审批人,里面就有变更提案"
assert a.can("approve")
assert not a.can("build"), "审批人仍然不能改本体"