test_notify.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 warnings
  13. import pytest
  14. from taipy.gui import Gui, Markdown, close_notification, notify
  15. from taipy.gui.servers import get_request_meta
  16. from taipy.gui.servers.request import RequestAccessor
  17. @pytest.mark.skip_if_not_server("flask")
  18. def test_notify(gui: Gui, helpers):
  19. # set gui frame
  20. gui._set_frame(inspect.currentframe())
  21. gui.add_page("test", Markdown("<|Hello|button|>"))
  22. gui.run(run_server=False)
  23. server_client = gui._server.test_client()
  24. # WS client and emit
  25. ws_client = gui._server._ws.test_client(gui._server.get_server_instance()) # type: ignore[arg-type]
  26. cid = helpers.create_scope_and_get_sid(gui)
  27. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  28. with gui._server.test_request_context(f"/taipy-jsx/test/?client_id={cid}", data={"client_id": cid}):
  29. get_request_meta().client_id = cid
  30. notify_id = notify(gui._Gui__state, "Info", "Message", id="id") # type: ignore[attr-defined]
  31. assert notify_id == "id"
  32. received_messages = ws_client.get_received()
  33. helpers.assert_outward_ws_simple_message(
  34. received_messages[0], "AL", {"nType": "Info", "message": "Message", "notificationId": "id"}
  35. )
  36. @pytest.mark.skip_if_not_server("fastapi")
  37. @pytest.mark.teste2e
  38. def test_notify_fastapi(gui: Gui, helpers):
  39. # set gui frame
  40. gui._set_frame(inspect.currentframe())
  41. gui.add_page("test", Markdown("<|Hello|button|>"))
  42. gui.run(run_server=False)
  43. helpers.run_e2e_multi_client(gui)
  44. ws_client = helpers.get_socketio_test_client()
  45. cid = helpers.create_scope_and_get_sid(gui)
  46. sid = ws_client.get_sid()
  47. ws_client.get(f"/taipy-jsx/test?client_id={cid}")
  48. with gui.get_app_context():
  49. RequestAccessor.set_sid(sid)
  50. get_request_meta().client_id = cid
  51. notify_id = notify(gui._Gui__state, "Info", "Message", id="id") # type: ignore[attr-defined]
  52. assert notify_id == "id"
  53. received_messages = ws_client.get_received()
  54. try:
  55. helpers.assert_outward_ws_simple_message(
  56. received_messages[0], "AL", {"nType": "Info", "message": "Message", "notificationId": "id"}
  57. )
  58. finally:
  59. ws_client.disconnect()
  60. def test_bad_notify(gui: Gui, helpers):
  61. with warnings.catch_warnings(record=True) as records:
  62. notify_id = notify(None, "Info", "Message", id="id") # type: ignore[arg-type]
  63. assert notify_id is None
  64. assert len(records) == 1
  65. @pytest.mark.skip_if_not_server("flask")
  66. def test_close_notification(gui: Gui, helpers):
  67. # set gui frame
  68. gui._set_frame(inspect.currentframe())
  69. gui.add_page("test", Markdown("<|Hello|button|>"))
  70. gui.run(run_server=False)
  71. server_client = gui._server.test_client()
  72. # WS client and emit
  73. ws_client = gui._server._ws.test_client(gui._server.get_server_instance()) # type: ignore[arg-type]
  74. cid = helpers.create_scope_and_get_sid(gui)
  75. # Get the jsx once so that the page will be evaluated -> variable will be registered
  76. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  77. with gui._server.test_request_context(f"/taipy-jsx/test/?client_id={cid}", data={"client_id": cid}):
  78. get_request_meta().client_id = cid
  79. notify_id = notify(gui._Gui__state, "Info", "Message", id="id") # type: ignore[attr-defined]
  80. close_notification(gui._Gui__state, notify_id) # type: ignore[attr-defined, arg-type]
  81. received_messages = ws_client.get_received()
  82. helpers.assert_outward_ws_simple_message(
  83. received_messages[0], "AL", {"nType": "Info", "message": "Message", "notificationId": "id"}
  84. )
  85. helpers.assert_outward_ws_simple_message(received_messages[1], "AL", {"nType": "", "notificationId": "id"})
  86. @pytest.mark.skip_if_not_server("fastapi")
  87. @pytest.mark.teste2e
  88. def test_close_notification_fastapi(gui: Gui, helpers):
  89. # set gui frame
  90. gui._set_frame(inspect.currentframe())
  91. gui.add_page("test", Markdown("<|Hello|button|>"))
  92. gui.run(run_server=False)
  93. helpers.run_e2e_multi_client(gui)
  94. ws_client = helpers.get_socketio_test_client()
  95. cid = helpers.create_scope_and_get_sid(gui)
  96. sid = ws_client.get_sid()
  97. ws_client.get(f"/taipy-jsx/test?client_id={cid}")
  98. with gui.get_app_context():
  99. RequestAccessor.set_sid(sid)
  100. get_request_meta().client_id = cid
  101. notify_id = notify(gui._Gui__state, "Info", "Message", id="id") # type: ignore[attr-defined]
  102. close_notification(gui._Gui__state, notify_id) # type: ignore[attr-defined, arg-type]
  103. received_messages = ws_client.get_received()
  104. try:
  105. helpers.assert_outward_ws_simple_message(
  106. received_messages[0], "AL", {"nType": "Info", "message": "Message", "notificationId": "id"}
  107. )
  108. helpers.assert_outward_ws_simple_message(received_messages[1], "AL", {"nType": "", "notificationId": "id"})
  109. finally:
  110. ws_client.disconnect()
  111. def test_bad_close_notification(gui: Gui, helpers):
  112. with warnings.catch_warnings(record=True) as records:
  113. close_notification(None, "id") # type: ignore[arg-type]
  114. assert len(records) == 1