test_get_module_context.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. from taipy.gui import Gui, Markdown, get_module_context, get_module_name_from_state
  14. from taipy.gui.servers import get_request_meta
  15. def test_get_module_context(gui: Gui, helpers):
  16. # set gui frame
  17. gui._set_frame(inspect.currentframe())
  18. gui.add_page("test", Markdown("<|Hello|button|>"))
  19. gui.run(run_server=False)
  20. server_client = gui._server.test_client()
  21. cid = helpers.create_scope_and_get_sid(gui)
  22. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  23. with gui.get_app_context():
  24. get_request_meta().client_id = cid
  25. module = get_module_context(gui._Gui__state) # type: ignore[attr-defined]
  26. assert module == "test_get_module_context"
  27. def test_bad_get_module_context(gui: Gui, helpers):
  28. with warnings.catch_warnings(record=True) as records:
  29. assert get_module_context(None) is None # type: ignore[arg-type]
  30. assert len(records) == 0
  31. def test_get_module_name_from_state(gui: Gui, helpers):
  32. # set gui frame
  33. gui._set_frame(inspect.currentframe())
  34. gui.add_page("test", Markdown("<|Hello|button|>"))
  35. gui.run(run_server=False)
  36. server_client = gui._server.test_client()
  37. cid = helpers.create_scope_and_get_sid(gui)
  38. server_client.get(f"/taipy-jsx/test?client_id={cid}")
  39. with gui.get_app_context():
  40. get_request_meta().client_id = cid
  41. module = get_module_name_from_state(gui._Gui__state) # type: ignore[attr-defined]
  42. assert module == "test_get_module_context"
  43. def test_bad_get_module_name_from_state(gui: Gui, helpers):
  44. with warnings.catch_warnings(record=True) as records:
  45. assert get_module_name_from_state(None) is None # type: ignore[arg-type]
  46. assert len(records) == 0