test_gui.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 json
  13. from contextlib import nullcontext
  14. import pandas as pd
  15. import pytest
  16. from taipy.gui import Gui
  17. from taipy.gui.utils import _get_module_name_from_frame, _TaipyContent
  18. def test__get_real_var_name(gui: Gui):
  19. res = gui._get_real_var_name("")
  20. frame = inspect.currentframe()
  21. gui._set_frame(frame)
  22. assert isinstance(res, tuple)
  23. assert res[0] == ""
  24. assert res[1] == ""
  25. gui.run(run_server=False)
  26. with gui.get_flask_app().app_context():
  27. with gui._set_locals_context(_get_module_name_from_frame(frame)) if frame else nullcontext():
  28. with pytest.raises(NameError):
  29. res = gui._get_real_var_name(f"{_TaipyContent.get_hash()}_var")
  30. def test__get_user_instance(gui: Gui):
  31. gui.run(run_server=False)
  32. with gui.get_flask_app().app_context():
  33. with pytest.warns(UserWarning):
  34. gui._get_user_instance("", type(None))
  35. def test__refresh_expr(gui: Gui):
  36. gui.run(run_server=False)
  37. with gui.get_flask_app().app_context():
  38. res = gui._refresh_expr("var", None)
  39. assert res is None
  40. def test__tbl_cols(gui: Gui):
  41. data = pd.DataFrame({"col1": [0, 1, 2], "col2": [True, True, False]})
  42. gui.run(run_server=False)
  43. with gui.get_flask_app().app_context():
  44. res = gui._tbl_cols(True, None, json.dumps({}), json.dumps({"data": "data"}), data=data)
  45. assert isinstance(res, str)
  46. d = json.loads(res)
  47. assert isinstance(d, dict)
  48. assert d["col1"]["type"] == "int"
  49. res = gui._tbl_cols(False, None, "", "")
  50. assert repr(res) == "Taipy: Do not update"
  51. def test__chart_conf(gui: Gui):
  52. data = pd.DataFrame({"col1": [0, 1, 2], "col2": [True, True, False]})
  53. gui.run(run_server=False)
  54. with gui.get_flask_app().app_context():
  55. res = gui._chart_conf(True, None, json.dumps({}), json.dumps({"data": "data"}), data=data)
  56. assert isinstance(res, str)
  57. d = json.loads(res)
  58. assert isinstance(d, dict)
  59. assert d["columns"]["col1"]["type"] == "int"
  60. res = gui._chart_conf(False, None, "", "")
  61. assert repr(res) == "Taipy: Do not update"
  62. with pytest.warns(UserWarning):
  63. res = gui._chart_conf(True, None, "", "")
  64. assert repr(res) == "Taipy: Do not update"
  65. def test__get_valid_adapter_result(gui: Gui):
  66. gui.run(run_server=False)
  67. with gui.get_flask_app().app_context():
  68. res = gui._get_valid_adapter_result(("id", "label"))
  69. assert isinstance(res, tuple)
  70. assert res[0] == "id"