test_database.py 3.8 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 Database."""
  18. from unittest.mock import patch
  19. import pytest
  20. from pydolphinscheduler.tasks.database import Database
  21. TEST_DATABASE_TASK_TYPE = "SQL"
  22. TEST_DATABASE_SQL = "select 1"
  23. TEST_DATABASE_DATASOURCE_NAME = "test_datasource"
  24. @patch(
  25. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  26. return_value=(123, 1),
  27. )
  28. @patch(
  29. "pydolphinscheduler.tasks.database.Database.get_datasource_info",
  30. return_value=({"id": 1, "type": "mock_type"}),
  31. )
  32. def test_get_datasource_detail(mock_datasource, mock_code_version):
  33. """Test :func:`get_datasource_type` and :func:`get_datasource_id` can return expect value."""
  34. name = "test_get_database_detail"
  35. task = Database(
  36. TEST_DATABASE_TASK_TYPE, name, TEST_DATABASE_DATASOURCE_NAME, TEST_DATABASE_SQL
  37. )
  38. assert 1 == task.get_datasource_id()
  39. assert "mock_type" == task.get_datasource_type()
  40. @pytest.mark.parametrize(
  41. "attr, expect",
  42. [
  43. (
  44. {
  45. "task_type": TEST_DATABASE_TASK_TYPE,
  46. "name": "test-task-params",
  47. "datasource_name": TEST_DATABASE_DATASOURCE_NAME,
  48. },
  49. {
  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.database.Database.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 database task property."""
  71. task = Database(**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.database.Database.get_datasource_info",
  79. return_value=({"id": 1, "type": "MYSQL"}),
  80. )
  81. def test_database_get_define(mock_datasource, mock_code_version):
  82. """Test task database function get_define."""
  83. name = "test_database_get_define"
  84. expect = {
  85. "code": 123,
  86. "name": name,
  87. "version": 1,
  88. "description": None,
  89. "delayTime": 0,
  90. "taskType": TEST_DATABASE_TASK_TYPE,
  91. "taskParams": {
  92. "type": "MYSQL",
  93. "datasource": 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. task = Database(TEST_DATABASE_TASK_TYPE, name, TEST_DATABASE_DATASOURCE_NAME)
  110. assert task.get_define() == expect