test_evaluator.py 3.4 KB

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