1
0

test_compilation.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. import pytest
  12. from taipy.config.config import Config
  13. from taipy.config.section import Section
  14. from tests.config.utils.named_temporary_file import NamedTemporaryFile
  15. from tests.config.utils.section_for_tests import SectionForTest
  16. from tests.config.utils.section_of_sections_list_for_tests import SectionOfSectionsListForTest
  17. @pytest.fixture
  18. def _init_list_section_for_test():
  19. Config._register_default(SectionOfSectionsListForTest(Section._DEFAULT_KEY, [], prop="default_prop", prop_int=0))
  20. Config.configure_list_section_for_tests = SectionOfSectionsListForTest._configure
  21. Config.list_section_name = Config.sections[SectionOfSectionsListForTest.name]
  22. def test_applied_config_compilation_does_not_change_other_configs():
  23. assert len(Config._default_config._unique_sections) == 1
  24. assert Config._default_config._unique_sections["unique_section_name"] is not None
  25. assert Config._default_config._unique_sections["unique_section_name"].attribute == "default_attribute"
  26. assert Config._default_config._unique_sections["unique_section_name"].prop is None
  27. assert len(Config._python_config._unique_sections) == 0
  28. assert len(Config._file_config._unique_sections) == 0
  29. assert len(Config._env_file_config._unique_sections) == 0
  30. assert len(Config._applied_config._unique_sections) == 1
  31. assert Config._applied_config._unique_sections["unique_section_name"] is not None
  32. assert Config._applied_config._unique_sections["unique_section_name"].attribute == "default_attribute"
  33. assert Config._applied_config._unique_sections["unique_section_name"].prop is None
  34. assert len(Config.unique_sections) == 1
  35. assert Config.unique_sections["unique_section_name"] is not None
  36. assert Config.unique_sections["unique_section_name"].attribute == "default_attribute"
  37. assert Config.unique_sections["unique_section_name"].prop is None
  38. assert (
  39. Config._applied_config._unique_sections["unique_section_name"]
  40. is not Config._default_config._unique_sections["unique_section_name"]
  41. )
  42. Config.configure_unique_section_for_tests("qwe", prop="rty")
  43. assert len(Config._default_config._unique_sections) == 1
  44. assert Config._default_config._unique_sections["unique_section_name"] is not None
  45. assert Config._default_config._unique_sections["unique_section_name"].attribute == "default_attribute"
  46. assert Config._default_config._unique_sections["unique_section_name"].prop is None
  47. assert len(Config._python_config._unique_sections) == 1
  48. assert Config._python_config._unique_sections["unique_section_name"] is not None
  49. assert Config._python_config._unique_sections["unique_section_name"].attribute == "qwe"
  50. assert Config._python_config._unique_sections["unique_section_name"].prop == "rty"
  51. assert (
  52. Config._python_config._unique_sections["unique_section_name"]
  53. != Config._default_config._unique_sections["unique_section_name"]
  54. )
  55. assert len(Config._file_config._unique_sections) == 0
  56. assert len(Config._env_file_config._unique_sections) == 0
  57. assert len(Config._applied_config._unique_sections) == 1
  58. assert Config._applied_config._unique_sections["unique_section_name"] is not None
  59. assert Config._applied_config._unique_sections["unique_section_name"].attribute == "qwe"
  60. assert Config._applied_config._unique_sections["unique_section_name"].prop == "rty"
  61. assert (
  62. Config._python_config._unique_sections["unique_section_name"]
  63. != Config._applied_config._unique_sections["unique_section_name"]
  64. )
  65. assert (
  66. Config._default_config._unique_sections["unique_section_name"]
  67. != Config._applied_config._unique_sections["unique_section_name"]
  68. )
  69. assert len(Config.unique_sections) == 1
  70. assert Config.unique_sections["unique_section_name"] is not None
  71. assert Config.unique_sections["unique_section_name"].attribute == "qwe"
  72. assert Config.unique_sections["unique_section_name"].prop == "rty"
  73. def test_nested_section_instance_in_python(_init_list_section_for_test):
  74. s1_cfg = Config.configure_section_for_tests("s1", attribute="foo")
  75. s2_cfg = Config.configure_section_for_tests("s2", attribute="bar")
  76. ss_cfg = Config.configure_list_section_for_tests("ss", attribute="foo", sections_list=[s1_cfg, s2_cfg])
  77. s1_config_applied_instance = Config.section_name["s1"]
  78. s1_config_python_instance = Config._python_config._sections[SectionForTest.name]["s1"]
  79. s2_config_applied_instance = Config.section_name["s2"]
  80. s2_config_python_instance = Config._python_config._sections[SectionForTest.name]["s2"]
  81. assert ss_cfg.sections_list[0] is s1_config_applied_instance
  82. assert ss_cfg.sections_list[0] is not s1_config_python_instance
  83. assert ss_cfg.sections_list[1] is s2_config_applied_instance
  84. assert ss_cfg.sections_list[1] is not s2_config_python_instance
  85. def _configure_in_toml():
  86. return NamedTemporaryFile(
  87. content="""
  88. [TAIPY]
  89. [section_name.s1]
  90. attribute = "foo"
  91. [section_name.s2]
  92. attribute = "bar"
  93. [list_section_name.ss]
  94. sections_list = [ "foo", "s1:SECTION", "s2:SECTION"]
  95. """
  96. )
  97. def test_nested_section_instance_load_toml(_init_list_section_for_test):
  98. toml_config = _configure_in_toml()
  99. Config.load(toml_config)
  100. s1_config_applied_instance = Config.section_name["s1"]
  101. s1_config_python_instance = Config._python_config._sections[SectionForTest.name]["s1"]
  102. s2_config_applied_instance = Config.section_name["s2"]
  103. s2_config_python_instance = Config._python_config._sections[SectionForTest.name]["s2"]
  104. ss_cfg = Config.list_section_name["ss"]
  105. assert ss_cfg.sections_list[0] == "foo"
  106. assert ss_cfg.sections_list[1] is s1_config_applied_instance
  107. assert ss_cfg.sections_list[1] is not s1_config_python_instance
  108. assert ss_cfg.sections_list[2] is s2_config_applied_instance
  109. assert ss_cfg.sections_list[2] is not s2_config_python_instance
  110. def test_nested_section_instance_override_toml(_init_list_section_for_test):
  111. toml_config = _configure_in_toml()
  112. Config.override(toml_config)
  113. s1_config_applied_instance = Config.section_name["s1"]
  114. s1_config_python_instance = Config._file_config._sections[SectionForTest.name]["s1"]
  115. s2_config_applied_instance = Config.section_name["s2"]
  116. s2_config_python_instance = Config._file_config._sections[SectionForTest.name]["s2"]
  117. ss_cfg = Config.list_section_name["ss"]
  118. assert ss_cfg.sections_list[0] == "foo"
  119. assert ss_cfg.sections_list[1] is s1_config_applied_instance
  120. assert ss_cfg.sections_list[1] is not s1_config_python_instance
  121. assert ss_cfg.sections_list[2] is s2_config_applied_instance
  122. assert ss_cfg.sections_list[1] is not s2_config_python_instance