test_broadcast.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # Copyright 2021-2024 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 contextlib
  12. import inspect
  13. import pytest
  14. from taipy.gui import Gui
  15. @contextlib.contextmanager
  16. def get_state(gui: Gui, state_id: str):
  17. with gui.get_flask_app().app_context():
  18. client_id = gui._bindings()._get_or_create_scope(state_id)[0]
  19. gui._Gui__set_client_id_in_context(client_id) # type: ignore[attr-defined]
  20. yield gui._Gui__state # type: ignore[attr-defined]
  21. def test_multiple_scopes(gui: Gui):
  22. var = 1 # noqa: F841
  23. gui._set_frame(inspect.currentframe())
  24. gui.add_page("page1", "<|{var}|>")
  25. gui.run(run_server=False)
  26. with get_state(gui, "s1") as state1:
  27. assert state1.var == 1
  28. with get_state(gui, "s2") as state2:
  29. assert state2.var == 1
  30. with get_state(gui, "s1") as state1:
  31. state1.var = 2
  32. assert state1.var == 2
  33. with get_state(gui, "s2") as state2:
  34. assert state2.var == 1
  35. def test_shared_variable(gui: Gui):
  36. var = 1 # noqa: F841
  37. s_var = 10 # noqa: F841
  38. gui._set_frame(inspect.currentframe())
  39. gui.add_page("test", "<|{var}|>")
  40. gui.add_shared_variable("s_var")
  41. gui.run(run_server=False)
  42. with get_state(gui, "s1") as state1:
  43. assert state1.var == 1
  44. assert state1.s_var == 10
  45. state1.var = 2
  46. state1.s_var = 20
  47. assert state1.var == 2
  48. assert state1.s_var == 20
  49. with get_state(gui, "s2") as state2:
  50. assert state2.var == 1
  51. assert state1.s_var == 20
  52. Gui._clear_shared_variable()
  53. def test_broadcast_change(gui: Gui):
  54. # Bind test variables
  55. v1 = "none" # noqa: F841
  56. v2 = 1 # noqa: F841
  57. gui._set_frame(inspect.currentframe())
  58. gui.add_page("test", " <|{v1}|><|{v2}|>")
  59. gui.run(run_server=False)
  60. s1, _ = gui._bindings()._get_or_create_scope("s1")
  61. s2, _ = gui._bindings()._get_or_create_scope("s2")
  62. gui.broadcast_change("v2", 2)
  63. with get_state(gui, s1) as state1:
  64. assert state1.v1 == "none"
  65. assert state1.v2 == 2
  66. with get_state(gui, s2) as state2:
  67. assert state2.v1 == "none"
  68. assert state2.v2 == 2
  69. def test_broadcast_changes(gui: Gui):
  70. # Bind test variables
  71. v1 = "none" # noqa: F841
  72. v2 = 1 # noqa: F841
  73. gui._set_frame(inspect.currentframe())
  74. gui.add_page("test", " <|{v1}|><|{v2}|>")
  75. gui.run(run_server=False)
  76. s1, _ = gui._bindings()._get_or_create_scope("s1")
  77. s2, _ = gui._bindings()._get_or_create_scope("s2")
  78. changes = { "v1": "some", "v2": 2}
  79. gui.broadcast_changes(changes)
  80. with get_state(gui, s1) as state1:
  81. assert state1.v1 == "some"
  82. assert state1.v2 == 2
  83. with get_state(gui, s2) as state2:
  84. assert state2.v1 == "some"
  85. assert state2.v2 == 2
  86. gui.broadcast_changes(v1="more", v2=3)
  87. with get_state(gui, s1) as state1:
  88. assert state1.v1 == "more"
  89. assert state1.v2 == 3
  90. with get_state(gui, s2) as state2:
  91. assert state2.v1 == "more"
  92. assert state2.v2 == 3
  93. gui.broadcast_changes({ "v1": "more yet"}, v2=4)
  94. with get_state(gui, s1) as state1:
  95. assert state1.v1 == "more yet"
  96. assert state1.v2 == 4
  97. with get_state(gui, s2) as state2:
  98. assert state2.v1 == "more yet"
  99. assert state2.v2 == 4
  100. def test_broadcast_callback(gui: Gui):
  101. gui.run(run_server=False)
  102. res = gui.broadcast_callback(lambda _, t: t, ["Hello World"], "mine")
  103. assert isinstance(res, dict)
  104. assert not res
  105. gui._bindings()._get_or_create_scope("test scope")
  106. res = gui.broadcast_callback(lambda _, t: t, ["Hello World"], "mine")
  107. assert len(res) == 1
  108. assert res.get("test scope", None) == "Hello World"
  109. with pytest.warns(UserWarning):
  110. res = gui.broadcast_callback(print, ["Hello World"], "mine")
  111. assert isinstance(res, dict)
  112. assert res.get("test scope", "Hello World") is None
  113. gui._bindings()._get_or_create_scope("another scope")
  114. res = gui.broadcast_callback(lambda s, t: t, ["Hello World"], "mine")
  115. assert len(res) == 2
  116. assert res.get("test scope", None) == "Hello World"
  117. assert res.get("another scope", None) == "Hello World"