386 lines
21 KiB
Python
386 lines
21 KiB
Python
#!/usr/bin/env python3
|
||
"""守「流程编号什么时候排出来」这一格配置:五档时机各跑一遍真流程。
|
||
|
||
编号**长什么样**写在表单那格的 ``autoRule`` 上,**什么时候排**写在流程上
|
||
(``oa_process.serial_config`` 的 ``at`` / ``activity`` / ``when``)。写歪一处就是
|
||
界面与库里各说一套,所以这里不打桩,直接按设计器走的那几条接口建表单、建流程、发布、
|
||
发起、办环节,把六种排号时机各跑一张真单子出来:
|
||
|
||
1. ``start``:发起那一刻就有号,表单那格、``work.serial``、标题里的 ``{单号}`` 三处一致;
|
||
2. ``create``:新建填写页占一个号,提交时凭条换成**同一个号**;同一张凭条交两次、凭条是编的
|
||
都不认(那两张单子另排新号);存草稿也认这张凭条,从这张草稿发起仍是同一个号;
|
||
3. ``draft``:存草稿就占号,发起时**接过来同一个号**(不另占一个),同一条草稿存两遍也只占一次;
|
||
4. ``activity`` + ``enter``:发起时没号,**走到**指定环节那一刻排上,标题里那句 ``{单号}``
|
||
当场补回来(靠的是 ``issue_serial`` 里 ``titles.refresh(force=True)`` 那一道);
|
||
5. ``activity`` + ``leave``:那一环**批完**才排;到环节时还没有号;
|
||
6. ``none``:一路办完都不给号(文控另行登记那一类)。
|
||
|
||
顺带钉四条容易写歪的:退回重走不许多占一个号、指到已不存在的环节保存要被挡、
|
||
排号那一刻在审批记录里留下 ``serial`` 那一条(谁什么时候给这单排的上号)、
|
||
编号时机没配成 ``create`` 的流程不给提前占号(否则等于让人白跳一批号)。
|
||
|
||
跑法(用当前代码,不依赖开发服开没开;建的东西最后自己删干净)::
|
||
|
||
PYTHONPATH=. python3 scripts/check_serial_timing.py
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
|
||
from starlette.testclient import TestClient
|
||
|
||
from app.main import app
|
||
|
||
FORM_NAME = "校验表单-编号时机"
|
||
PROCESS_PREFIX = "校验流程-编号时机"
|
||
AUTO_RULE = {"prefix": "SJ", "date": "YYYYMMDD", "length": 4, "period": "day"}
|
||
|
||
PASSED: list[str] = []
|
||
FAILED: list[str] = []
|
||
|
||
|
||
def check(name: str, condition: bool, detail: object = "") -> bool:
|
||
(PASSED if condition else FAILED).append(name)
|
||
suffix = "" if condition else f" :: {str(detail)[:300]}"
|
||
print(f"[{'PASS' if condition else 'FAIL'}] {name}{suffix}")
|
||
return condition
|
||
|
||
|
||
class Api:
|
||
"""薄薄一层:把 code != 0 与 5xx 都当成"这一趟没跑成",当场停下来说清楚在哪一步。"""
|
||
|
||
def __init__(self, client: TestClient, token: str) -> None:
|
||
self.client = client
|
||
self.token = token
|
||
|
||
def call(self, method: str, path: str, body: dict | None = None) -> dict:
|
||
response = self.client.request(
|
||
method, path, json=body, headers={"Authorization": f"Bearer {self.token}"}
|
||
)
|
||
try:
|
||
payload = response.json()
|
||
except ValueError:
|
||
payload = {"code": response.status_code, "message": response.text[:200]}
|
||
if response.status_code >= 500:
|
||
raise SystemExit(f"{method} {path} 服务端出错:{payload}")
|
||
return payload
|
||
|
||
def must(self, name: str, method: str, path: str, body: dict | None = None):
|
||
payload = self.call(method, path, body)
|
||
if payload.get("code") != 0:
|
||
raise SystemExit(f"{name}失败:{method} {path} -> {payload}")
|
||
return payload.get("data")
|
||
|
||
def ok(self, name: str, method: str, path: str, body: dict | None = None) -> bool:
|
||
payload = self.call(method, path, body)
|
||
if payload.get("code") != 0:
|
||
return True
|
||
print(f" 提示:{name} 竟然成功了 -> {str(payload)[:200]}")
|
||
return False
|
||
|
||
|
||
def title_field() -> dict:
|
||
"""标题写在表单那一格上,规则里带 ``{单号}``——编号晚排时这一句最考验补得齐不齐。"""
|
||
return {
|
||
"key": "subject", "label": "流程标题", "type": "title", "span": 12, "row": 1, "col": 1,
|
||
"titleRule": {"mode": "template", "template": "{单号}|{reason}", "editable": True, "refresh": False},
|
||
}
|
||
|
||
|
||
FIELDS = [
|
||
title_field(),
|
||
{"key": "reason", "label": "申请事由", "type": "text", "required": True, "span": 6, "row": 2, "col": 1},
|
||
{"key": "serial", "label": "单据编号", "type": "auto", "auto": "serial",
|
||
"autoRule": dict(AUTO_RULE), "span": 6, "row": 2, "col": 7},
|
||
]
|
||
|
||
|
||
def activities(form_id: str, pfx: str) -> list[dict]:
|
||
"""环节 id 是**全局主键**:每条流程都得用自己那套前缀,撞了会被后端换号。"""
|
||
return [
|
||
{"id": f"{pfx}_begin", "type": "begin", "name": "开始", "x": 40, "y": 200, "form": form_id},
|
||
{"id": f"{pfx}_draft", "type": "manual", "name": "填写申请", "x": 240, "y": 200, "form": form_id,
|
||
"config": {"mode": "any", "first": True, "taskValues": [{"type": "creator", "data": []}]}},
|
||
# 审批人就是登录人自己,所以显式关掉"发起人自审跳过",这一环才会真的停下来
|
||
{"id": f"{pfx}_first", "type": "manual", "name": "一级审批", "x": 440, "y": 200, "form": form_id,
|
||
"config": {"mode": "any", "taskValues": [{"type": "creator", "data": []}], "skipStarter": False}},
|
||
{"id": f"{pfx}_end", "type": "end", "name": "结束", "x": 640, "y": 200},
|
||
]
|
||
|
||
|
||
def routes(pfx: str) -> list[dict]:
|
||
return [
|
||
{"id": f"{pfx}r1", "name": "提交", "from_activity": f"{pfx}_begin", "to_activity": f"{pfx}_draft"},
|
||
{"id": f"{pfx}r2", "name": "提交审批", "from_activity": f"{pfx}_draft", "to_activity": f"{pfx}_first"},
|
||
{"id": f"{pfx}r3", "name": "同意", "from_activity": f"{pfx}_first", "to_activity": f"{pfx}_end"},
|
||
{"id": f"{pfx}r4", "name": "退回", "from_activity": f"{pfx}_first", "to_activity": f"{pfx}_draft",
|
||
"decision": "goBack"},
|
||
]
|
||
|
||
|
||
def build_process(api: Api, form_id: str, suffix: str, timing: dict, pfx: str = "") -> str:
|
||
row = api.must("建流程", "POST", "/process/processes",
|
||
{"name": f"{PROCESS_PREFIX}-{suffix}", "description": "编号时机自检",
|
||
"serial_config": timing})
|
||
process_id = row["id"]
|
||
api.must("保存设计", "PUT", f"/process/processes/{process_id}/design", {
|
||
"activities": activities(form_id, pfx), "routes": routes(pfx), "latest_form": form_id,
|
||
"canvas": {"edge_style": "orthogonal", "edge_radius": 8},
|
||
"approval_config": {"skipStarter": False, "dedup": "none"},
|
||
})
|
||
problems = (api.call("POST", f"/process/processes/{process_id}/validate").get("data") or {}).get("problems") or []
|
||
check(f"{suffix}:流程通过发布前校验", not problems, problems)
|
||
api.must("发布", "POST", f"/process/processes/{process_id}/publish", {"comment": "编号时机自检"})
|
||
return process_id
|
||
|
||
|
||
def todo_of(api: Api, job: str) -> dict:
|
||
items = (api.call("GET", "/task/processing").get("data") or {}).get("items") or []
|
||
return next((item for item in items if item.get("job") == job), {})
|
||
|
||
|
||
def submit(api: Api, job: str, route: str, data: dict | None = None) -> None:
|
||
task = todo_of(api, job)
|
||
if not task:
|
||
raise SystemExit(f"{job} 手上没有待办,没法走「{route}」")
|
||
api.must(f"办理 {route}", "POST", f"/task/{task['id']}/execute",
|
||
{"route_name": route, "opinion": f"同意({route})", "data": data or {}})
|
||
|
||
|
||
def detail_of(api: Api, job: str) -> dict:
|
||
return api.must("读单据", "GET", f"/job/{job}") or {}
|
||
|
||
|
||
def logs_of(detail: dict) -> list[dict]:
|
||
return list(detail.get("work_logs") or [])
|
||
|
||
|
||
def serial_log(detail: dict) -> dict:
|
||
return next((item for item in logs_of(detail) if item.get("type") == "serial"), {})
|
||
|
||
|
||
def data(job: str) -> dict:
|
||
return {"reason": job, "subject": ""}
|
||
|
||
|
||
def purge(api: Api) -> None:
|
||
"""上一轮跑剩下的流程与在途单先清掉:同名流程留一堆会让下次运行找不到北。"""
|
||
rows = api.must("读流程", "GET", "/process/processes") or []
|
||
for row in rows:
|
||
name = str(row.get("name") or "")
|
||
if not name.startswith(PROCESS_PREFIX):
|
||
continue
|
||
process_id = row["id"]
|
||
for status in ("running", "completed"):
|
||
items = (api.call("GET", "/work/created", {"process": process_id, "status": status})
|
||
.get("data") or {}).get("items") or []
|
||
for item in items:
|
||
api.call("POST", f"/job/{item.get('job') or item.get('id')}/terminate", {"opinion": "自检数据重置"})
|
||
api.call("DELETE", f"/process/processes/{process_id}")
|
||
for row in api.must("读表单", "GET", "/process/forms") or []:
|
||
if row.get("name") == FORM_NAME:
|
||
api.call("DELETE", f"/process/forms/{row['id']}")
|
||
|
||
|
||
def case_start(api: Api, form_id: str) -> None:
|
||
print("\n--- at=start:发起那一刻就有号(出厂口径)---")
|
||
process_id = build_process(api, form_id, "发起", {"at": "start", "activity": "", "when": "leave"}, "ta")
|
||
job = api.must("发起", "POST", "/job", {"process": process_id, "title": "", "data": data("时机-start")})["job"]
|
||
detail = detail_of(api, job)
|
||
serial = str(detail.get("serial") or "")
|
||
check("start:发起回来 work.serial 就有值", bool(serial), detail.get("serial"))
|
||
check("start:表单里那格「单据编号」填的是同一个号", (detail.get("data") or {}).get("serial") == serial,
|
||
(detail.get("data") or {}).get("serial"))
|
||
check("start:标题里的 {单号} 当场就是这一句", serial in str(detail.get("title") or ""), detail.get("title"))
|
||
check("start:发起那一刻不必另留排号留痕(创建那条已经说了这件事)",
|
||
not serial_log(detail), serial_log(detail))
|
||
|
||
|
||
def case_draft(api: Api, form_id: str) -> None:
|
||
print("\n--- at=draft:存草稿就占号,发起时接同一个号 ---")
|
||
process_id = build_process(api, form_id, "草稿", {"at": "draft", "activity": "", "when": "leave"}, "tb")
|
||
draft = api.must("存草稿", "POST", "/draft", {"process": process_id, "title": "", "data": data("时机-draft")})
|
||
reserved = str(draft.get("serial") or "")
|
||
check("draft:草稿自己就带着号", bool(reserved), draft.get("serial"))
|
||
check("draft:草稿里那格「单据编号」也填上了",
|
||
(draft.get("data") or {}).get("serial") == reserved, (draft.get("data") or {}).get("serial"))
|
||
again = api.must("再存一次草稿", "PUT", f"/draft/{draft['id']}",
|
||
{"process": process_id, "title": "", "data": data("时机-draft 改过")})
|
||
check("draft:同一条草稿存第二遍不许多吃一个号", str(again.get("serial") or "") == reserved, again.get("serial"))
|
||
job = api.must("从草稿发起", "POST", "/job",
|
||
{"process": process_id, "title": "", "data": data("时机-draft"), "draft_id": draft["id"]})["job"]
|
||
detail = detail_of(api, job)
|
||
check("draft:发起时接的是草稿占下的那个号(不另占一个)",
|
||
str(detail.get("serial") or "") == reserved, detail.get("serial"))
|
||
|
||
|
||
def case_create(api: Api, form_id: str) -> None:
|
||
print("\n--- at=create:打开填写页就占号,凭条把那个号带到提交/存草稿 ---")
|
||
# 配成别的时机的流程不给占:那一档的代价是白跳号,不能让人在没配的图上吃掉号
|
||
control = build_process(api, form_id, "占号对照", {"at": "start", "activity": "", "when": "leave"}, "tg")
|
||
mismatch = api.call("POST", "/job/serial/reserve", {"process": control})
|
||
check("create:没配这一档的流程不给提前占号",
|
||
mismatch.get("code") != 0 and "SERIAL_TIMING_MISMATCH" in str(mismatch), mismatch)
|
||
|
||
process_id = build_process(api, form_id, "占号", {"at": "create", "activity": "", "when": "leave"}, "tf")
|
||
got = api.must("占号", "POST", "/job/serial/reserve", {"process": process_id})
|
||
reserved = str(got.get("serial") or "")
|
||
ticket = str(got.get("ticket") or "")
|
||
check("create:占号回来带着号与凭条", bool(reserved) and bool(ticket), got)
|
||
check("create:凭条说清了活多久", int(got.get("expiresIn") or 0) > 0, got)
|
||
# 号要当场从计数器取走:连着占两次必须是两个号。只 flush 不提交的话计数器会滚回去,
|
||
# 同一个号发给好几个人,界面上看不出来,撞号才炸。
|
||
held = api.must("再占一个号", "POST", "/job/serial/reserve", {"process": process_id})
|
||
check("create:连着占两次号拿到的是两个号(计数器当场落库)",
|
||
str(held.get("serial") or "") not in ("", reserved), held.get("serial"))
|
||
|
||
# 这一条同时钉住宿的一件事:上面 at=start 那张单子占的号字面上就是 SJ...0001,
|
||
# 本流程占到的也是它——号按「流程 + 周期」各记一本账,凭条只在本流程里查重,
|
||
# 按全局查就会把别人用过的号当成被占过,凭条白白作废。
|
||
job = api.must("带凭条发起", "POST", "/job",
|
||
{"process": process_id, "title": "", "data": data("时机-create"), "serial_ticket": ticket})["job"]
|
||
detail = detail_of(api, job)
|
||
check("create:单子用的就是填写页上那个号", str(detail.get("serial") or "") == reserved, detail.get("serial"))
|
||
check("create:表单里那格「单据编号」是同一个号", (detail.get("data") or {}).get("serial") == reserved,
|
||
(detail.get("data") or {}).get("serial"))
|
||
check("create:标题里的 {单号} 当场就是它", reserved in str(detail.get("title") or ""), detail.get("title"))
|
||
|
||
# 凭条只能落定一次:刷新页面把同一张凭条再交一遍,第二张单子必须另排号
|
||
second = detail_of(api, api.must("同一张凭条再交一次", "POST", "/job",
|
||
{"process": process_id, "title": "", "data": data("时机-create 第二张"),
|
||
"serial_ticket": ticket})["job"])
|
||
check("create:同一张凭条不给两张单子(第二张另排号)",
|
||
str(second.get("serial") or "") not in ("", reserved), second.get("serial"))
|
||
|
||
faked = detail_of(api, api.must("凭条是编的", "POST", "/job",
|
||
{"process": process_id, "title": "", "data": data("时机-create 假凭条"),
|
||
"serial_ticket": "eyJhbGciOiJIUzI1NiJ9.bogus.bogus"})["job"])
|
||
check("create:编出来的凭条不当真,照样正常排号",
|
||
str(faked.get("serial") or "") not in ("", reserved), faked.get("serial"))
|
||
|
||
got2 = held
|
||
held = str(got2.get("serial") or "")
|
||
draft = api.must("带凭条存草稿", "POST", "/draft",
|
||
{"process": process_id, "title": "", "data": data("时机-create 草稿"),
|
||
"serialTicket": got2.get("ticket")})
|
||
check("create:存草稿时凭条换成号,不再另占一个", str(draft.get("serial") or "") == held, draft.get("serial"))
|
||
from_draft = detail_of(api, api.must("从草稿发起", "POST", "/job",
|
||
{"process": process_id, "title": "", "data": data("时机-create 草稿"),
|
||
"draft_id": draft["id"]})["job"])
|
||
check("create:从这张草稿发出去还是同一个号", str(from_draft.get("serial") or "") == held,
|
||
from_draft.get("serial"))
|
||
|
||
|
||
def case_activity(api: Api, form_id: str, when: str, pfx: str) -> None:
|
||
label = "走到就排" if when == "enter" else "批完才排"
|
||
print(f"\n--- at=activity + when={when}:{label} ---")
|
||
process_id = build_process(api, form_id, f"环节{when}",
|
||
{"at": "activity", "activity": f"{pfx}_first", "when": when}, pfx)
|
||
job = api.must("发起", "POST", "/job", {"process": process_id, "title": "", "data": data(f"时机-{when}")})["job"]
|
||
detail = detail_of(api, job)
|
||
check(f"{when}:发起这一刻还没有号", not str(detail.get("serial") or ""), detail.get("serial"))
|
||
check(f"{when}:标题里「单号」那一段先空着,不拿猜的号糊上去",
|
||
"|时机-" in str(detail.get("title") or ""), detail.get("title"))
|
||
|
||
submit(api, job, "提交审批")
|
||
detail = detail_of(api, job)
|
||
serial = str(detail.get("serial") or "")
|
||
if when == "leave":
|
||
check("leave:刚到一级审批还没有号", not serial, serial)
|
||
submit(api, job, "同意")
|
||
detail = detail_of(api, job)
|
||
serial = str(detail.get("serial") or "")
|
||
check(f"{when}:轮到那一环就把号排出来了", bool(serial), detail.get("serial"))
|
||
check(f"{when}:表单里那格「单据编号」跟着填上", (detail.get("data") or {}).get("serial") == serial,
|
||
(detail.get("data") or {}).get("serial"))
|
||
check(f"{when}:标题里缺的那截「单号」补回来了(refresh 关着也补)",
|
||
serial in str(detail.get("title") or ""), detail.get("title"))
|
||
logged = serial_log(detail)
|
||
check(f"{when}:审批记录里留下 serial 那一条", bool(logged), [item.get("type") for item in logs_of(detail)])
|
||
check(f"{when}:那一条写清了排的是哪个号", serial in str(logged.get("from_opinion") or ""), logged)
|
||
|
||
# 把这一环办完再回头看:号还是那一个,留痕也只有一条,重复触发不许多吃号
|
||
if todo_of(api, job):
|
||
submit(api, job, "同意")
|
||
settled = detail_of(api, job)
|
||
check(f"{when}:办完之后再没有第二个号", str(settled.get("serial") or "") == serial, settled.get("serial"))
|
||
check(f"{when}:排号留痕只有一条",
|
||
len([item for item in logs_of(settled) if item.get("type") == "serial"]) == 1,
|
||
[item.get("type") for item in logs_of(settled)])
|
||
|
||
|
||
def case_none(api: Api, form_id: str) -> None:
|
||
print("\n--- at=none:一路办完都不给号 ---")
|
||
process_id = build_process(api, form_id, "不编号", {"at": "none", "activity": "", "when": "leave"}, "tn")
|
||
job = api.must("发起", "POST", "/job", {"process": process_id, "title": "", "data": data("时机-none")})["job"]
|
||
submit(api, job, "提交审批")
|
||
submit(api, job, "同意")
|
||
detail = detail_of(api, job)
|
||
check("none:办结了也还是没有号(文控另行登记那一类)", not str(detail.get("serial") or ""), detail.get("serial"))
|
||
check("none:那格「单据编号」也没被偷偷填上", not (detail.get("data") or {}).get("serial"),
|
||
(detail.get("data") or {}).get("serial"))
|
||
check("none:一条排号留痕都不该有", not serial_log(detail), serial_log(detail))
|
||
check("none:标题照旧算得出来,只是没有号那一段", "时机-none" in str(detail.get("title") or ""),
|
||
detail.get("title"))
|
||
|
||
|
||
def case_guard(api: Api, form_id: str) -> None:
|
||
print("\n--- 保存时把关 ---")
|
||
check("选了「指定环节」却没点环节:保存被挡",
|
||
api.ok("环节没点", "POST", "/process/processes",
|
||
{"name": f"{PROCESS_PREFIX}-挡1", "serial_config": {"at": "activity", "activity": "", "when": "enter"}}))
|
||
process_id = build_process(api, form_id, "把关", {"at": "activity", "activity": "te_first", "when": "enter"}, "te")
|
||
# 把那个环节从画上删掉,但编号时机还指着它:这种单子会一路办完都排不出号
|
||
thin = [item for item in activities(form_id, "te") if item["id"] != "te_first"]
|
||
kept = [item for item in routes("te") if item["from_activity"] != "te_first" and item["to_activity"] != "te_first"]
|
||
kept = kept + [{"id": "ter9", "name": "同意", "from_activity": "te_draft", "to_activity": "te_end"}]
|
||
check("环节被删掉之后时机还指着它:保存被挡",
|
||
api.ok("环节已不存在", "PUT", f"/process/processes/{process_id}/design", {
|
||
"activities": thin, "routes": kept, "latest_form": form_id,
|
||
"serial_config": {"at": "activity", "activity": "t_first", "when": "enter"},
|
||
}))
|
||
# 认不出的时机:读的时候宽容收回默认,写的时候当场说清楚
|
||
check("时机写了不认识的档位:保存被挡",
|
||
api.ok("档位不认识", "PUT", "/process/processes/%s/design" % process_id, {
|
||
"activities": activities(form_id, "te"), "routes": routes("te"), "latest_form": form_id,
|
||
"serial_config": {"at": "whenever", "activity": "", "when": "leave"},
|
||
}))
|
||
api.call("DELETE", f"/process/processes/{process_id}")
|
||
|
||
|
||
def main() -> int:
|
||
with TestClient(app, base_url="http://testserver/api/v1") as client:
|
||
login = client.post("/auth/login", json={"account": "localadmin", "password": "admin123"}).json()
|
||
if login.get("code") != 0:
|
||
raise SystemExit(f"登录失败:{login}")
|
||
api = Api(client, login["data"]["access_token"])
|
||
|
||
print(f"=== 编号时机自检:{PROCESS_PREFIX} ===")
|
||
purge(api)
|
||
form_id = api.must("建表单", "POST", "/process/forms", {
|
||
"name": FORM_NAME, "description": "编号时机自检用",
|
||
"content": {"fields": FIELDS, "layout": {"columns": 12, "grid": 12, "labelWidth": 100}},
|
||
})["id"]
|
||
|
||
case_start(api, form_id)
|
||
case_create(api, form_id)
|
||
case_draft(api, form_id)
|
||
case_activity(api, form_id, "enter", "tc")
|
||
case_activity(api, form_id, "leave", "td")
|
||
case_none(api, form_id)
|
||
case_guard(api, form_id)
|
||
|
||
print("\n--- 收尾:清掉本轮建的东西 ---")
|
||
purge(api)
|
||
api.call("DELETE", f"/process/forms/{form_id}")
|
||
|
||
print(f"\n通过 {len(PASSED)} / 失败 {len(FAILED)}")
|
||
for item in FAILED:
|
||
print(f" FAIL {item}")
|
||
return 1 if FAILED else 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main())
|