test_state.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 inspect
  12. import pytest
  13. from taipy.gui import Gui
  14. from .state_asset.page1 import get_a, md_page1, set_a
  15. def test_state(gui: Gui):
  16. a = 10 # noqa: F841
  17. gui._set_frame(inspect.currentframe())
  18. gui.add_page("page1", md_page1)
  19. gui.run(run_server=False, single_client=True)
  20. state = gui._Gui__state # type: ignore[attr-defined]
  21. with gui.get_flask_app().app_context():
  22. assert state.a == 10
  23. assert state["page1"].a == 20
  24. assert state["tests.gui.gui_specific.state_asset.page1"].a == 20
  25. assert state._gui == gui
  26. with pytest.raises(Exception) as e:
  27. _ = state.b
  28. assert e.value.args[0] == "Variable 'b' is not defined."
  29. with pytest.raises(Exception) as e:
  30. state.b = 10
  31. assert e.value.args[0] == "Variable 'b' is not accessible."
  32. with pytest.raises(Exception) as e:
  33. _ = state._taipy_p1
  34. assert e.value.args[0] == "Variable '_taipy_p1' is protected and is not accessible."
  35. with pytest.raises(Exception) as e:
  36. state._taipy_p1 = 10
  37. assert e.value.args[0] == "Variable '_taipy_p1' is not accessible."
  38. assert state._get_placeholder("_taipy_p1") is None
  39. state._set_placeholder("_taipy_p1", 10)
  40. assert state._get_placeholder("_taipy_p1") == 10
  41. assert state._get_placeholder_attrs() == (
  42. "_taipy_p1",
  43. "_current_context",
  44. "__state_id"
  45. )
  46. assert get_a(state) == 20
  47. set_a(state, 30)
  48. assert get_a(state) == 30