test_user_content.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. import pytest
  14. from taipy.gui import Gui
  15. def test_user_content_without_callback(gui: Gui, helpers):
  16. with patch("sys.argv", ["prog"]):
  17. gui.run(run_server=False, single_client=True)
  18. flask_client = gui._server.test_client()
  19. with pytest.warns(UserWarning):
  20. ret = flask_client.get(gui._get_user_content_url("path"))
  21. assert ret.status_code == 404
  22. def test_user_content_with_wrong_callback(gui: Gui, helpers):
  23. def on_user_content_cb(state, path, args):
  24. return None
  25. on_user_content = on_user_content_cb
  26. gui._set_frame(inspect.currentframe())
  27. with patch("sys.argv", ["prog"]):
  28. gui.run(run_server=False, single_client=True)
  29. flask_client = gui._server.test_client()
  30. with pytest.warns(UserWarning):
  31. ret = flask_client.get(gui._get_user_content_url("path", {"a": "b"}))
  32. assert ret.status_code == 404
  33. def test_user_content_with_callback(gui: Gui, helpers):
  34. def on_user_content_cb(state, path, args):
  35. return ""
  36. on_user_content = on_user_content_cb
  37. gui._set_frame(inspect.currentframe())
  38. with patch("sys.argv", ["prog"]):
  39. gui.run(run_server=False, single_client=True)
  40. flask_client = gui._server.test_client()
  41. ret = flask_client.get(gui._get_user_content_url("path"))
  42. assert ret.status_code == 200