1
0

test_locals_context.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. from unittest.mock import patch
  12. import pytest
  13. from taipy.gui import Gui
  14. from taipy.gui.utils._locals_context import _LocalsContext
  15. def test_locals_context(gui: Gui):
  16. lc = _LocalsContext()
  17. with patch("sys.argv", ["prog"]):
  18. gui.run(run_server=False)
  19. with gui.get_flask_app().app_context():
  20. with pytest.raises(KeyError):
  21. lc.get_default()
  22. current_locals = locals()
  23. lc.set_default(current_locals)
  24. assert lc.get_default() == current_locals
  25. temp_locals = {"__main__": "test"}
  26. lc.add("test", temp_locals)
  27. assert lc.get_context() is None
  28. assert lc.get_locals() == current_locals
  29. with lc.set_locals_context("test"):
  30. assert lc.get_context() == "test"
  31. assert lc.get_locals() == temp_locals
  32. assert lc.get_context() is None
  33. assert lc.get_locals() == current_locals
  34. assert lc.is_default() is True
  35. assert "__main__" in lc.get_all_keys()