conftest.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. @pytest.fixture(scope="session")
  29. def e2e_base_url(request: pytest.FixtureRequest) -> str:
  30. """Fixture to get the base URL for e2e testing."""
  31. return request.config.getoption("--e2e-base-url")
  32. @pytest.fixture(scope="session")
  33. def e2e_port(request: pytest.FixtureRequest) -> str:
  34. """Fixture to get the port for e2e testing."""
  35. return request.config.getoption("--e2e-port")
  36. def remove_subparser(name: str) -> None:
  37. """Remove a subparser from argparse."""
  38. _TaipyParser._sub_taipyparsers.pop(name, None)
  39. if _TaipyParser._subparser_action:
  40. _TaipyParser._subparser_action._name_parser_map.pop(name, None)
  41. for action in _TaipyParser._subparser_action._choices_actions:
  42. if action.dest == name:
  43. _TaipyParser._subparser_action._choices_actions.remove(action)
  44. @pytest.fixture
  45. def clean_argparser() -> t.Callable:
  46. """Fixture to clean the argument parser."""
  47. def _clean_argparser() -> None:
  48. _TaipyParser._parser = argparse.ArgumentParser(conflict_handler="resolve")
  49. _TaipyParser._subparser_action = None
  50. _TaipyParser._arg_groups = {}
  51. subcommands = list(_TaipyParser._sub_taipyparsers.keys())
  52. for subcommand in subcommands:
  53. remove_subparser(subcommand)
  54. return _clean_argparser
  55. @pytest.fixture
  56. def reset_configuration_singleton() -> t.Callable:
  57. """Fixture to reset the configuration singleton."""
  58. def _reset_configuration_singleton() -> None:
  59. Config.unblock_update()
  60. Config._default_config = _Config()._default_config()
  61. Config._python_config = _Config()
  62. Config._file_config = _Config()
  63. Config._env_file_config = _Config()
  64. Config._applied_config = _Config()
  65. Config._collector = IssueCollector()
  66. Config._serializer = _TomlSerializer()
  67. Config._comparator = _ConfigComparator()
  68. _BaseSerializer._SERIALIZABLE_TYPES = [
  69. "bool",
  70. "str",
  71. "int",
  72. "float",
  73. "datetime",
  74. "timedelta",
  75. "function",
  76. "class",
  77. "SECTION",
  78. ]
  79. _Checker._checkers = []
  80. return _reset_configuration_singleton
  81. @pytest.fixture
  82. def inject_core_sections() -> t.Callable:
  83. """Fixture to inject core sections into the configuration."""
  84. def _inject_core_sections() -> None:
  85. _inject_section(
  86. JobConfig,
  87. "job_config",
  88. JobConfig("development"),
  89. [("configure_job_executions", JobConfig._configure)],
  90. True,
  91. )
  92. _inject_section(
  93. CoreSection,
  94. "core",
  95. CoreSection.default_config(),
  96. [("configure_core", CoreSection._configure)],
  97. add_to_unconflicted_sections=True,
  98. )
  99. _inject_section(
  100. DataNodeConfig,
  101. "data_nodes",
  102. DataNodeConfig.default_config(),
  103. [
  104. ("configure_data_node", DataNodeConfig._configure),
  105. ("configure_data_node_from", DataNodeConfig._configure_from),
  106. ("set_default_data_node_configuration", DataNodeConfig._set_default_configuration),
  107. ("configure_csv_data_node", DataNodeConfig._configure_csv),
  108. ("configure_json_data_node", DataNodeConfig._configure_json),
  109. ("configure_sql_table_data_node", DataNodeConfig._configure_sql_table),
  110. ("configure_sql_data_node", DataNodeConfig._configure_sql),
  111. ("configure_mongo_collection_data_node", DataNodeConfig._configure_mongo_collection),
  112. ("configure_in_memory_data_node", DataNodeConfig._configure_in_memory),
  113. ("configure_pickle_data_node", DataNodeConfig._configure_pickle),
  114. ("configure_excel_data_node", DataNodeConfig._configure_excel),
  115. ("configure_generic_data_node", DataNodeConfig._configure_generic),
  116. ("configure_s3_object_data_node", DataNodeConfig._configure_s3_object),
  117. ],
  118. )
  119. _inject_section(
  120. TaskConfig,
  121. "tasks",
  122. TaskConfig.default_config(),
  123. [
  124. ("configure_task", TaskConfig._configure),
  125. ("set_default_task_configuration", TaskConfig._set_default_configuration),
  126. ],
  127. )
  128. _inject_section(
  129. ScenarioConfig,
  130. "scenarios",
  131. ScenarioConfig.default_config(),
  132. [
  133. ("configure_scenario", ScenarioConfig._configure),
  134. ("set_default_scenario_configuration", ScenarioConfig._set_default_configuration),
  135. ],
  136. )
  137. return _inject_core_sections
  138. @pytest.fixture
  139. def inject_rest_sections() -> t.Callable:
  140. """Fixture to inject core sections into the configuration."""
  141. def _inject_rest_sections() -> None:
  142. _inject_section(
  143. RestConfig,
  144. "rest",
  145. default=RestConfig.default_config(),
  146. configuration_methods=[("configure_rest", RestConfig._configure_rest)],
  147. )
  148. return _inject_rest_sections