test_switch.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. """Test Task switch."""
  18. from typing import Optional, Tuple
  19. from unittest.mock import patch
  20. import pytest
  21. from pydolphinscheduler.core.process_definition import ProcessDefinition
  22. from pydolphinscheduler.exceptions import PyDSParamException
  23. from pydolphinscheduler.tasks.switch import (
  24. Branch,
  25. Default,
  26. Switch,
  27. SwitchBranch,
  28. SwitchCondition,
  29. )
  30. from tests.testing.task import Task
  31. TEST_NAME = "test-task"
  32. TEST_TYPE = "test-type"
  33. def task_switch_arg_wrapper(obj, task: Task, exp: Optional[str] = None) -> SwitchBranch:
  34. """Wrap task switch and its subclass."""
  35. if obj is Default:
  36. return obj(task)
  37. elif obj is Branch:
  38. return obj(exp, task)
  39. else:
  40. return obj(task, exp)
  41. @pytest.mark.parametrize(
  42. "obj",
  43. [
  44. SwitchBranch,
  45. Branch,
  46. Default,
  47. ],
  48. )
  49. def test_switch_branch_attr_next_node(obj: SwitchBranch):
  50. """Test get attribute from class switch branch."""
  51. task = Task(name=TEST_NAME, task_type=TEST_TYPE)
  52. switch_branch = task_switch_arg_wrapper(obj, task=task, exp="unittest")
  53. assert switch_branch.next_node == task.code
  54. @pytest.mark.parametrize(
  55. "obj",
  56. [
  57. SwitchBranch,
  58. Default,
  59. ],
  60. )
  61. def test_switch_branch_get_define_without_condition(obj: SwitchBranch):
  62. """Test function :func:`get_define` with None value of attribute condition from class switch branch."""
  63. task = Task(name=TEST_NAME, task_type=TEST_TYPE)
  64. expect = {"nextNode": task.code}
  65. switch_branch = task_switch_arg_wrapper(obj, task=task)
  66. assert switch_branch.get_define() == expect
  67. @pytest.mark.parametrize(
  68. "obj",
  69. [
  70. SwitchBranch,
  71. Branch,
  72. ],
  73. )
  74. def test_switch_branch_get_define_condition(obj: SwitchBranch):
  75. """Test function :func:`get_define` with specific attribute condition from class switch branch."""
  76. task = Task(name=TEST_NAME, task_type=TEST_TYPE)
  77. exp = "${var} == 1"
  78. expect = {
  79. "nextNode": task.code,
  80. "condition": exp,
  81. }
  82. switch_branch = task_switch_arg_wrapper(obj, task=task, exp=exp)
  83. assert switch_branch.get_define() == expect
  84. @pytest.mark.parametrize(
  85. "args, msg",
  86. [
  87. (
  88. (1,),
  89. ".*?parameter only support SwitchBranch but got.*?",
  90. ),
  91. (
  92. (Default(Task(TEST_NAME, TEST_TYPE)), 2),
  93. ".*?parameter only support SwitchBranch but got.*?",
  94. ),
  95. (
  96. (Default(Task(TEST_NAME, TEST_TYPE)), Default(Task(TEST_NAME, TEST_TYPE))),
  97. ".*?parameter only support exactly one default branch",
  98. ),
  99. (
  100. (
  101. Branch(condition="unittest", task=Task(TEST_NAME, TEST_TYPE)),
  102. Default(Task(TEST_NAME, TEST_TYPE)),
  103. Default(Task(TEST_NAME, TEST_TYPE)),
  104. ),
  105. ".*?parameter only support exactly one default branch",
  106. ),
  107. ],
  108. )
  109. def test_switch_condition_set_define_attr_error(args: Tuple, msg: str):
  110. """Test error case on :class:`SwitchCondition`."""
  111. switch_condition = SwitchCondition(*args)
  112. with pytest.raises(PyDSParamException, match=msg):
  113. switch_condition.set_define_attr()
  114. def test_switch_condition_set_define_attr_default():
  115. """Test set :class:`Default` to attribute on :class:`SwitchCondition`."""
  116. task = Task(TEST_NAME, TEST_TYPE)
  117. switch_condition = SwitchCondition(Default(task))
  118. switch_condition.set_define_attr()
  119. assert getattr(switch_condition, "next_node") == task.code
  120. assert getattr(switch_condition, "depend_task_list") == []
  121. def test_switch_condition_set_define_attr_branch():
  122. """Test set :class:`Branch` to attribute on :class:`SwitchCondition`."""
  123. task = Task(TEST_NAME, TEST_TYPE)
  124. switch_condition = SwitchCondition(
  125. Branch("unittest1", task), Branch("unittest2", task)
  126. )
  127. expect = [
  128. {"condition": "unittest1", "nextNode": task.code},
  129. {"condition": "unittest2", "nextNode": task.code},
  130. ]
  131. switch_condition.set_define_attr()
  132. assert getattr(switch_condition, "next_node") == ""
  133. assert getattr(switch_condition, "depend_task_list") == expect
  134. def test_switch_condition_set_define_attr_mix_branch_and_default():
  135. """Test set bot :class:`Branch` and :class:`Default` to attribute on :class:`SwitchCondition`."""
  136. task = Task(TEST_NAME, TEST_TYPE)
  137. switch_condition = SwitchCondition(
  138. Branch("unittest1", task), Branch("unittest2", task), Default(task)
  139. )
  140. expect = [
  141. {"condition": "unittest1", "nextNode": task.code},
  142. {"condition": "unittest2", "nextNode": task.code},
  143. ]
  144. switch_condition.set_define_attr()
  145. assert getattr(switch_condition, "next_node") == task.code
  146. assert getattr(switch_condition, "depend_task_list") == expect
  147. def test_switch_condition_get_define_default():
  148. """Test function :func:`get_define` with :class:`Default` in :class:`SwitchCondition`."""
  149. task = Task(TEST_NAME, TEST_TYPE)
  150. switch_condition = SwitchCondition(Default(task))
  151. expect = {
  152. "dependTaskList": [],
  153. "nextNode": task.code,
  154. }
  155. assert switch_condition.get_define() == expect
  156. def test_switch_condition_get_define_branch():
  157. """Test function :func:`get_define` with :class:`Branch` in :class:`SwitchCondition`."""
  158. task = Task(TEST_NAME, TEST_TYPE)
  159. switch_condition = SwitchCondition(
  160. Branch("unittest1", task), Branch("unittest2", task)
  161. )
  162. expect = {
  163. "dependTaskList": [
  164. {"condition": "unittest1", "nextNode": task.code},
  165. {"condition": "unittest2", "nextNode": task.code},
  166. ],
  167. "nextNode": "",
  168. }
  169. assert switch_condition.get_define() == expect
  170. def test_switch_condition_get_define_mix_branch_and_default():
  171. """Test function :func:`get_define` with both :class:`Branch` and :class:`Default`."""
  172. task = Task(TEST_NAME, TEST_TYPE)
  173. switch_condition = SwitchCondition(
  174. Branch("unittest1", task), Branch("unittest2", task), Default(task)
  175. )
  176. expect = {
  177. "dependTaskList": [
  178. {"condition": "unittest1", "nextNode": task.code},
  179. {"condition": "unittest2", "nextNode": task.code},
  180. ],
  181. "nextNode": task.code,
  182. }
  183. assert switch_condition.get_define() == expect
  184. @patch(
  185. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  186. return_value=(123, 1),
  187. )
  188. def test_switch_get_define(mock_task_code_version):
  189. """Test task switch :func:`get_define`."""
  190. task = Task(name=TEST_NAME, task_type=TEST_TYPE)
  191. switch_condition = SwitchCondition(
  192. Branch(condition="${var1} > 1", task=task),
  193. Branch(condition="${var1} <= 1", task=task),
  194. Default(task),
  195. )
  196. name = "test_switch_get_define"
  197. expect = {
  198. "code": 123,
  199. "name": name,
  200. "version": 1,
  201. "description": None,
  202. "delayTime": 0,
  203. "taskType": "SWITCH",
  204. "taskParams": {
  205. "resourceList": [],
  206. "localParams": [],
  207. "dependence": {},
  208. "conditionResult": {"successNode": [""], "failedNode": [""]},
  209. "waitStartTimeout": {},
  210. "switchResult": {
  211. "dependTaskList": [
  212. {"condition": "${var1} > 1", "nextNode": task.code},
  213. {"condition": "${var1} <= 1", "nextNode": task.code},
  214. ],
  215. "nextNode": task.code,
  216. },
  217. },
  218. "flag": "YES",
  219. "taskPriority": "MEDIUM",
  220. "workerGroup": "default",
  221. "failRetryTimes": 0,
  222. "failRetryInterval": 1,
  223. "timeoutFlag": "CLOSE",
  224. "timeoutNotifyStrategy": None,
  225. "timeout": 0,
  226. }
  227. task = Switch(name, condition=switch_condition)
  228. assert task.get_define() == expect
  229. @patch(
  230. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  231. return_value=(123, 1),
  232. )
  233. def test_switch_set_dep_workflow(mock_task_code_version):
  234. """Test task switch set dependence in workflow level."""
  235. with ProcessDefinition(name="test-switch-set-dep-workflow") as pd:
  236. parent = Task(name="parent", task_type=TEST_TYPE)
  237. switch_child_1 = Task(name="switch_child_1", task_type=TEST_TYPE)
  238. switch_child_2 = Task(name="switch_child_2", task_type=TEST_TYPE)
  239. switch_condition = SwitchCondition(
  240. Branch(condition="${var} > 1", task=switch_child_1),
  241. Default(task=switch_child_2),
  242. )
  243. switch = Switch(name=TEST_NAME, condition=switch_condition)
  244. parent >> switch
  245. # General tasks test
  246. assert len(pd.tasks) == 4
  247. assert sorted(pd.task_list, key=lambda t: t.name) == sorted(
  248. [parent, switch, switch_child_1, switch_child_2], key=lambda t: t.name
  249. )
  250. # Task dep test
  251. assert parent._downstream_task_codes == {switch.code}
  252. assert switch._upstream_task_codes == {parent.code}
  253. # Switch task dep after ProcessDefinition function get_define called
  254. assert switch._downstream_task_codes == {
  255. switch_child_1.code,
  256. switch_child_2.code,
  257. }
  258. assert all(
  259. [
  260. child._upstream_task_codes == {switch.code}
  261. for child in [switch_child_1, switch_child_2]
  262. ]
  263. )