1
0

test_navigate.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, State, navigate
  15. @pytest.mark.skip_if_not_server("flask")
  16. def test_navigate(gui: Gui, helpers):
  17. def navigate_to(state: State):
  18. navigate(state, "test")
  19. with warnings.catch_warnings(record=True):
  20. gui._set_frame(inspect.currentframe())
  21. gui.add_page("test", Markdown("#This is a page"))
  22. gui.run(run_server=False)
  23. client = gui._server.test_client()
  24. # WS client and emit
  25. ws_client = gui._server._ws.test_client(gui._server.get_server_instance())
  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. client.get(f"/taipy-jsx/test/?client_id={sid}")
  29. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  30. # assert for received message (message that would be sent to the front-end client)
  31. assert ws_client.get_received()
  32. @pytest.mark.skip_if_not_server("fastapi")
  33. @pytest.mark.teste2e
  34. def test_navigate_fastapi(gui: Gui, helpers):
  35. def navigate_to(state: State):
  36. navigate(state, "test")
  37. with warnings.catch_warnings(record=True):
  38. gui._set_frame(inspect.currentframe())
  39. gui.add_page("test", Markdown("#This is a page"))
  40. helpers.run_e2e_multi_client(gui)
  41. ws_client = helpers.get_socketio_test_client()
  42. cid = helpers.create_scope_and_get_sid(gui)
  43. sid = ws_client.get_sid()
  44. ws_client.get(f"/taipy-jsx/test?client_id={cid}")
  45. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  46. # assert for received message (message that would be sent to the front-end client)
  47. assert ws_client.get_received()
  48. @pytest.mark.skip_if_not_server("flask")
  49. def test_navigate_to_no_route(gui: Gui, helpers):
  50. def navigate_to(state: State):
  51. navigate(state, "toto")
  52. with warnings.catch_warnings(record=True):
  53. gui._set_frame(inspect.currentframe())
  54. gui.add_page("test", Markdown("#This is a page"))
  55. gui.run(run_server=False)
  56. client = gui._server.test_client()
  57. # WS client and emit
  58. ws_client = gui._server._ws.test_client(gui._server.get_server_instance())
  59. # Get the jsx once so that the page will be evaluated -> variable will be registered
  60. sid = helpers.create_scope_and_get_sid(gui)
  61. client.get(f"/taipy-jsx/test/?client_id={sid}")
  62. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  63. # assert for received message (message that would be sent to the front-end client)
  64. assert not ws_client.get_received()
  65. @pytest.mark.skip_if_not_server("fastapi")
  66. @pytest.mark.teste2e
  67. def test_navigate_to_no_route_fastapi(gui: Gui, helpers):
  68. def navigate_to(state: State):
  69. navigate(state, "toto")
  70. with warnings.catch_warnings(record=True):
  71. gui._set_frame(inspect.currentframe())
  72. gui.add_page("test", Markdown("#This is a page"))
  73. helpers.run_e2e_multi_client(gui)
  74. ws_client = helpers.get_socketio_test_client()
  75. cid = helpers.create_scope_and_get_sid(gui)
  76. sid = ws_client.get_sid()
  77. ws_client.get(f"/taipy-jsx/test?client_id={cid}")
  78. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "navigate_to"})
  79. # assert for received message (message that would be sent to the front-end client)
  80. assert not ws_client.get_received()
  81. def test_on_navigate_to_inexistant(gui: Gui, helpers):
  82. def on_navigate(state: State, page: str):
  83. return "test2" if page == "test" else page
  84. with warnings.catch_warnings(record=True) as records:
  85. gui._set_frame(inspect.currentframe())
  86. gui.add_page("test", Markdown("#This is a page"))
  87. gui.run(run_server=False)
  88. client = gui._server.test_client()
  89. # Get the jsx once so that the page will be evaluated -> variable will be registered
  90. sid = helpers.create_scope_and_get_sid(gui)
  91. client.get(f"/taipy-jsx/test?client_id={sid}")
  92. warns = helpers.get_taipy_warnings(records)
  93. assert len(warns) == 1
  94. text = warns[0].message.args[0] if isinstance(warns[0].message, Warning) else warns[0].message
  95. assert text == 'Cannot navigate to "test2": unknown page.'
  96. def test_on_navigate_to_existant(gui: Gui, helpers):
  97. def on_navigate(state: State, page: str):
  98. return "test2" if page == "test1" else page
  99. with warnings.catch_warnings(record=True):
  100. gui._set_frame(inspect.currentframe())
  101. gui.add_page("test1", Markdown("#This is a page test1"))
  102. gui.add_page("test2", Markdown("#This is a page test2"))
  103. gui.run(run_server=False)
  104. client = gui._server.test_client()
  105. # Get the jsx once so that the page will be evaluated -> variable will be registered
  106. sid = helpers.create_scope_and_get_sid(gui)
  107. content = client.get(f"/taipy-jsx/test1?client_id={sid}")
  108. assert content.status_code == 302