test_sql.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 Sql."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.tasks.sql import Sql, SqlType
  21. @pytest.mark.parametrize(
  22. "sql, sql_type",
  23. [
  24. ("select 1", SqlType.SELECT),
  25. (" select 1", SqlType.SELECT),
  26. (" select 1 ", SqlType.SELECT),
  27. (" select 'insert' ", SqlType.SELECT),
  28. (" select 'insert ' ", SqlType.SELECT),
  29. ("with tmp as (select 1) select * from tmp ", SqlType.SELECT),
  30. ("insert into table_name(col1, col2) value (val1, val2)", SqlType.NOT_SELECT),
  31. (
  32. "insert into table_name(select, col2) value ('select', val2)",
  33. SqlType.NOT_SELECT,
  34. ),
  35. ("update table_name SET col1=val1 where col1=val2", SqlType.NOT_SELECT),
  36. ("update table_name SET col1='select' where col1=val2", SqlType.NOT_SELECT),
  37. ("delete from table_name where id < 10", SqlType.NOT_SELECT),
  38. ("delete from table_name where id < 10", SqlType.NOT_SELECT),
  39. ("alter table table_name add column col1 int", SqlType.NOT_SELECT),
  40. ],
  41. )
  42. @patch(
  43. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  44. return_value=(123, 1),
  45. )
  46. @patch(
  47. "pydolphinscheduler.core.database.Database.get_database_info",
  48. return_value=({"id": 1, "type": "mock_type"}),
  49. )
  50. def test_get_sql_type(mock_datasource, mock_code_version, sql, sql_type):
  51. """Test property sql_type could return correct type."""
  52. name = "test_get_sql_type"
  53. datasource_name = "test_datasource"
  54. task = Sql(name, datasource_name, sql)
  55. assert (
  56. sql_type == task.sql_type
  57. ), f"Sql {sql} expect sql type is {sql_type} but got {task.sql_type}"
  58. @pytest.mark.parametrize(
  59. "attr, expect",
  60. [
  61. (
  62. {"datasource_name": "datasource_name", "sql": "select 1"},
  63. {
  64. "sql": "select 1",
  65. "type": "MYSQL",
  66. "datasource": 1,
  67. "sqlType": SqlType.SELECT,
  68. "preStatements": [],
  69. "postStatements": [],
  70. "displayRows": 10,
  71. "localParams": [],
  72. "resourceList": [],
  73. "dependence": {},
  74. "waitStartTimeout": {},
  75. "conditionResult": {"successNode": [""], "failedNode": [""]},
  76. },
  77. )
  78. ],
  79. )
  80. @patch(
  81. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  82. return_value=(123, 1),
  83. )
  84. @patch(
  85. "pydolphinscheduler.core.database.Database.get_database_info",
  86. return_value=({"id": 1, "type": "MYSQL"}),
  87. )
  88. def test_property_task_params(mock_datasource, mock_code_version, attr, expect):
  89. """Test task sql task property."""
  90. task = Sql("test-sql-task-params", **attr)
  91. assert expect == task.task_params
  92. @patch(
  93. "pydolphinscheduler.core.database.Database.get_database_info",
  94. return_value=({"id": 1, "type": "MYSQL"}),
  95. )
  96. def test_sql_get_define(mock_datasource):
  97. """Test task sql function get_define."""
  98. code = 123
  99. version = 1
  100. name = "test_sql_get_define"
  101. command = "select 1"
  102. datasource_name = "test_datasource"
  103. expect = {
  104. "code": code,
  105. "name": name,
  106. "version": 1,
  107. "description": None,
  108. "delayTime": 0,
  109. "taskType": "SQL",
  110. "taskParams": {
  111. "type": "MYSQL",
  112. "datasource": 1,
  113. "sql": command,
  114. "sqlType": SqlType.SELECT,
  115. "displayRows": 10,
  116. "preStatements": [],
  117. "postStatements": [],
  118. "localParams": [],
  119. "resourceList": [],
  120. "dependence": {},
  121. "conditionResult": {"successNode": [""], "failedNode": [""]},
  122. "waitStartTimeout": {},
  123. },
  124. "flag": "YES",
  125. "taskPriority": "MEDIUM",
  126. "workerGroup": "default",
  127. "failRetryTimes": 0,
  128. "failRetryInterval": 1,
  129. "timeoutFlag": "CLOSE",
  130. "timeoutNotifyStrategy": None,
  131. "timeout": 0,
  132. }
  133. with patch(
  134. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  135. return_value=(code, version),
  136. ):
  137. task = Sql(name, datasource_name, command)
  138. assert task.get_define() == expect