test_u.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. from unittest.mock import patch
  13. from taipy.gui import Gui, Markdown
  14. def ws_u_assert_template(gui: Gui, helpers, value_before_update, value_after_update, payload):
  15. # Bind test variable
  16. var = value_before_update # noqa: F841
  17. # set gui frame
  18. gui._set_frame(inspect.currentframe())
  19. # Bind a page so that the variable will be evaluated as expression
  20. gui.add_page("test", Markdown("<|{var}|>"))
  21. with patch("sys.argv", ["prog"]):
  22. gui.run(run_server=False)
  23. flask_client = gui._server.test_client()
  24. # WS client and emit
  25. ws_client = gui._server._ws.test_client(gui._server.get_flask())
  26. # Get the jsx once so that the page will be evaluated -> variable will be registered
  27. sid = helpers.create_scope_and_get_sid(gui)
  28. flask_client.get(f"/taipy-jsx/test?client_id={sid}")
  29. assert gui._bindings()._get_all_scopes()[sid].var == value_before_update
  30. ws_client.emit("message", {"client_id": sid, "type": "U", "name": "tpec_TpExPr_var_TPMDL_0", "payload": payload})
  31. assert gui._bindings()._get_all_scopes()[sid].var == value_after_update
  32. # assert for received message (message that would be sent to the front-end client)
  33. received_message = ws_client.get_received()
  34. assert len(received_message)
  35. helpers.assert_outward_ws_message(received_message[0], "MU", "tpec_TpExPr_var_TPMDL_0", value_after_update)
  36. def test_ws_u_string(gui: Gui, helpers):
  37. value_before_update = "a random string"
  38. value_after_update = "a random string is added"
  39. payload = {"value": value_after_update}
  40. # set gui frame
  41. gui._set_frame(inspect.currentframe())
  42. ws_u_assert_template(gui, helpers, value_before_update, value_after_update, payload)
  43. def test_ws_u_number(gui: Gui, helpers):
  44. value_before_update = 10
  45. value_after_update = "11"
  46. payload = {"value": value_after_update}
  47. # set gui frame
  48. gui._set_frame(inspect.currentframe())
  49. ws_u_assert_template(gui, helpers, value_before_update, value_after_update, payload)