test_core_version.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2021-2024 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. from unittest import mock
  12. import pytest
  13. from taipy.common.config import Config
  14. from taipy.core._init_version import _read_version
  15. from taipy.core.config.core_section import CoreSection
  16. from taipy.core.exceptions import ConfigCoreVersionMismatched
  17. from tests.core.utils.named_temporary_file import NamedTemporaryFile
  18. _MOCK_CORE_VERSION = "3.1.1"
  19. @pytest.fixture(scope="function", autouse=True)
  20. def mock_core_version():
  21. with mock.patch("taipy.core.config.core_section._read_version") as mock_read_version:
  22. mock_read_version.return_value = _MOCK_CORE_VERSION
  23. CoreSection._CURRENT_CORE_VERSION = _MOCK_CORE_VERSION
  24. Config.unique_sections[CoreSection.name] = CoreSection.default_config()
  25. Config._default_config._unique_sections[CoreSection.name] = CoreSection.default_config()
  26. Config._python_config._unique_sections[CoreSection.name] = CoreSection.default_config()
  27. yield
  28. CoreSection._CURRENT_CORE_VERSION = _read_version()
  29. class TestCoreVersionInCoreSectionConfig:
  30. major, minor, patch = _MOCK_CORE_VERSION.split(".")
  31. current_version = f"{major}.{minor}.{patch}"
  32. current_dev_version = f"{major}.{minor}.{patch}.dev0"
  33. compatible_future_version = f"{major}.{minor}.{int(patch) + 1}"
  34. compatible_future_dev_version = f"{major}.{minor}.{int(patch) + 1}.dev0"
  35. core_version_is_compatible = [
  36. # Current version and dev version should be compatible
  37. (f"{major}.{minor}.{patch}", True),
  38. (f"{major}.{minor}.{patch}.dev0", True),
  39. # Future versions with same major and minor should be compatible
  40. (f"{major}.{minor}.{int(patch) + 1}", True),
  41. (f"{major}.{minor}.{int(patch) + 1}.dev0", True),
  42. # Past versions with same major and minor should be compatible
  43. (f"{major}.{minor}.{int(patch) - 1}", True),
  44. (f"{major}.{minor}.{int(patch) - 1}.dev0", True),
  45. # Future versions with different minor number should be incompatible
  46. (f"{major}.{int(minor) + 1}.{patch}", False),
  47. (f"{major}.{int(minor) + 1}.{patch}.dev0", False),
  48. # Past versions with different minor number should be incompatible
  49. (f"{major}.{int(minor) - 1}.{patch}", False),
  50. (f"{major}.{int(minor) - 1}.{patch}.dev0", False),
  51. ]
  52. @pytest.mark.parametrize("core_version, is_compatible", core_version_is_compatible)
  53. def test_load_configuration_file(self, core_version, is_compatible):
  54. file_config = NamedTemporaryFile(
  55. f"""
  56. [TAIPY]
  57. [JOB]
  58. mode = "standalone"
  59. max_nb_of_workers = "2:int"
  60. [CORE]
  61. root_folder = "./taipy/"
  62. storage_folder = ".data/"
  63. repository_type = "filesystem"
  64. read_entity_retry = "0:int"
  65. mode = "development"
  66. version_number = ""
  67. force = "False:bool"
  68. core_version = "{core_version}"
  69. """
  70. )
  71. if is_compatible:
  72. Config.load(file_config.filename)
  73. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION
  74. else:
  75. with pytest.raises(ConfigCoreVersionMismatched):
  76. Config.load(file_config.filename)
  77. @pytest.mark.parametrize("core_version,is_compatible", core_version_is_compatible)
  78. def test_override_configuration_file(self, core_version, is_compatible):
  79. file_config = NamedTemporaryFile(
  80. f"""
  81. [TAIPY]
  82. [JOB]
  83. mode = "standalone"
  84. max_nb_of_workers = "2:int"
  85. [CORE]
  86. root_folder = "./taipy/"
  87. storage_folder = ".data/"
  88. repository_type = "filesystem"
  89. read_entity_retry = "0:int"
  90. mode = "development"
  91. version_number = ""
  92. force = "False:bool"
  93. core_version = "{core_version}"
  94. """
  95. )
  96. if is_compatible:
  97. Config.override(file_config.filename)
  98. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION
  99. else:
  100. with pytest.raises(ConfigCoreVersionMismatched):
  101. Config.override(file_config.filename)
  102. def test_load_configuration_file_without_core_section(self):
  103. file_config = NamedTemporaryFile(
  104. """
  105. [TAIPY]
  106. [JOB]
  107. mode = "standalone"
  108. max_nb_of_workers = "2:int"
  109. [CORE]
  110. root_folder = "./taipy/"
  111. storage_folder = ".data/"
  112. repository_type = "filesystem"
  113. read_entity_retry = "0:int"
  114. mode = "development"
  115. version_number = ""
  116. force = "False:bool"
  117. """
  118. )
  119. Config.load(file_config.filename)
  120. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION