test_datax.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 DataX."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.tasks.datax import CustomDataX, DataX
  21. @patch(
  22. "pydolphinscheduler.core.database.Database.get_database_info",
  23. return_value=({"id": 1, "type": "MYSQL"}),
  24. )
  25. def test_datax_get_define(mock_datasource):
  26. """Test task datax function get_define."""
  27. code = 123
  28. version = 1
  29. name = "test_datax_get_define"
  30. command = "select name from test_source_table_name"
  31. datasource_name = "test_datasource"
  32. datatarget_name = "test_datatarget"
  33. target_table = "test_target_table_name"
  34. expect = {
  35. "code": code,
  36. "name": name,
  37. "version": 1,
  38. "description": None,
  39. "delayTime": 0,
  40. "taskType": "DATAX",
  41. "taskParams": {
  42. "customConfig": 0,
  43. "dsType": "MYSQL",
  44. "dataSource": 1,
  45. "dtType": "MYSQL",
  46. "dataTarget": 1,
  47. "sql": command,
  48. "targetTable": target_table,
  49. "jobSpeedByte": 0,
  50. "jobSpeedRecord": 1000,
  51. "xms": 1,
  52. "xmx": 1,
  53. "preStatements": [],
  54. "postStatements": [],
  55. "localParams": [],
  56. "resourceList": [],
  57. "dependence": {},
  58. "conditionResult": {"successNode": [""], "failedNode": [""]},
  59. "waitStartTimeout": {},
  60. },
  61. "flag": "YES",
  62. "taskPriority": "MEDIUM",
  63. "workerGroup": "default",
  64. "failRetryTimes": 0,
  65. "failRetryInterval": 1,
  66. "timeoutFlag": "CLOSE",
  67. "timeoutNotifyStrategy": None,
  68. "timeout": 0,
  69. }
  70. with patch(
  71. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  72. return_value=(code, version),
  73. ):
  74. task = DataX(name, datasource_name, datatarget_name, command, target_table)
  75. assert task.get_define() == expect
  76. @pytest.mark.parametrize("json_template", ["json_template"])
  77. def test_custom_datax_get_define(json_template):
  78. """Test task custom datax function get_define."""
  79. code = 123
  80. version = 1
  81. name = "test_custom_datax_get_define"
  82. expect = {
  83. "code": code,
  84. "name": name,
  85. "version": 1,
  86. "description": None,
  87. "delayTime": 0,
  88. "taskType": "DATAX",
  89. "taskParams": {
  90. "customConfig": 1,
  91. "json": json_template,
  92. "xms": 1,
  93. "xmx": 1,
  94. "localParams": [],
  95. "resourceList": [],
  96. "dependence": {},
  97. "conditionResult": {"successNode": [""], "failedNode": [""]},
  98. "waitStartTimeout": {},
  99. },
  100. "flag": "YES",
  101. "taskPriority": "MEDIUM",
  102. "workerGroup": "default",
  103. "failRetryTimes": 0,
  104. "failRetryInterval": 1,
  105. "timeoutFlag": "CLOSE",
  106. "timeoutNotifyStrategy": None,
  107. "timeout": 0,
  108. }
  109. with patch(
  110. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  111. return_value=(code, version),
  112. ):
  113. task = CustomDataX(name, json_template)
  114. print(task.get_define())
  115. print(expect)
  116. assert task.get_define() == expect