test_python.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 python."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.exceptions import PyDSParamException
  21. from pydolphinscheduler.tasks.python import Python
  22. def foo(): # noqa: D103
  23. print("hello world.")
  24. @pytest.mark.parametrize(
  25. "attr, expect",
  26. [
  27. (
  28. {"definition": "print(1)"},
  29. {
  30. "rawScript": "print(1)",
  31. "localParams": [],
  32. "resourceList": [],
  33. "dependence": {},
  34. "waitStartTimeout": {},
  35. "conditionResult": {"successNode": [""], "failedNode": [""]},
  36. },
  37. ),
  38. (
  39. {"definition": "def foo():\n print('I am foo')"},
  40. {
  41. "rawScript": "def foo():\n print('I am foo')\nfoo()",
  42. "localParams": [],
  43. "resourceList": [],
  44. "dependence": {},
  45. "waitStartTimeout": {},
  46. "conditionResult": {"successNode": [""], "failedNode": [""]},
  47. },
  48. ),
  49. (
  50. {"definition": foo},
  51. {
  52. "rawScript": 'def foo(): # noqa: D103\n print("hello world.")\nfoo()',
  53. "localParams": [],
  54. "resourceList": [],
  55. "dependence": {},
  56. "waitStartTimeout": {},
  57. "conditionResult": {"successNode": [""], "failedNode": [""]},
  58. },
  59. ),
  60. ],
  61. )
  62. @patch(
  63. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  64. return_value=(123, 1),
  65. )
  66. def test_property_task_params(mock_code_version, attr, expect):
  67. """Test task python property."""
  68. task = Python("test-python-task-params", **attr)
  69. assert expect == task.task_params
  70. @pytest.mark.parametrize(
  71. "script_code",
  72. [
  73. 123,
  74. ("print", "hello world"),
  75. ],
  76. )
  77. @patch(
  78. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  79. return_value=(123, 1),
  80. )
  81. def test_python_task_not_support_code(mock_code, script_code):
  82. """Test python task parameters."""
  83. name = "not_support_code_type"
  84. with pytest.raises(
  85. PyDSParamException, match="Parameter definition do not support .*?"
  86. ):
  87. task = Python(name, script_code)
  88. task.raw_script
  89. @pytest.mark.parametrize(
  90. "name, script_code, raw",
  91. [
  92. ("string_define", 'print("hello world.")', 'print("hello world.")'),
  93. (
  94. "function_define",
  95. foo,
  96. 'def foo(): # noqa: D103\n print("hello world.")\nfoo()',
  97. ),
  98. ],
  99. )
  100. def test_python_get_define(name, script_code, raw):
  101. """Test task python function get_define."""
  102. code = 123
  103. version = 1
  104. expect = {
  105. "code": code,
  106. "name": name,
  107. "version": 1,
  108. "description": None,
  109. "delayTime": 0,
  110. "taskType": "PYTHON",
  111. "taskParams": {
  112. "resourceList": [],
  113. "localParams": [],
  114. "rawScript": raw,
  115. "dependence": {},
  116. "conditionResult": {"successNode": [""], "failedNode": [""]},
  117. "waitStartTimeout": {},
  118. },
  119. "flag": "YES",
  120. "taskPriority": "MEDIUM",
  121. "workerGroup": "default",
  122. "failRetryTimes": 0,
  123. "failRetryInterval": 1,
  124. "timeoutFlag": "CLOSE",
  125. "timeoutNotifyStrategy": None,
  126. "timeout": 0,
  127. }
  128. with patch(
  129. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  130. return_value=(code, version),
  131. ):
  132. shell = Python(name, script_code)
  133. assert shell.get_define() == expect