test_python.py 3.5 KB

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