test_core_section.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2023 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 os
  12. from unittest.mock import patch
  13. import pytest
  14. from taipy.core import Core
  15. from taipy.core._version._version_manager_factory import _VersionManagerFactory
  16. from taipy.core.config import CoreSection
  17. from tests.core.utils.named_temporary_file import NamedTemporaryFile
  18. from taipy.config import Config
  19. from taipy.config.exceptions.exceptions import MissingEnvVariableError
  20. def test_core_section():
  21. with patch("sys.argv", ["prog"]):
  22. core = Core()
  23. core.run()
  24. assert Config.core.mode == "development"
  25. assert Config.core.version_number == _VersionManagerFactory._build_manager()._get_development_version()
  26. assert not Config.core.force
  27. core.stop()
  28. with patch("sys.argv", ["prog"]):
  29. Config.configure_core(mode="experiment", version_number="test_num", force=True)
  30. core = Core()
  31. core.run()
  32. assert Config.core.mode == "experiment"
  33. assert Config.core.version_number == "test_num"
  34. assert Config.core.force
  35. core.stop()
  36. toml_config = NamedTemporaryFile(
  37. content="""
  38. [TAIPY]
  39. [CORE]
  40. mode = "production"
  41. version_number = "test_num_2"
  42. force = "true:bool"
  43. """
  44. )
  45. Config.load(toml_config.filename)
  46. with patch("sys.argv", ["prog"]):
  47. core = Core()
  48. core.run()
  49. assert Config.core.mode == "production"
  50. assert Config.core.version_number == "test_num_2"
  51. assert Config.core.force
  52. core.stop()
  53. with patch("sys.argv", ["prog", "--experiment", "test_num_3", "--no-taipy-force"]):
  54. core = Core()
  55. core.run()
  56. assert Config.core.mode == "experiment"
  57. assert Config.core.version_number == "test_num_3"
  58. assert not Config.core.force
  59. core.stop()
  60. def test_config_attribute_overiden_by_code_config_including_env_variable_values():
  61. assert Config.core.root_folder == CoreSection._DEFAULT_ROOT_FOLDER
  62. assert Config.core.storage_folder == CoreSection._DEFAULT_STORAGE_FOLDER
  63. Config.configure_core(root_folder="ENV[ROOT_FOLDER]", storage_folder="ENV[STORAGE_FOLDER]")
  64. with pytest.raises(MissingEnvVariableError):
  65. Config.core.root_folder
  66. with pytest.raises(MissingEnvVariableError):
  67. Config.core.storage_folder
  68. with patch.dict(os.environ, {"ROOT_FOLDER": "foo", "STORAGE_FOLDER": "bar"}):
  69. assert Config.core.root_folder == "foo"
  70. assert Config.core.storage_folder == "bar"
  71. with patch.dict(os.environ, {"ROOT_FOLDER": "baz", "STORAGE_FOLDER": "qux"}):
  72. assert Config.core.root_folder == "baz"
  73. assert Config.core.storage_folder == "qux"
  74. def test_clean_config():
  75. core_config = Config.configure_core(mode="experiment", version_number="test_num", force=True)
  76. assert Config.core is core_config
  77. core_config._clean()
  78. # Check if the instance before and after _clean() is the same
  79. assert Config.core is core_config
  80. assert core_config.mode == "development"
  81. assert core_config.version_number == ""
  82. assert core_config.force is False
  83. assert core_config.properties == {}