test_template_handler.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 datetime
  12. import os
  13. from unittest import mock
  14. import pytest
  15. from taipy.config.common._template_handler import _TemplateHandler
  16. from taipy.config.common.frequency import Frequency
  17. from taipy.config.common.scope import Scope
  18. from taipy.config.exceptions.exceptions import InconsistentEnvVariableError
  19. def test_replace_if_template():
  20. assert_does_not_change("123")
  21. assert_does_not_change("foo")
  22. assert_does_not_change("_foo")
  23. assert_does_not_change("_foo_")
  24. assert_does_not_change("foo_")
  25. assert_does_not_change("foo")
  26. assert_does_not_change("foo_1")
  27. assert_does_not_change("1foo_1")
  28. assert_does_not_change("env(foo)")
  29. assert_does_not_change("env<foo>")
  30. assert_does_not_change("env[foo]")
  31. assert_does_not_change("Env[foo]")
  32. assert_does_not_change("ENV[1foo]")
  33. assert_does_not_change("123: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_:bool")
  38. assert_does_not_change("foo:bool")
  39. assert_does_not_change("foo_1:bool")
  40. assert_does_not_change("1foo_1:bool")
  41. assert_does_not_change("env(foo):bool")
  42. assert_does_not_change("env<foo>:bool")
  43. assert_does_not_change("env[foo]:bool")
  44. assert_does_not_change("Env[foo]:bool")
  45. assert_does_not_change("ENV[1foo]:bool")
  46. assert_does_not_change("ENV[foo]:")
  47. assert_does_not_change("ENV[_foo]:")
  48. assert_does_not_change("ENV[foo_]:")
  49. assert_does_not_change("ENV[foo0]:")
  50. assert_does_not_change("ENV[foo_0]:")
  51. assert_does_not_change("ENV[_foo_0]:")
  52. assert_does_not_change("ENV[foo]:foo")
  53. assert_does_not_change("ENV[_foo]:foo")
  54. assert_does_not_change("ENV[foo_]:foo")
  55. assert_does_not_change("ENV[foo0]:foo")
  56. assert_does_not_change("ENV[foo_0]:foo")
  57. assert_does_not_change("ENV[_foo_0]:foo")
  58. assert_does_replace("ENV[foo]", "foo", "VALUE", str)
  59. assert_does_replace("ENV[_foo]", "_foo", "VALUE", str)
  60. assert_does_replace("ENV[foo_]", "foo_", "VALUE", str)
  61. assert_does_replace("ENV[foo0]", "foo0", "VALUE", str)
  62. assert_does_replace("ENV[foo_0]", "foo_0", "VALUE", str)
  63. assert_does_replace("ENV[_foo_0]", "_foo_0", "VALUE", str)
  64. assert_does_replace("ENV[foo]:str", "foo", "VALUE", str)
  65. assert_does_replace("ENV[_foo]:str", "_foo", "VALUE", str)
  66. assert_does_replace("ENV[foo_]:str", "foo_", "VALUE", str)
  67. assert_does_replace("ENV[foo0]:str", "foo0", "VALUE", str)
  68. assert_does_replace("ENV[foo_0]:str", "foo_0", "VALUE", str)
  69. assert_does_replace("ENV[_foo_0]:str", "_foo_0", "VALUE", str)
  70. assert_does_replace("ENV[foo]:int", "foo", "1", int)
  71. assert_does_replace("ENV[_foo]:int", "_foo", "1", int)
  72. assert_does_replace("ENV[foo_]:int", "foo_", "1", int)
  73. assert_does_replace("ENV[foo0]:int", "foo0", "1", int)
  74. assert_does_replace("ENV[foo_0]:int", "foo_0", "1", int)
  75. assert_does_replace("ENV[_foo_0]:int", "_foo_0", "1", int)
  76. assert_does_replace("ENV[foo]:float", "foo", "1.", float)
  77. assert_does_replace("ENV[_foo]:float", "_foo", "1.", float)
  78. assert_does_replace("ENV[foo_]:float", "foo_", "1.", float)
  79. assert_does_replace("ENV[foo0]:float", "foo0", "1.", float)
  80. assert_does_replace("ENV[foo_0]:float", "foo_0", "1.", float)
  81. assert_does_replace("ENV[_foo_0]:float", "_foo_0", "1.", float)
  82. assert_does_replace("ENV[foo]:bool", "foo", "True", bool)
  83. assert_does_replace("ENV[_foo]:bool", "_foo", "True", bool)
  84. assert_does_replace("ENV[foo_]:bool", "foo_", "True", bool)
  85. assert_does_replace("ENV[foo0]:bool", "foo0", "True", bool)
  86. assert_does_replace("ENV[foo_0]:bool", "foo_0", "True", bool)
  87. assert_does_replace("ENV[_foo_0]:bool", "_foo_0", "True", bool)
  88. def assert_does_replace(template, env_variable_name, replaced_by, as_type):
  89. with mock.patch.dict(os.environ, {env_variable_name: replaced_by}):
  90. tpl = _TemplateHandler()
  91. assert tpl._replace_templates(template) == as_type(replaced_by)
  92. def assert_does_not_change(template):
  93. tpl = _TemplateHandler()
  94. assert tpl._replace_templates(template) == template
  95. def test_replace_tuple_list_dict():
  96. with mock.patch.dict(os.environ, {"FOO": "true", "BAR": "3", "BAZ": "qux"}):
  97. tpl = _TemplateHandler()
  98. now = datetime.datetime.now()
  99. actual = tpl._replace_templates(("ENV[FOO]:bool", now, "ENV[BAR]:int", "ENV[BAZ]", "quz"))
  100. assert actual == (True, now, 3, "qux", "quz")
  101. actual = tpl._replace_templates(("ENV[FOO]:bool", now, "ENV[BAR]:int", "ENV[BAZ]", "quz"))
  102. assert actual == (True, now, 3, "qux", "quz")
  103. def test_to_bool():
  104. with pytest.raises(InconsistentEnvVariableError):
  105. _TemplateHandler._to_bool("okhds")
  106. with pytest.raises(InconsistentEnvVariableError):
  107. _TemplateHandler._to_bool("no")
  108. with pytest.raises(InconsistentEnvVariableError):
  109. _TemplateHandler._to_bool("tru")
  110. with pytest.raises(InconsistentEnvVariableError):
  111. _TemplateHandler._to_bool("tru_e")
  112. assert _TemplateHandler._to_bool("true")
  113. assert _TemplateHandler._to_bool("True")
  114. assert _TemplateHandler._to_bool("TRUE")
  115. assert _TemplateHandler._to_bool("TruE")
  116. assert _TemplateHandler._to_bool("TrUE")
  117. assert not _TemplateHandler._to_bool("false")
  118. assert not _TemplateHandler._to_bool("False")
  119. assert not _TemplateHandler._to_bool("FALSE")
  120. assert not _TemplateHandler._to_bool("FalSE")
  121. assert not _TemplateHandler._to_bool("FalSe")
  122. def test_to_int():
  123. with pytest.raises(InconsistentEnvVariableError):
  124. _TemplateHandler._to_int("okhds")
  125. with pytest.raises(InconsistentEnvVariableError):
  126. _TemplateHandler._to_int("_45")
  127. with pytest.raises(InconsistentEnvVariableError):
  128. _TemplateHandler._to_int("12.5")
  129. assert 12 == _TemplateHandler._to_int("12")
  130. assert 0 == _TemplateHandler._to_int("0")
  131. assert -2 == _TemplateHandler._to_int("-2")
  132. assert 156165 == _TemplateHandler._to_int("156165")
  133. def test_to_float():
  134. with pytest.raises(InconsistentEnvVariableError):
  135. _TemplateHandler._to_float("okhds")
  136. with pytest.raises(InconsistentEnvVariableError):
  137. _TemplateHandler._to_float("_45")
  138. assert 12.5 == _TemplateHandler._to_float("12.5")
  139. assert 2.0 == _TemplateHandler._to_float("2")
  140. assert 0.0 == _TemplateHandler._to_float("0")
  141. assert -2.1 == _TemplateHandler._to_float("-2.1")
  142. assert 156165.3 == _TemplateHandler._to_float("156165.3")
  143. def test_to_scope():
  144. with pytest.raises(InconsistentEnvVariableError):
  145. _TemplateHandler._to_scope("okhds")
  146. with pytest.raises(InconsistentEnvVariableError):
  147. _TemplateHandler._to_scope("plop")
  148. assert Scope.GLOBAL == _TemplateHandler._to_scope("global")
  149. assert Scope.GLOBAL == _TemplateHandler._to_scope("GLOBAL")
  150. assert Scope.SCENARIO == _TemplateHandler._to_scope("SCENARIO")
  151. assert Scope.CYCLE == _TemplateHandler._to_scope("cycle")
  152. def test_to_frequency():
  153. with pytest.raises(InconsistentEnvVariableError):
  154. _TemplateHandler._to_frequency("okhds")
  155. with pytest.raises(InconsistentEnvVariableError):
  156. _TemplateHandler._to_frequency("plop")
  157. assert Frequency.DAILY == _TemplateHandler._to_frequency("DAILY")
  158. assert Frequency.DAILY == _TemplateHandler._to_frequency("Daily")
  159. assert Frequency.WEEKLY == _TemplateHandler._to_frequency("weekly")
  160. assert Frequency.WEEKLY == _TemplateHandler._to_frequency("WEEKLY")
  161. assert Frequency.MONTHLY == _TemplateHandler._to_frequency("Monthly")
  162. assert Frequency.MONTHLY == _TemplateHandler._to_frequency("MONThLY")
  163. assert Frequency.QUARTERLY == _TemplateHandler._to_frequency("QuaRtERlY")
  164. assert Frequency.YEARLY == _TemplateHandler._to_frequency("Yearly")