test_status.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 inspect
  12. from unittest.mock import patch
  13. from flask import g
  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 = ret.json.get("gui")
  37. assert "backend_version" in gui, "json.gui has no key backend_version"
  38. assert "flask_version" in gui, "json.gui has no key flask_version"
  39. assert "frontend_version" in gui, "json.gui has no key frontend_version"
  40. assert "host" in gui, "json.gui has no key host"
  41. assert "python_version" in gui, "json.gui has no key python_version"
  42. assert "user_status" in gui, "json.gui has no key user_status"
  43. assert gui.get("user_status") == "", "json.gui.user_status is not empty"
  44. def test_get_status_with_user_status(gui: Gui):
  45. user_status = "user_status"
  46. def on_status(state):
  47. return user_status
  48. gui._set_frame(inspect.currentframe())
  49. with patch("sys.argv", ["prog"]):
  50. gui.run(run_server=False)
  51. flask_client = gui._server.test_client()
  52. ret = flask_client.get("/taipy.status.json")
  53. assert ret.status_code == 200, f"status_code => {ret.status_code} != 200"
  54. assert ret.json, "json is not defined"
  55. gui = ret.json.get("gui")
  56. assert "user_status" in gui, "json.gui has no key user_status"
  57. assert gui.get("user_status") == user_status, f'json.gui.user_status => {gui.get("user_status")} != {user_status}'