test_template_handler.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 datetime
  12. import os
  13. from unittest import mock
  14. import pytest
  15. from taipy.common.config.common._template_handler import _TemplateHandler
  16. from taipy.common.config.exceptions.exceptions import InconsistentEnvVariableError
  17. def test_replace_if_template():
  18. assert_does_not_change("123")
  19. assert_does_not_change("foo")
  20. assert_does_not_change("_foo")
  21. assert_does_not_change("_foo_")
  22. assert_does_not_change("foo_")
  23. assert_does_not_change("foo")
  24. assert_does_not_change("foo_1")
  25. assert_does_not_change("1foo_1")
  26. assert_does_not_change("env(foo)")
  27. assert_does_not_change("env<foo>")
  28. assert_does_not_change("env[foo]")
  29. assert_does_not_change("Env[foo]")
  30. assert_does_not_change("ENV[1foo]")
  31. assert_does_not_change("123:bool")
  32. assert_does_not_change("foo:bool")
  33. assert_does_not_change("_foo:bool")
  34. assert_does_not_change("_foo_:bool")
  35. assert_does_not_change("foo_:bool")
  36. assert_does_not_change("foo:bool")
  37. assert_does_not_change("foo_1:bool")
  38. assert_does_not_change("1foo_1:bool")
  39. assert_does_not_change("env(foo):bool")
  40. assert_does_not_change("env<foo>:bool")
  41. assert_does_not_change("env[foo]:bool")
  42. assert_does_not_change("Env[foo]:bool")
  43. assert_does_not_change("ENV[1foo]:bool")
  44. assert_does_not_change("ENV[foo]:")
  45. assert_does_not_change("ENV[_foo]:")
  46. assert_does_not_change("ENV[foo_]:")
  47. assert_does_not_change("ENV[foo0]:")
  48. assert_does_not_change("ENV[foo_0]:")
  49. assert_does_not_change("ENV[_foo_0]:")
  50. assert_does_not_change("ENV[foo]:foo")
  51. assert_does_not_change("ENV[_foo]:foo")
  52. assert_does_not_change("ENV[foo_]:foo")
  53. assert_does_not_change("ENV[foo0]:foo")
  54. assert_does_not_change("ENV[foo_0]:foo")
  55. assert_does_not_change("ENV[_foo_0]:foo")
  56. assert_does_replace("ENV[foo]", "foo", "VALUE", str)
  57. assert_does_replace("ENV[_foo]", "_foo", "VALUE", str)
  58. assert_does_replace("ENV[foo_]", "foo_", "VALUE", str)
  59. assert_does_replace("ENV[foo0]", "foo0", "VALUE", str)
  60. assert_does_replace("ENV[foo_0]", "foo_0", "VALUE", str)
  61. assert_does_replace("ENV[_foo_0]", "_foo_0", "VALUE", str)
  62. assert_does_replace("ENV[foo]:str", "foo", "VALUE", str)
  63. assert_does_replace("ENV[_foo]:str", "_foo", "VALUE", str)
  64. assert_does_replace("ENV[foo_]:str", "foo_", "VALUE", str)
  65. assert_does_replace("ENV[foo0]:str", "foo0", "VALUE", str)
  66. assert_does_replace("ENV[foo_0]:str", "foo_0", "VALUE", str)
  67. assert_does_replace("ENV[_foo_0]:str", "_foo_0", "VALUE", str)
  68. assert_does_replace("ENV[foo]:int", "foo", "1", int)
  69. assert_does_replace("ENV[_foo]:int", "_foo", "1", int)
  70. assert_does_replace("ENV[foo_]:int", "foo_", "1", int)
  71. assert_does_replace("ENV[foo0]:int", "foo0", "1", int)
  72. assert_does_replace("ENV[foo_0]:int", "foo_0", "1", int)
  73. assert_does_replace("ENV[_foo_0]:int", "_foo_0", "1", int)
  74. assert_does_replace("ENV[foo]:float", "foo", "1.", float)
  75. assert_does_replace("ENV[_foo]:float", "_foo", "1.", float)
  76. assert_does_replace("ENV[foo_]:float", "foo_", "1.", float)
  77. assert_does_replace("ENV[foo0]:float", "foo0", "1.", float)
  78. assert_does_replace("ENV[foo_0]:float", "foo_0", "1.", float)
  79. assert_does_replace("ENV[_foo_0]:float", "_foo_0", "1.", float)
  80. assert_does_replace("ENV[foo]:bool", "foo", "True", bool)
  81. assert_does_replace("ENV[_foo]:bool", "_foo", "True", bool)
  82. assert_does_replace("ENV[foo_]:bool", "foo_", "True", bool)
  83. assert_does_replace("ENV[foo0]:bool", "foo0", "True", bool)
  84. assert_does_replace("ENV[foo_0]:bool", "foo_0", "True", bool)
  85. assert_does_replace("ENV[_foo_0]:bool", "_foo_0", "True", bool)
  86. def assert_does_replace(template, env_variable_name, replaced_by, as_type):
  87. with mock.patch.dict(os.environ, {env_variable_name: replaced_by}):
  88. tpl = _TemplateHandler()
  89. assert tpl._replace_templates(template) == as_type(replaced_by)
  90. def assert_does_not_change(template):
  91. tpl = _TemplateHandler()
  92. assert tpl._replace_templates(template) == template
  93. def test_replace_tuple_list_dict():
  94. with mock.patch.dict(os.environ, {"FOO": "true", "BAR": "3", "BAZ": "qux"}):
  95. tpl = _TemplateHandler()
  96. now = datetime.datetime.now()
  97. actual = tpl._replace_templates(("ENV[FOO]:bool", now, "ENV[BAR]:int", "ENV[BAZ]", "quz"))
  98. assert actual == (True, now, 3, "qux", "quz")
  99. actual = tpl._replace_templates(("ENV[FOO]:bool", now, "ENV[BAR]:int", "ENV[BAZ]", "quz"))
  100. assert actual == (True, now, 3, "qux", "quz")
  101. def test_to_bool():
  102. with pytest.raises(InconsistentEnvVariableError):
  103. _TemplateHandler._to_bool("okhds")
  104. with pytest.raises(InconsistentEnvVariableError):
  105. _TemplateHandler._to_bool("no")
  106. with pytest.raises(InconsistentEnvVariableError):
  107. _TemplateHandler._to_bool("tru") # codespell:ignore tru
  108. with pytest.raises(InconsistentEnvVariableError):
  109. _TemplateHandler._to_bool("tru_e")
  110. assert _TemplateHandler._to_bool("true")
  111. assert _TemplateHandler._to_bool("True")
  112. assert _TemplateHandler._to_bool("TRUE")
  113. assert _TemplateHandler._to_bool("TruE")
  114. assert _TemplateHandler._to_bool("TrUE")
  115. assert not _TemplateHandler._to_bool("false")
  116. assert not _TemplateHandler._to_bool("False")
  117. assert not _TemplateHandler._to_bool("FALSE")
  118. assert not _TemplateHandler._to_bool("FalSE")
  119. assert not _TemplateHandler._to_bool("FalSe")
  120. def test_to_int():
  121. with pytest.raises(InconsistentEnvVariableError):
  122. _TemplateHandler._to_int("okhds")
  123. with pytest.raises(InconsistentEnvVariableError):
  124. _TemplateHandler._to_int("_45")
  125. with pytest.raises(InconsistentEnvVariableError):
  126. _TemplateHandler._to_int("12.5")
  127. assert 12 == _TemplateHandler._to_int("12")
  128. assert 0 == _TemplateHandler._to_int("0")
  129. assert -2 == _TemplateHandler._to_int("-2")
  130. assert 156165 == _TemplateHandler._to_int("156165")
  131. def test_to_float():
  132. with pytest.raises(InconsistentEnvVariableError):
  133. _TemplateHandler._to_float("okhds")
  134. with pytest.raises(InconsistentEnvVariableError):
  135. _TemplateHandler._to_float("_45")
  136. assert 12.5 == _TemplateHandler._to_float("12.5")
  137. assert 2.0 == _TemplateHandler._to_float("2")
  138. assert 0.0 == _TemplateHandler._to_float("0")
  139. assert -2.1 == _TemplateHandler._to_float("-2.1")
  140. assert 156165.3 == _TemplateHandler._to_float("156165.3")