test_core_version.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.config.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. [VERSION_MIGRATION.migration_fcts]
  70. """
  71. )
  72. if is_compatible:
  73. Config.load(file_config.filename)
  74. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION
  75. else:
  76. with pytest.raises(ConfigCoreVersionMismatched):
  77. Config.load(file_config.filename)
  78. @pytest.mark.parametrize("core_version,is_compatible", core_version_is_compatible)
  79. def test_override_configuration_file(self, core_version, is_compatible):
  80. file_config = NamedTemporaryFile(
  81. f"""
  82. [TAIPY]
  83. [JOB]
  84. mode = "standalone"
  85. max_nb_of_workers = "2:int"
  86. [CORE]
  87. root_folder = "./taipy/"
  88. storage_folder = ".data/"
  89. repository_type = "filesystem"
  90. read_entity_retry = "0:int"
  91. mode = "development"
  92. version_number = ""
  93. force = "False:bool"
  94. core_version = "{core_version}"
  95. [VERSION_MIGRATION.migration_fcts]
  96. """
  97. )
  98. if is_compatible:
  99. Config.override(file_config.filename)
  100. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION
  101. else:
  102. with pytest.raises(ConfigCoreVersionMismatched):
  103. Config.override(file_config.filename)
  104. def test_load_configuration_file_without_core_section(self):
  105. file_config = NamedTemporaryFile(
  106. """
  107. [TAIPY]
  108. [JOB]
  109. mode = "standalone"
  110. max_nb_of_workers = "2:int"
  111. [CORE]
  112. root_folder = "./taipy/"
  113. storage_folder = ".data/"
  114. repository_type = "filesystem"
  115. read_entity_retry = "0:int"
  116. mode = "development"
  117. version_number = ""
  118. force = "False:bool"
  119. [VERSION_MIGRATION.migration_fcts]
  120. """
  121. )
  122. Config.load(file_config.filename)
  123. assert Config.unique_sections[CoreSection.name]._core_version == _MOCK_CORE_VERSION