1
0

test_status.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. from os import path
  13. from unittest.mock import patch
  14. from taipy.gui import Gui
  15. def test_get_status(gui: Gui):
  16. with patch("sys.argv", ["prog"]):
  17. gui.run(run_server=False)
  18. flask_client = gui._server.test_client()
  19. ret = flask_client.get("/taipy.status.json")
  20. assert ret.status_code == 200, f"status_code => {ret.status_code} != 200"
  21. assert ret.mimetype == "application/json", f"mimetype => {ret.mimetype} != application/json"
  22. assert ret.json, "json is not defined"
  23. assert "gui" in ret.json, "json has no key gui"
  24. gui = ret.json.get("gui")
  25. assert isinstance(gui, dict), "json.gui is not a dict"
  26. assert "user_status" in gui, "json.gui has no key user_status"
  27. assert gui.get("user_status") == "", "json.gui.user_status is not empty"
  28. def test_get_extended_status(gui: Gui):
  29. with patch("sys.argv", ["prog"]):
  30. gui.run(run_server=False, extended_status=True)
  31. flask_client = gui._server.test_client()
  32. ret = flask_client.get("/taipy.status.json")
  33. assert ret.status_code == 200, f"status_code => {ret.status_code} != 200"
  34. assert ret.mimetype == "application/json", f"mimetype => {ret.mimetype} != application/json"
  35. assert ret.json, "json is not defined"
  36. gui_json = ret.json.get("gui")
  37. assert "backend_version" in gui_json, "json.gui has no key backend_version"
  38. assert "flask_version" in gui_json, "json.gui has no key flask_version"
  39. if path.exists(gui._get_webapp_path()):
  40. assert "frontend_version" in gui_json, "json.gui has no key frontend_version"
  41. assert "host" in gui_json, "json.gui has no key host"
  42. assert "python_version" in gui_json, "json.gui has no key python_version"
  43. assert "user_status" in gui_json, "json.gui has no key user_status"
  44. assert gui_json.get("user_status") == "", "json.gui.user_status is not empty"
  45. def test_get_status_with_user_status(gui: Gui):
  46. user_status = "user_status"
  47. def on_status(state):
  48. return user_status
  49. gui._set_frame(inspect.currentframe())
  50. with patch("sys.argv", ["prog"]):
  51. gui.run(run_server=False)
  52. flask_client = gui._server.test_client()
  53. ret = flask_client.get("/taipy.status.json")
  54. assert ret.status_code == 200, f"status_code => {ret.status_code} != 200"
  55. assert ret.json, "json is not defined"
  56. gui_ret = ret.json.get("gui")
  57. assert "user_status" in gui_ret, "json.gui has no key user_status"
  58. assert gui_ret.get("user_status") == user_status
  59. assert f'json.gui.user_status => {gui_ret.get("user_status")} != {user_status}'