test_state.py 2.1 KB

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