test_df.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. import logging
  13. import pathlib
  14. from unittest.mock import patch
  15. import pytest
  16. from taipy.gui import Gui, download
  17. def test_download_file(gui: Gui, helpers):
  18. def do_something(state, id):
  19. download(state, (pathlib.Path(__file__).parent.parent.parent / "resources" / "taipan.jpg"))
  20. # Bind a page so that the function will be called
  21. # gui.add_page(
  22. # "test", Markdown("<|Do something!|button|on_action=do_something|id=my_button|>")
  23. # )
  24. # set gui frame
  25. gui._set_frame(inspect.currentframe())
  26. with patch("sys.argv", ["prog"]):
  27. gui.run(run_server=False)
  28. # WS client and emit
  29. ws_client = gui._server._ws.test_client(gui._server.get_flask())
  30. # Get the jsx once so that the page will be evaluated -> variable will be registered
  31. sid = helpers.create_scope_and_get_sid(gui)
  32. ws_client.emit("message", {"client_id": sid, "type": "A", "name": "my_button", "payload": "do_something"})
  33. # assert for received message (message that would be sent to the front-end client)
  34. received_messages = ws_client.get_received()
  35. assert len(received_messages) == 1
  36. assert isinstance(received_messages[0], dict)
  37. assert "name" in received_messages[0] and received_messages[0]["name"] == "message"
  38. assert "args" in received_messages[0]
  39. args = received_messages[0]["args"]
  40. assert "type" in args and args["type"] == "DF"
  41. assert "content" in args and args["content"] == "/taipy-content/taipyStatic0/taipan.jpg"
  42. logging.getLogger().debug(args["content"])