test_example.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 example."""
  18. import ast
  19. import importlib
  20. # import os
  21. # import os
  22. from pathlib import Path
  23. from unittest.mock import patch
  24. import pytest
  25. from tests.testing.task import Task
  26. process_definition_name = set()
  27. def get_all_example_define():
  28. """Get all examples files in examples directory."""
  29. return (
  30. path
  31. for path in Path(__file__).parent.parent.parent.joinpath("examples").iterdir()
  32. if path.is_file()
  33. )
  34. def import_module(script_name, script_path):
  35. """Import and run example module in examples directory."""
  36. spec = importlib.util.spec_from_file_location(script_name, script_path)
  37. module = importlib.util.module_from_spec(spec)
  38. spec.loader.exec_module(module)
  39. return module
  40. @pytest.fixture
  41. def setup_and_teardown_for_stuff():
  42. """Fixture of py.test handle setup and teardown."""
  43. yield
  44. global process_definition_name
  45. process_definition_name = set()
  46. def submit_check_without_same_name(self):
  47. """Side effect for verifying process definition name and adding it to global variable."""
  48. if self.name in process_definition_name:
  49. raise ValueError(
  50. "Example process definition should not have same name, but get duplicate name: %s",
  51. self.name,
  52. )
  53. submit_add_process_definition(self)
  54. def submit_add_process_definition(self):
  55. """Side effect for adding process definition name to global variable."""
  56. process_definition_name.add(self.name)
  57. def test_example_basic():
  58. """Test example basic information.
  59. Which including:
  60. * File extension name is `.py`
  61. * All example except `tutorial.py` is end with keyword "_example"
  62. * All example must have not empty `__doc__`.
  63. """
  64. for ex in get_all_example_define():
  65. # All files in example is python script
  66. assert (
  67. ex.suffix == ".py"
  68. ), f"We expect all examples is python script, but get {ex.name}."
  69. # All except tutorial is end with keyword "_example"
  70. if ex.stem != "tutorial":
  71. assert ex.stem.endswith(
  72. "_example"
  73. ), f"We expect all examples script end with keyword '_example', but get {ex.stem}."
  74. # All files have __doc__
  75. tree = ast.parse(ex.read_text())
  76. example_doc = ast.get_docstring(tree, clean=False)
  77. assert (
  78. example_doc is not None
  79. ), f"We expect all examples have __doc__, but {ex.name} do not."
  80. @patch("pydolphinscheduler.core.process_definition.ProcessDefinition.start")
  81. @patch(
  82. "pydolphinscheduler.core.process_definition.ProcessDefinition.submit",
  83. side_effect=submit_check_without_same_name,
  84. autospec=True,
  85. )
  86. @patch(
  87. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  88. # Example bulk_create_example.py would create workflow dynamic by :func:`get_one_task_by_name`
  89. # and would raise error in :func:`get_one_task_by_name` if we return constant value
  90. # using :arg:`return_value`
  91. side_effect=Task("test_example", "test_example").gen_code_and_version,
  92. )
  93. def test_example_process_definition_without_same_name(
  94. mock_code_version, mock_submit, mock_start
  95. ):
  96. """Test all examples file without same process definition's name.
  97. Our process definition would compete with others if we have same process definition name. It will make
  98. different between actually workflow and our workflow-as-code file which make users feel strange.
  99. """
  100. for ex in get_all_example_define():
  101. # We use side_effect `submit_check_without_same_name` overwrite :func:`submit`
  102. # and check whether it have duplicate name or not
  103. import_module(ex.name, str(ex))
  104. assert True
  105. @patch("pydolphinscheduler.core.process_definition.ProcessDefinition.start")
  106. @patch(
  107. "pydolphinscheduler.core.process_definition.ProcessDefinition.submit",
  108. side_effect=submit_add_process_definition,
  109. autospec=True,
  110. )
  111. @patch(
  112. "pydolphinscheduler.core.task.Task.gen_code_and_version",
  113. # Example bulk_create_example.py would create workflow dynamic by :func:`get_one_task_by_name`
  114. # and would raise error in :func:`get_one_task_by_name` if we return constant value
  115. # using :arg:`return_value`
  116. side_effect=Task("test_example", "test_example").gen_code_and_version,
  117. )
  118. def test_file_name_in_process_definition(mock_code_version, mock_submit, mock_start):
  119. """Test example file name in example definition name.
  120. We should not directly assert equal, because some of the examples contain
  121. more than one process definition.
  122. """
  123. global process_definition_name
  124. for ex in get_all_example_define():
  125. # Skip bulk_create_example check, cause it contain multiple workflow and
  126. # without one named bulk_create_example
  127. if ex.stem == "bulk_create_example":
  128. continue
  129. process_definition_name = set()
  130. assert ex.stem not in process_definition_name
  131. import_module(ex.name, str(ex))
  132. assert ex.stem in process_definition_name