test_ru.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 inspect
  12. import pytest
  13. from taipy.gui import Gui, Markdown
  14. from taipy.gui.servers.request import RequestAccessor
  15. @pytest.mark.skip_if_not_server("flask")
  16. def test_ru_selector(gui: Gui, helpers, csvdata):
  17. # Bind test variables
  18. selected_val = ["value1", "value2"] # noqa: F841
  19. # set gui frame
  20. gui._set_frame(inspect.currentframe())
  21. # Bind a page so that the variable will be evaluated as expression
  22. gui.add_page(
  23. "test",
  24. Markdown("<|{selected_val}|selector|multiple|>"),
  25. )
  26. gui.run(run_server=False)
  27. server_client = gui._server.test_client()
  28. # WS client and emit
  29. ws_client = gui._server._ws.test_client(gui._server.get_server_instance())
  30. sid = helpers.create_scope_and_get_sid(gui)
  31. # Get the jsx once so that the page will be evaluated -> variable will be registered
  32. server_client.get(f"/taipy-jsx/test?client_id={sid}")
  33. ws_client.emit(
  34. "message",
  35. {"client_id": sid, "type": "RU", "name": "", "payload": {"names": ["_TpLv_tpec_TpExPr_selected_val_TPMDL_0"]}},
  36. )
  37. # assert for received message (message that would be sent to the front-end client)
  38. received_messages = ws_client.get_received()
  39. assert len(received_messages)
  40. helpers.assert_outward_ws_message(
  41. received_messages[0], "MU", "_TpLv_tpec_TpExPr_selected_val_TPMDL_0", ["value1", "value2"]
  42. )
  43. @pytest.mark.skip_if_not_server("fastapi")
  44. @pytest.mark.teste2e
  45. def test_ru_selector_fastapi(gui: Gui, helpers, csvdata):
  46. # Bind test variables
  47. selected_val = ["value1", "value2"] # noqa: F841
  48. # set gui frame
  49. gui._set_frame(inspect.currentframe())
  50. # Bind a page so that the variable will be evaluated as expression
  51. gui.add_page(
  52. "test",
  53. Markdown("<|{selected_val}|selector|multiple|>"),
  54. )
  55. helpers.run_e2e_multi_client(gui)
  56. ws_client = helpers.get_socketio_test_client()
  57. sid = helpers.create_scope_and_get_sid(gui)
  58. RequestAccessor.set_sid(ws_client.get_sid())
  59. ws_client.get(f"/taipy-jsx/test?client_id={sid}")
  60. ws_client.emit(
  61. "message",
  62. {"client_id": sid, "type": "RU", "name": "", "payload": {"names": ["_TpLv_tpec_TpExPr_selected_val_TPMDL_0"]}},
  63. )
  64. # assert for received message (message that would be sent to the front-end client)
  65. received_messages = ws_client.get_received()
  66. try:
  67. assert len(received_messages)
  68. helpers.assert_outward_ws_message(
  69. received_messages[0], "MU", "_TpLv_tpec_TpExPr_selected_val_TPMDL_0", ["value1", "value2"]
  70. )
  71. finally:
  72. ws_client.disconnect()