test_gui.py 3.3 KB

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