test_oa_flow_preview.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. """OA 发起预览:条件字段门闩与单路 walk"""
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from app.services.oa_engine import (
  5. INITIATOR_CONDITION_FIELD,
  6. _FLOW_PREVIEW_PENDING_MSG,
  7. _collect_condition_fields,
  8. _condition_fields_ready,
  9. _form_value_present,
  10. _iter_resolved_path_nodes,
  11. _walk_preview_nodes,
  12. )
  13. def _cond_flow():
  14. return {
  15. "id": "s",
  16. "type": "start",
  17. "name": "发起人",
  18. "child": {
  19. "id": "a0",
  20. "type": "approval",
  21. "name": "一级审批",
  22. "approverConfig": {"assigneeType": "users", "userIds": ["u1"], "signMode": "or"},
  23. "child": {
  24. "id": "c1",
  25. "type": "condition",
  26. "conditionGroups": [
  27. {
  28. "name": "条件1",
  29. "rules": [{"field": "days", "op": ">", "value": 3}],
  30. "child": {
  31. "id": "a1",
  32. "type": "approval",
  33. "name": "董事长审批",
  34. "approverConfig": {
  35. "assigneeType": "users",
  36. "userIds": ["u2"],
  37. "signMode": "or",
  38. },
  39. "child": {"id": "e1", "type": "end", "name": "结束"},
  40. },
  41. },
  42. {
  43. "name": "默认",
  44. "isDefault": True,
  45. "rules": [],
  46. "child": {"id": "e2", "type": "end", "name": "结束"},
  47. },
  48. ],
  49. },
  50. },
  51. }
  52. class TestFormValuePresent(unittest.TestCase):
  53. def test_empty(self):
  54. self.assertFalse(_form_value_present({}, "days"))
  55. self.assertFalse(_form_value_present({"days": None}, "days"))
  56. self.assertFalse(_form_value_present({"days": ""}, "days"))
  57. self.assertFalse(_form_value_present({"days": " "}, "days"))
  58. self.assertFalse(_form_value_present({"days": []}, "days"))
  59. def test_present(self):
  60. self.assertTrue(_form_value_present({"days": 0}, "days"))
  61. self.assertTrue(_form_value_present({"ok": False}, "ok"))
  62. self.assertTrue(_form_value_present({"days": 5}, "days"))
  63. self.assertTrue(_form_value_present({"t": "事假"}, "t"))
  64. class TestCollectConditionFields(unittest.TestCase):
  65. def test_linear_no_fields(self):
  66. root = {
  67. "id": "s",
  68. "type": "start",
  69. "child": {"id": "e", "type": "end"},
  70. }
  71. self.assertEqual(_collect_condition_fields(root), [])
  72. self.assertTrue(_condition_fields_ready(root, {}))
  73. def test_rules_field(self):
  74. root = _cond_flow()
  75. self.assertEqual(_collect_condition_fields(root), ["days"])
  76. self.assertFalse(_condition_fields_ready(root, {}))
  77. self.assertTrue(_condition_fields_ready(root, {"days": 5}))
  78. def test_when_formula_fields(self):
  79. root = {
  80. "type": "condition",
  81. "conditionGroups": [
  82. {"name": "x", "when": "{days} > 3 and {leaveType} == '事假'", "child": {}},
  83. {"name": "d", "isDefault": True, "rules": []},
  84. ],
  85. }
  86. self.assertEqual(_collect_condition_fields(root), ["days", "leaveType"])
  87. def test_initiator_system_field_not_gated_by_form_data(self):
  88. """系统伪字段 __initiator__ 不依赖 formData,空表单也应 resolved 门闩通过。"""
  89. root = {
  90. "id": "s",
  91. "type": "start",
  92. "child": {
  93. "id": "c1",
  94. "type": "condition",
  95. "conditionGroups": [
  96. {
  97. "name": "条件1",
  98. "rules": [
  99. {
  100. "field": INITIATOR_CONDITION_FIELD,
  101. "op": "contains",
  102. "value": ["u1"],
  103. }
  104. ],
  105. "child": {"id": "e1", "type": "end"},
  106. },
  107. {
  108. "name": "默认",
  109. "isDefault": True,
  110. "rules": [],
  111. "child": {"id": "e2", "type": "end"},
  112. },
  113. ],
  114. },
  115. }
  116. self.assertEqual(_collect_condition_fields(root), [])
  117. self.assertTrue(_condition_fields_ready(root, {}))
  118. def test_initiator_plus_form_field_still_gates_form(self):
  119. root = {
  120. "type": "condition",
  121. "conditionGroups": [
  122. {
  123. "name": "条件1",
  124. "rules": [
  125. {
  126. "field": INITIATOR_CONDITION_FIELD,
  127. "op": "contains",
  128. "value": ["u1"],
  129. },
  130. {"field": "days", "op": ">", "value": 3},
  131. ],
  132. "child": {},
  133. },
  134. {"name": "默认", "isDefault": True, "rules": []},
  135. ],
  136. }
  137. self.assertEqual(_collect_condition_fields(root), ["days"])
  138. self.assertFalse(_condition_fields_ready(root, {}))
  139. self.assertTrue(_condition_fields_ready(root, {"days": 5}))
  140. class TestWalkPreviewResolvedPath(unittest.TestCase):
  141. def test_hit_condition1(self):
  142. db = MagicMock()
  143. steps = []
  144. with patch("app.services.oa_engine.resolve_assignees", return_value=(["u1"], {})), patch(
  145. "app.services.oa_engine._preview_people",
  146. side_effect=lambda _db, ids: [{"id": i, "nickName": i} for i in ids],
  147. ), patch("app.services.oa_engine._assignee_meta", return_value="指定成员"):
  148. ok = _walk_preview_nodes(
  149. db,
  150. node=_cond_flow(),
  151. tenant_no="t1",
  152. initiator_user_id="init",
  153. initiator_dept_id=None,
  154. advanced={},
  155. initiator_select_by_node=None,
  156. form_data={"days": 5},
  157. steps=steps,
  158. )
  159. self.assertTrue(ok)
  160. types = [s["type"] for s in steps]
  161. names = [s["name"] for s in steps]
  162. self.assertNotIn("branch", types)
  163. self.assertEqual(names, ["发起人", "一级审批", "董事长审批", "结束"])
  164. def test_hit_default(self):
  165. db = MagicMock()
  166. steps = []
  167. with patch("app.services.oa_engine.resolve_assignees", return_value=(["u1"], {})), patch(
  168. "app.services.oa_engine._preview_people",
  169. side_effect=lambda _db, ids: [{"id": i, "nickName": i} for i in ids],
  170. ), patch("app.services.oa_engine._assignee_meta", return_value="指定成员"):
  171. ok = _walk_preview_nodes(
  172. db,
  173. node=_cond_flow(),
  174. tenant_no="t1",
  175. initiator_user_id="init",
  176. initiator_dept_id=None,
  177. advanced={},
  178. initiator_select_by_node=None,
  179. form_data={"days": 1},
  180. steps=steps,
  181. )
  182. self.assertTrue(ok)
  183. names = [s["name"] for s in steps]
  184. self.assertEqual(names, ["发起人", "一级审批", "结束"])
  185. self.assertNotIn("董事长审批", names)
  186. def test_pending_message_constant(self):
  187. self.assertEqual(_FLOW_PREVIEW_PENDING_MSG, "必填信息填写后,流程将自动显示")
  188. def test_merge_after_condition_skips_branch_end(self):
  189. """条件分支叶子 end 后若有汇合主链,预览只保留真正的结束节点。"""
  190. root = {
  191. "id": "s",
  192. "type": "start",
  193. "name": "发起人",
  194. "child": {
  195. "id": "c1",
  196. "type": "condition",
  197. "conditionGroups": [
  198. {
  199. "name": "条件1",
  200. "rules": [{"field": "days", "op": ">", "value": 3}],
  201. "child": {
  202. "id": "a1",
  203. "type": "approval",
  204. "name": "领导会签",
  205. "approverConfig": {
  206. "assigneeType": "users",
  207. "userIds": ["u2"],
  208. "signMode": "and",
  209. },
  210. "child": {"id": "e_branch", "type": "end", "name": "结束"},
  211. },
  212. },
  213. {
  214. "name": "默认",
  215. "isDefault": True,
  216. "rules": [],
  217. "child": {"id": "e_def", "type": "end", "name": "结束"},
  218. },
  219. ],
  220. "child": {
  221. "id": "cc1",
  222. "type": "cc",
  223. "name": "抄送人",
  224. "approverConfig": {
  225. "assigneeTypes": ["initiator_select"],
  226. "assigneeType": "initiator_select",
  227. },
  228. "child": {"id": "e_final", "type": "end", "name": "结束"},
  229. },
  230. },
  231. }
  232. db = MagicMock()
  233. steps = []
  234. with patch(
  235. "app.services.oa_engine.resolve_assignees", return_value=(["u2"], {})
  236. ), patch(
  237. "app.services.oa_engine.resolve_cc_assignees", return_value=([], {})
  238. ), patch(
  239. "app.services.oa_engine._preview_people",
  240. side_effect=lambda _db, ids: [{"id": i, "nickName": i} for i in ids],
  241. ), patch(
  242. "app.services.oa_engine._assignee_meta", return_value="指定成员"
  243. ), patch(
  244. "app.services.oa_engine._assignee_meta_for_cc", return_value="发起人自选"
  245. ):
  246. ok = _walk_preview_nodes(
  247. db,
  248. node=root,
  249. tenant_no="t1",
  250. initiator_user_id="init",
  251. initiator_dept_id=None,
  252. advanced={},
  253. initiator_select_by_node=None,
  254. form_data={"days": 5},
  255. steps=steps,
  256. )
  257. self.assertTrue(ok)
  258. names = [s["name"] for s in steps]
  259. types = [s["type"] for s in steps]
  260. self.assertEqual(names, ["发起人", "领导会签", "抄送人", "结束"])
  261. self.assertEqual(types.count("end"), 1)
  262. self.assertEqual(steps[-1]["id"], "e_final")
  263. path = _iter_resolved_path_nodes(root, {"days": 5}, initiator_user_id="init")
  264. self.assertIsNotNone(path)
  265. path_ids = [n.get("id") for n in path]
  266. self.assertNotIn("e_branch", path_ids)
  267. self.assertIn("e_final", path_ids)
  268. self.assertEqual(sum(1 for n in path if n.get("type") == "end"), 1)
  269. if __name__ == "__main__":
  270. unittest.main()