test_with.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.data.data_scope import _DataScopes
  15. from taipy.gui.servers.request import RequestAccessor
  16. @pytest.mark.skip_if_not_server("flask")
  17. def test_sending_messages_in_group(gui: Gui, helpers):
  18. name = "World!" # noqa: F841
  19. btn_id = "button1" # noqa: F841
  20. # set gui frame
  21. gui._set_frame(inspect.currentframe())
  22. gui.add_page("test", Markdown("<|Hello {name}|button|id={btn_id}|>"))
  23. gui.run(run_server=False, single_client=True)
  24. server_client = gui._server.test_client()
  25. # WS client and emit
  26. ws_client = gui._server._ws.test_client(gui._server.get_server_instance())
  27. cid = _DataScopes._GLOBAL_ID
  28. # Get the jsx once so that the page will be evaluated -> variable will be registered
  29. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  30. assert gui._bindings()._get_all_scopes()[cid].name == "World!" # type: ignore
  31. assert gui._bindings()._get_all_scopes()[cid].btn_id == "button1" # type: ignore
  32. with gui._server.test_request_context(f"/taipy-jsx/test/?client_id={cid}", data={"client_id": cid}):
  33. with gui as aGui:
  34. aGui._Gui__state.name = "Monde!"
  35. aGui._Gui__state.btn_id = "button2"
  36. assert gui._bindings()._get_all_scopes()[cid].btn_id == "button2" # type: ignore
  37. assert gui._bindings()._get_all_scopes()[cid].name == "Monde!"
  38. received_messages = ws_client.get_received()
  39. helpers.assert_outward_ws_multiple_message(received_messages[0], "MS", 2)
  40. @pytest.mark.skip_if_not_server("fastapi")
  41. @pytest.mark.teste2e
  42. def test_sending_messages_in_group_fastapi(gui: Gui, helpers):
  43. name = "World!" # noqa: F841
  44. btn_id = "button1" # noqa: F841
  45. # set gui frame
  46. gui._set_frame(inspect.currentframe())
  47. gui.add_page("test", Markdown("<|Hello {name}|button|id={btn_id}|>"))
  48. helpers.run_e2e(gui)
  49. ws_client = helpers.get_socketio_test_client()
  50. RequestAccessor.set_sid(ws_client.get_sid())
  51. cid = _DataScopes._GLOBAL_ID
  52. ws_client.get(f"/taipy-jsx/test?client_id={cid}")
  53. try:
  54. assert gui._bindings()._get_all_scopes()[cid].name == "World!" # type: ignore
  55. assert gui._bindings()._get_all_scopes()[cid].btn_id == "button1" # type: ignore
  56. with gui._server.test_request_context(f"/taipy-jsx/test/?client_id={cid}", data={"client_id": cid}):
  57. with gui as aGui:
  58. aGui._Gui__state.name = "Monde!"
  59. aGui._Gui__state.btn_id = "button2"
  60. assert gui._bindings()._get_all_scopes()[cid].btn_id == "button2" # type: ignore
  61. assert gui._bindings()._get_all_scopes()[cid].name == "Monde!"
  62. received_messages = ws_client.get_received()
  63. helpers.assert_outward_ws_multiple_message(received_messages[0], "MS", 2)
  64. finally:
  65. ws_client.disconnect()