conftest.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Copyright 2021-2025 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. import argparse
  12. import typing as t
  13. import pytest
  14. from taipy.common._cli._base_cli._taipy_parser import _TaipyParser
  15. from taipy.common.config import Config, _inject_section
  16. from taipy.common.config._config import _Config
  17. from taipy.common.config._config_comparator._config_comparator import _ConfigComparator
  18. from taipy.common.config._serializer._base_serializer import _BaseSerializer
  19. from taipy.common.config._serializer._toml_serializer import _TomlSerializer
  20. from taipy.common.config.checker._checker import _Checker
  21. from taipy.common.config.checker.issue_collector import IssueCollector
  22. from taipy.core.config import CoreSection, DataNodeConfig, JobConfig, ScenarioConfig, TaskConfig
  23. from taipy.rest.config import RestConfig
  24. def pytest_addoption(parser: pytest.Parser) -> None:
  25. """Add custom command line options for pytest."""
  26. parser.addoption("--e2e-base-url", action="store", default="/", help="base url for e2e testing")
  27. parser.addoption("--e2e-port", action="store", default="5000", help="port for e2e testing")
  28. parser.addoption("--gui-server", action="store", default="flask", help="server for e2e testing")
  29. @pytest.fixture(scope="session")
  30. def e2e_base_url(request: pytest.FixtureRequest) -> str:
  31. """Fixture to get the base URL for e2e testing."""
  32. return request.config.getoption("--e2e-base-url")
  33. @pytest.fixture(scope="session")
  34. def e2e_port(request: pytest.FixtureRequest) -> str:
  35. """Fixture to get the port for e2e testing."""
  36. return request.config.getoption("--e2e-port")
  37. @pytest.fixture(scope="session")
  38. def gui_server(request: pytest.FixtureRequest) -> str:
  39. """Fixture to get the server for e2e testing."""
  40. return request.config.getoption("--gui-server")
  41. def remove_subparser(name: str) -> None:
  42. """Remove a subparser from argparse."""
  43. _TaipyParser._sub_taipyparsers.pop(name, None)
  44. if _TaipyParser._subparser_action:
  45. _TaipyParser._subparser_action._name_parser_map.pop(name, None)
  46. for action in _TaipyParser._subparser_action._choices_actions:
  47. if action.dest == name:
  48. _TaipyParser._subparser_action._choices_actions.remove(action)
  49. @pytest.fixture
  50. def clean_argparser() -> t.Callable:
  51. """Fixture to clean the argument parser."""
  52. def _clean_argparser() -> None:
  53. _TaipyParser._parser = argparse.ArgumentParser(conflict_handler="resolve")
  54. _TaipyParser._subparser_action = None
  55. _TaipyParser._arg_groups = {}
  56. subcommands = list(_TaipyParser._sub_taipyparsers.keys())
  57. for subcommand in subcommands:
  58. remove_subparser(subcommand)
  59. return _clean_argparser
  60. @pytest.fixture
  61. def reset_configuration_singleton() -> t.Callable:
  62. """Fixture to reset the configuration singleton."""
  63. def _reset_configuration_singleton() -> None:
  64. Config.unblock_update()
  65. Config._default_config = _Config()._default_config()
  66. Config._python_config = _Config()
  67. Config._file_config = _Config()
  68. Config._env_file_config = _Config()
  69. Config._applied_config = _Config()
  70. Config._collector = IssueCollector()
  71. Config._serializer = _TomlSerializer()
  72. Config._comparator = _ConfigComparator()
  73. _BaseSerializer._SERIALIZABLE_TYPES = [
  74. "bool",
  75. "str",
  76. "int",
  77. "float",
  78. "datetime",
  79. "timedelta",
  80. "function",
  81. "class",
  82. "SECTION",
  83. ]
  84. _Checker._checkers = []
  85. return _reset_configuration_singleton
  86. @pytest.fixture
  87. def inject_core_sections() -> t.Callable:
  88. """Fixture to inject core sections into the configuration."""
  89. def _inject_core_sections() -> None:
  90. _inject_section(
  91. JobConfig,
  92. "job_config",
  93. JobConfig("development"),
  94. [("configure_job_executions", JobConfig._configure)],
  95. True,
  96. )
  97. _inject_section(
  98. CoreSection,
  99. "core",
  100. CoreSection.default_config(),
  101. [("configure_core", CoreSection._configure)],
  102. add_to_unconflicted_sections=True,
  103. )
  104. _inject_section(
  105. DataNodeConfig,
  106. "data_nodes",
  107. DataNodeConfig.default_config(),
  108. [
  109. ("configure_data_node", DataNodeConfig._configure),
  110. ("configure_data_node_from", DataNodeConfig._configure_from),
  111. ("set_default_data_node_configuration", DataNodeConfig._set_default_configuration),
  112. ("configure_csv_data_node", DataNodeConfig._configure_csv),
  113. ("configure_json_data_node", DataNodeConfig._configure_json),
  114. ("configure_sql_table_data_node", DataNodeConfig._configure_sql_table),
  115. ("configure_sql_data_node", DataNodeConfig._configure_sql),
  116. ("configure_mongo_collection_data_node", DataNodeConfig._configure_mongo_collection),
  117. ("configure_in_memory_data_node", DataNodeConfig._configure_in_memory),
  118. ("configure_pickle_data_node", DataNodeConfig._configure_pickle),
  119. ("configure_excel_data_node", DataNodeConfig._configure_excel),
  120. ("configure_generic_data_node", DataNodeConfig._configure_generic),
  121. ("configure_s3_object_data_node", DataNodeConfig._configure_s3_object),
  122. ],
  123. )
  124. _inject_section(
  125. TaskConfig,
  126. "tasks",
  127. TaskConfig.default_config(),
  128. [
  129. ("configure_task", TaskConfig._configure),
  130. ("set_default_task_configuration", TaskConfig._set_default_configuration),
  131. ],
  132. )
  133. _inject_section(
  134. ScenarioConfig,
  135. "scenarios",
  136. ScenarioConfig.default_config(),
  137. [
  138. ("configure_scenario", ScenarioConfig._configure),
  139. ("set_default_scenario_configuration", ScenarioConfig._set_default_configuration),
  140. ],
  141. )
  142. return _inject_core_sections
  143. @pytest.fixture
  144. def inject_rest_sections() -> t.Callable:
  145. """Fixture to inject core sections into the configuration."""
  146. def _inject_rest_sections() -> None:
  147. _inject_section(
  148. RestConfig,
  149. "rest",
  150. default=RestConfig.default_config(),
  151. configuration_methods=[("configure_rest", RestConfig._configure_rest)],
  152. )
  153. return _inject_rest_sections