test_procedure.py 3.9 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 Procedure."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.tasks.procedure import Procedure
  21. TEST_PROCEDURE_SQL = (
  22. 'create procedure HelloWorld() selece "hello world"; call HelloWorld();'
  23. )
  24. TEST_PROCEDURE_DATASOURCE_NAME = "test_datasource"
  25. @patch(
  26. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  27. return_value=(123, 1),
  28. )
  29. @patch(
  30. "pydolphinscheduler.tasks.procedure.Procedure.get_datasource_info",
  31. return_value=({"id": 1, "type": "mock_type"}),
  32. )
  33. def test_get_datasource_detail(mock_datasource, mock_code_version):
  34. """Test :func:`get_datasource_type` and :func:`get_datasource_id` can return expect value."""
  35. name = "test_get_datasource_detail"
  36. task = Procedure(name, TEST_PROCEDURE_DATASOURCE_NAME, TEST_PROCEDURE_SQL)
  37. assert 1 == task.get_datasource_id()
  38. assert "mock_type" == task.get_datasource_type()
  39. @pytest.mark.parametrize(
  40. "attr, expect",
  41. [
  42. (
  43. {
  44. "name": "test-procedure-task-params",
  45. "datasource_name": TEST_PROCEDURE_DATASOURCE_NAME,
  46. "sql": TEST_PROCEDURE_SQL,
  47. },
  48. {
  49. "sql": TEST_PROCEDURE_SQL,
  50. "type": "MYSQL",
  51. "datasource": 1,
  52. "localParams": [],
  53. "resourceList": [],
  54. "dependence": {},
  55. "waitStartTimeout": {},
  56. "conditionResult": {"successNode": [""], "failedNode": [""]},
  57. },
  58. )
  59. ],
  60. )
  61. @patch(
  62. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  63. return_value=(123, 1),
  64. )
  65. @patch(
  66. "pydolphinscheduler.tasks.procedure.Procedure.get_datasource_info",
  67. return_value=({"id": 1, "type": "MYSQL"}),
  68. )
  69. def test_property_task_params(mock_datasource, mock_code_version, attr, expect):
  70. """Test task sql task property."""
  71. task = Procedure(**attr)
  72. assert expect == task.task_params
  73. @patch(
  74. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  75. return_value=(123, 1),
  76. )
  77. @patch(
  78. "pydolphinscheduler.tasks.procedure.Procedure.get_datasource_info",
  79. return_value=({"id": 1, "type": "MYSQL"}),
  80. )
  81. def test_sql_get_define(mock_datasource, mock_code_version):
  82. """Test task procedure function get_define."""
  83. name = "test_procedure_get_define"
  84. expect = {
  85. "code": 123,
  86. "name": name,
  87. "version": 1,
  88. "description": None,
  89. "delayTime": 0,
  90. "taskType": "PROCEDURE",
  91. "taskParams": {
  92. "type": "MYSQL",
  93. "datasource": 1,
  94. "sql": TEST_PROCEDURE_SQL,
  95. "localParams": [],
  96. "resourceList": [],
  97. "dependence": {},
  98. "conditionResult": {"successNode": [""], "failedNode": [""]},
  99. "waitStartTimeout": {},
  100. },
  101. "flag": "YES",
  102. "taskPriority": "MEDIUM",
  103. "workerGroup": "default",
  104. "failRetryTimes": 0,
  105. "failRetryInterval": 1,
  106. "timeoutFlag": "CLOSE",
  107. "timeoutNotifyStrategy": None,
  108. "timeout": 0,
  109. }
  110. task = Procedure(name, TEST_PROCEDURE_DATASOURCE_NAME, TEST_PROCEDURE_SQL)
  111. assert task.get_define() == expect