test_file_download.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 os
  12. import pathlib
  13. from importlib import util
  14. from taipy.gui import Gui
  15. def test_file_download_url_md(gui: Gui, test_client, helpers):
  16. gui._bind_var_val("content", "some_url")
  17. md_string = "<|{content}|file_download|>"
  18. expected_list = [
  19. "<FileDownload",
  20. "content={_TpC_tpec_TpExPr_content_TPMDL_0}",
  21. 'defaultContent="some_url"',
  22. ]
  23. helpers.test_control_md(gui, md_string, expected_list)
  24. def test_file_download_file_md(gui: Gui, test_client, helpers):
  25. with open((pathlib.Path(__file__).parent.parent / "resources" / "fred.png").resolve(), "rb") as content:
  26. gui._bind_var_val("content", content.read())
  27. md_string = "<|{content}|file_download|>"
  28. expected_list = [
  29. "<FileDownload",
  30. 'defaultContent="data:image/png;base64,',
  31. ]
  32. if not util.find_spec("magic"):
  33. expected_list = ["<FileDownload", 'defaultContent="/taipy-content/taipyStatic0/TaiPyContent.', ".bin"]
  34. helpers.test_control_md(gui, md_string, expected_list)
  35. def test_file_download_path_md(gui: Gui, test_client, helpers):
  36. gui._bind_var_val("content", str((pathlib.Path(__file__).parent.parent / "resources" / "fred.png").resolve()))
  37. md_string = "<|{content}|file_download|>"
  38. expected_list = [
  39. "<FileDownload",
  40. 'defaultContent="/taipy-content/taipyStatic0/fred.png',
  41. ]
  42. helpers.test_control_md(gui, md_string, expected_list)
  43. def test_file_download_any_file_md(gui: Gui, test_client, helpers):
  44. with open(os.path.abspath(__file__), "rb") as content:
  45. gui._bind_var_val("content", content.read())
  46. md_string = "<|{content}|file_download|>"
  47. expected_list = [
  48. "<FileDownload",
  49. 'defaultContent="data:text/x',
  50. "python;base64,",
  51. ]
  52. if not util.find_spec("magic"):
  53. expected_list = ["<FileDownload", 'defaultContent="/taipy-content/taipyStatic0/TaiPyContent.', ".bin"]
  54. helpers.test_control_md(gui, md_string, expected_list)
  55. def test_file_download_url_html(gui: Gui, test_client, helpers):
  56. gui._bind_var_val("content", "some_url")
  57. html_string = '<taipy:file_download content="{content}" />'
  58. expected_list = [
  59. "<FileDownload",
  60. 'defaultContent="some_url"',
  61. ]
  62. helpers.test_control_html(gui, html_string, expected_list)