test_sql.py 5.4 KB

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