test_locals_context.py 1.5 KB

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