test_section.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright 2021-2025 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 os
  12. from unittest import mock
  13. import pytest
  14. from taipy.common.config.exceptions.exceptions import InvalidConfigurationId
  15. from tests.common.config.utils.section_for_tests import SectionForTest
  16. from tests.common.config.utils.unique_section_for_tests import UniqueSectionForTest
  17. class WrongUniqueSection(UniqueSectionForTest):
  18. name = "1wrong_id"
  19. class WrongSection(SectionForTest):
  20. name = "correct_name"
  21. def test_section_uses_valid_id():
  22. with pytest.raises(InvalidConfigurationId):
  23. WrongUniqueSection(attribute="foo")
  24. with pytest.raises(InvalidConfigurationId):
  25. WrongSection("wrong id", attribute="foo")
  26. with pytest.raises(InvalidConfigurationId):
  27. WrongSection("1wrong_id", attribute="foo")
  28. with pytest.raises(InvalidConfigurationId):
  29. WrongSection("wrong_@id", attribute="foo")
  30. def test_templated_properties_are_replaced():
  31. with mock.patch.dict(os.environ, {"foo": "bar", "baz": "1"}):
  32. u_sect = UniqueSectionForTest(attribute="attribute", tpl_property="ENV[foo]")
  33. assert u_sect.tpl_property == "bar"
  34. sect = SectionForTest(id="my_id", attribute="attribute", tpl_property="ENV[baz]:int")
  35. assert sect.tpl_property == 1