test_core_version.py 5.5 KB

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