test_gui.py 3.5 KB

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