test_oa_flow_preview.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """OA 发起预览:条件字段门闩与单路 walk"""
  2. import unittest
  3. from unittest.mock import MagicMock, patch
  4. from app.services.oa_engine import (
  5. _FLOW_PREVIEW_PENDING_MSG,
  6. _collect_condition_fields,
  7. _condition_fields_ready,
  8. _form_value_present,
  9. _walk_preview_nodes,
  10. )
  11. def _cond_flow():
  12. return {
  13. "id": "s",
  14. "type": "start",
  15. "name": "发起人",
  16. "child": {
  17. "id": "a0",
  18. "type": "approval",
  19. "name": "一级审批",
  20. "approverConfig": {"assigneeType": "users", "userIds": ["u1"], "signMode": "or"},
  21. "child": {
  22. "id": "c1",
  23. "type": "condition",
  24. "conditionGroups": [
  25. {
  26. "name": "条件1",
  27. "rules": [{"field": "days", "op": ">", "value": 3}],
  28. "child": {
  29. "id": "a1",
  30. "type": "approval",
  31. "name": "董事长审批",
  32. "approverConfig": {
  33. "assigneeType": "users",
  34. "userIds": ["u2"],
  35. "signMode": "or",
  36. },
  37. "child": {"id": "e1", "type": "end", "name": "结束"},
  38. },
  39. },
  40. {
  41. "name": "默认",
  42. "isDefault": True,
  43. "rules": [],
  44. "child": {"id": "e2", "type": "end", "name": "结束"},
  45. },
  46. ],
  47. },
  48. },
  49. }
  50. class TestFormValuePresent(unittest.TestCase):
  51. def test_empty(self):
  52. self.assertFalse(_form_value_present({}, "days"))
  53. self.assertFalse(_form_value_present({"days": None}, "days"))
  54. self.assertFalse(_form_value_present({"days": ""}, "days"))
  55. self.assertFalse(_form_value_present({"days": " "}, "days"))
  56. self.assertFalse(_form_value_present({"days": []}, "days"))
  57. def test_present(self):
  58. self.assertTrue(_form_value_present({"days": 0}, "days"))
  59. self.assertTrue(_form_value_present({"ok": False}, "ok"))
  60. self.assertTrue(_form_value_present({"days": 5}, "days"))
  61. self.assertTrue(_form_value_present({"t": "事假"}, "t"))
  62. class TestCollectConditionFields(unittest.TestCase):
  63. def test_linear_no_fields(self):
  64. root = {
  65. "id": "s",
  66. "type": "start",
  67. "child": {"id": "e", "type": "end"},
  68. }
  69. self.assertEqual(_collect_condition_fields(root), [])
  70. self.assertTrue(_condition_fields_ready(root, {}))
  71. def test_rules_field(self):
  72. root = _cond_flow()
  73. self.assertEqual(_collect_condition_fields(root), ["days"])
  74. self.assertFalse(_condition_fields_ready(root, {}))
  75. self.assertTrue(_condition_fields_ready(root, {"days": 5}))
  76. def test_when_formula_fields(self):
  77. root = {
  78. "type": "condition",
  79. "conditionGroups": [
  80. {"name": "x", "when": "{days} > 3 and {leaveType} == '事假'", "child": {}},
  81. {"name": "d", "isDefault": True, "rules": []},
  82. ],
  83. }
  84. self.assertEqual(_collect_condition_fields(root), ["days", "leaveType"])
  85. class TestWalkPreviewResolvedPath(unittest.TestCase):
  86. def test_hit_condition1(self):
  87. db = MagicMock()
  88. steps = []
  89. with patch("app.services.oa_engine.resolve_assignees", return_value=(["u1"], {})), patch(
  90. "app.services.oa_engine._preview_people",
  91. side_effect=lambda _db, ids: [{"id": i, "nickName": i} for i in ids],
  92. ), patch("app.services.oa_engine._assignee_meta", return_value="指定成员"):
  93. ok = _walk_preview_nodes(
  94. db,
  95. node=_cond_flow(),
  96. tenant_no="t1",
  97. initiator_user_id="init",
  98. initiator_dept_id=None,
  99. advanced={},
  100. initiator_select_by_node=None,
  101. form_data={"days": 5},
  102. steps=steps,
  103. )
  104. self.assertTrue(ok)
  105. types = [s["type"] for s in steps]
  106. names = [s["name"] for s in steps]
  107. self.assertNotIn("branch", types)
  108. self.assertEqual(names, ["发起人", "一级审批", "董事长审批", "结束"])
  109. def test_hit_default(self):
  110. db = MagicMock()
  111. steps = []
  112. with patch("app.services.oa_engine.resolve_assignees", return_value=(["u1"], {})), patch(
  113. "app.services.oa_engine._preview_people",
  114. side_effect=lambda _db, ids: [{"id": i, "nickName": i} for i in ids],
  115. ), patch("app.services.oa_engine._assignee_meta", return_value="指定成员"):
  116. ok = _walk_preview_nodes(
  117. db,
  118. node=_cond_flow(),
  119. tenant_no="t1",
  120. initiator_user_id="init",
  121. initiator_dept_id=None,
  122. advanced={},
  123. initiator_select_by_node=None,
  124. form_data={"days": 1},
  125. steps=steps,
  126. )
  127. self.assertTrue(ok)
  128. names = [s["name"] for s in steps]
  129. self.assertEqual(names, ["发起人", "一级审批", "结束"])
  130. self.assertNotIn("董事长审批", names)
  131. def test_pending_message_constant(self):
  132. self.assertEqual(_FLOW_PREVIEW_PENDING_MSG, "必填信息填写后,流程将自动显示")
  133. if __name__ == "__main__":
  134. unittest.main()