conftest.py 6.7 KB

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