test_evaluator.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 inspect
  12. import warnings
  13. from unittest.mock import patch
  14. from flask import g
  15. from taipy.gui import Gui
  16. from taipy.gui.utils.types import _TaipyNumber
  17. def test_unbind_variable_in_expression(gui: Gui, helpers):
  18. with patch("sys.argv", ["prog"]):
  19. gui.run(run_server=False, single_client=True)
  20. with warnings.catch_warnings(record=True) as records:
  21. with gui.get_flask_app().app_context():
  22. gui._evaluate_expr("{x}")
  23. warns = helpers.get_taipy_warnings(records)
  24. assert len(warns) == 3
  25. assert "Variable 'x' is not available in" in str(warns[0].message)
  26. assert "Variable 'x' is not defined" in str(warns[1].message)
  27. assert "Cannot evaluate expression 'x'" in str(warns[2].message)
  28. assert "name 'x' is not defined" in str(warns[2].message)
  29. def test_evaluate_same_expression_multiple_times(gui: Gui):
  30. x = 10 # noqa: F841
  31. gui._set_frame(inspect.currentframe())
  32. with patch("sys.argv", ["prog"]):
  33. gui.run(run_server=False, single_client=True)
  34. with gui.get_flask_app().app_context():
  35. s1 = gui._evaluate_expr("x + 10 = {x + 10}")
  36. s2 = gui._evaluate_expr("x + 10 = {x + 10}")
  37. assert s1 == s2
  38. def test_evaluate_expressions_same_variable(gui: Gui):
  39. x = 10 # noqa: F841
  40. gui._set_frame(inspect.currentframe())
  41. with patch("sys.argv", ["prog"]):
  42. gui.run(run_server=False, single_client=True)
  43. with gui.get_flask_app().app_context():
  44. s1 = gui._evaluate_expr("x + 10 = {x + 10}")
  45. s2 = gui._evaluate_expr("x = {x}")
  46. assert "tp_TpExPr_x" in s1 and "tp_TpExPr_x" in s2
  47. def test_evaluate_holder(gui: Gui):
  48. x = 10 # noqa: F841
  49. gui._set_frame(inspect.currentframe())
  50. with patch("sys.argv", ["prog"]):
  51. gui.run(run_server=False, single_client=True)
  52. with warnings.catch_warnings(record=True):
  53. with gui.get_flask_app().app_context():
  54. gui._evaluate_expr("{x + 10}")
  55. hash = gui._evaluate_bind_holder(_TaipyNumber, "TpExPr_x + 10_TPMDL_0")
  56. assert "_TpN_tp_TpExPr_x_10_TPMDL_0_0" in hash
  57. lst = gui._evaluate_holders("TpExPr_x + 10_TPMDL_0")
  58. assert len(lst) == 1
  59. assert "_TpN_tp_TpExPr_x_10_TPMDL_0_0" in lst[0]
  60. # test re-evaluate holders
  61. gui._bindings().x = 20
  62. gui._re_evaluate_expr(lst[0])
  63. def test_evaluate_not_expression_type(gui: Gui):
  64. with patch("sys.argv", ["prog"]):
  65. gui.run(run_server=False)
  66. with gui.get_flask_app().app_context():
  67. assert "x + 10" == gui._evaluate_expr("x + 10")
  68. def test_evaluate_expression_2_clients(gui: Gui):
  69. x = 10 # noqa: F841
  70. y = 20 # noqa: F841
  71. gui._set_frame(inspect.currentframe())
  72. with patch("sys.argv", ["prog"]):
  73. gui.run(run_server=False)
  74. with gui.get_flask_app().app_context():
  75. gui._bindings()._get_or_create_scope("A")
  76. gui._bindings()._get_or_create_scope("B")
  77. g.client_id = "A"
  78. gui._evaluate_expr("x + y = {x + y}")
  79. g.client_id = "B"
  80. gui._evaluate_expr("x")
  81. gui._re_evaluate_expr("x")