test_section_registration.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 import Config
  13. from taipy.config.exceptions.exceptions import ConfigurationUpdateBlocked
  14. from tests.config.utils.section_for_tests import SectionForTest
  15. from tests.config.utils.unique_section_for_tests import UniqueSectionForTest
  16. def test_unique_section_registration_and_usage():
  17. assert Config.unique_sections is not None
  18. assert Config.unique_sections[UniqueSectionForTest.name] is not None
  19. assert Config.unique_sections[UniqueSectionForTest.name].attribute == "default_attribute"
  20. assert Config.unique_sections[UniqueSectionForTest.name].prop is None
  21. mySection = Config.configure_unique_section_for_tests(attribute="my_attribute", prop="my_prop")
  22. assert Config.unique_sections is not None
  23. assert Config.unique_sections[UniqueSectionForTest.name] is not None
  24. assert mySection is not None
  25. assert Config.unique_sections[UniqueSectionForTest.name].attribute == "my_attribute"
  26. assert mySection.attribute == "my_attribute"
  27. assert Config.unique_sections[UniqueSectionForTest.name].prop == "my_prop"
  28. assert mySection.prop == "my_prop"
  29. myNewSection = Config.configure_unique_section_for_tests(attribute="my_new_attribute", prop="my_new_prop")
  30. assert Config.unique_sections is not None
  31. assert Config.unique_sections[UniqueSectionForTest.name] is not None
  32. assert myNewSection is not None
  33. assert mySection is not None
  34. assert Config.unique_sections[UniqueSectionForTest.name].attribute == "my_new_attribute"
  35. assert myNewSection.attribute == "my_new_attribute"
  36. assert mySection.attribute == "my_new_attribute"
  37. assert Config.unique_sections[UniqueSectionForTest.name].prop == "my_new_prop"
  38. assert myNewSection.prop == "my_new_prop"
  39. assert mySection.prop == "my_new_prop"
  40. def test_sections_exposed_as_attribute():
  41. assert Config.unique_section_name.attribute == "default_attribute"
  42. Config.configure_unique_section_for_tests("my_attribute")
  43. assert Config.unique_section_name.attribute == "my_attribute"
  44. assert Config.section_name["default"].attribute == "default_attribute"
  45. Config.configure_section_for_tests(id="my_id", attribute="my_attribute")
  46. assert Config.section_name["my_id"].attribute == "my_attribute"
  47. def test_section_registration_and_usage():
  48. assert Config.sections is not None
  49. assert len(Config.sections) == 1
  50. assert Config.sections[SectionForTest.name] is not None
  51. assert len(Config.sections[SectionForTest.name]) == 1
  52. assert Config.sections[SectionForTest.name]["default"] is not None
  53. assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
  54. assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
  55. assert Config.sections[SectionForTest.name]["default"].foo is None
  56. myFirstSection = Config.configure_section_for_tests(id="first", attribute="my_attribute", prop="my_prop", foo="bar")
  57. assert Config.sections is not None
  58. assert len(Config.sections) == 1
  59. assert Config.sections[SectionForTest.name] is not None
  60. assert len(Config.sections[SectionForTest.name]) == 2
  61. assert Config.sections[SectionForTest.name]["default"] is not None
  62. assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
  63. assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
  64. assert Config.sections[SectionForTest.name]["default"].foo is None
  65. assert Config.sections[SectionForTest.name]["first"] is not None
  66. assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
  67. assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
  68. assert Config.sections[SectionForTest.name]["first"].foo == "bar"
  69. assert myFirstSection.attribute == "my_attribute"
  70. assert myFirstSection.prop == "my_prop"
  71. assert myFirstSection.foo == "bar"
  72. myNewSection = Config.configure_section_for_tests(id="second", attribute="my_new_attribute", prop="my_new_prop")
  73. assert Config.sections is not None
  74. assert len(Config.sections) == 1
  75. assert Config.sections[SectionForTest.name] is not None
  76. assert len(Config.sections[SectionForTest.name]) == 3
  77. assert Config.sections[SectionForTest.name]["default"] is not None
  78. assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
  79. assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
  80. assert Config.sections[SectionForTest.name]["default"].foo is None
  81. assert Config.sections[SectionForTest.name]["first"] is not None
  82. assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
  83. assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
  84. assert Config.sections[SectionForTest.name]["first"].foo == "bar"
  85. assert Config.sections[SectionForTest.name]["second"] is not None
  86. assert Config.sections[SectionForTest.name]["second"].attribute == "my_new_attribute"
  87. assert Config.sections[SectionForTest.name]["second"].prop == "my_new_prop"
  88. assert Config.sections[SectionForTest.name]["second"].foo is None
  89. assert myFirstSection.attribute == "my_attribute"
  90. assert myFirstSection.prop == "my_prop"
  91. assert myFirstSection.foo == "bar"
  92. assert myNewSection.attribute == "my_new_attribute"
  93. assert myNewSection.prop == "my_new_prop"
  94. assert myNewSection.foo is None
  95. my2ndSection = Config.configure_section_for_tests(id="second", attribute="my_2nd_attribute", prop="my_2nd_prop")
  96. assert Config.sections is not None
  97. assert len(Config.sections) == 1
  98. assert Config.sections[SectionForTest.name] is not None
  99. assert len(Config.sections[SectionForTest.name]) == 3
  100. assert Config.sections[SectionForTest.name]["default"] is not None
  101. assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
  102. assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
  103. assert Config.sections[SectionForTest.name]["default"].foo is None
  104. assert Config.sections[SectionForTest.name]["first"] is not None
  105. assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
  106. assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
  107. assert Config.sections[SectionForTest.name]["first"].foo == "bar"
  108. assert Config.sections[SectionForTest.name]["second"] is not None
  109. assert Config.sections[SectionForTest.name]["second"].attribute == "my_2nd_attribute"
  110. assert Config.sections[SectionForTest.name]["second"].prop == "my_2nd_prop"
  111. assert Config.sections[SectionForTest.name]["second"].foo is None
  112. assert myFirstSection.attribute == "my_attribute"
  113. assert myFirstSection.prop == "my_prop"
  114. assert myFirstSection.foo == "bar"
  115. assert myNewSection.attribute == "my_2nd_attribute"
  116. assert myNewSection.prop == "my_2nd_prop"
  117. assert myNewSection.foo is None
  118. assert my2ndSection.attribute == "my_2nd_attribute"
  119. assert my2ndSection.prop == "my_2nd_prop"
  120. assert my2ndSection.foo is None
  121. def test_block_registration():
  122. myUniqueSection = Config.configure_unique_section_for_tests(attribute="my_unique_attribute", prop="my_unique_prop")
  123. mySection = Config.configure_section_for_tests(id="section_id", attribute="my_attribute", prop="my_prop", foo="bar")
  124. Config.block_update()
  125. with pytest.raises(ConfigurationUpdateBlocked):
  126. Config.configure_unique_section_for_tests(attribute="my_new_unique_attribute", prop="my_new_unique_prop")
  127. with pytest.raises(ConfigurationUpdateBlocked):
  128. Config.configure_section_for_tests(id="new", attribute="my_attribute", prop="my_prop", foo="bar")
  129. with pytest.raises(ConfigurationUpdateBlocked):
  130. myUniqueSection.attribute = "foo"
  131. with pytest.raises(ConfigurationUpdateBlocked):
  132. myUniqueSection.properties = {"foo": "bar"}
  133. # myUniqueSection stay the same
  134. assert myUniqueSection.attribute == "my_unique_attribute"
  135. assert myUniqueSection.properties == {"prop": "my_unique_prop"}
  136. with pytest.raises(ConfigurationUpdateBlocked):
  137. mySection.attribute = "foo"
  138. with pytest.raises(ConfigurationUpdateBlocked):
  139. mySection.properties = {"foo": "foo"}
  140. # mySection stay the same
  141. assert mySection.attribute == "my_attribute"
  142. assert mySection.properties == {"prop": "my_prop", "foo": "bar", "prop_int": 0}