test_file_download.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2021-2025 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_with_spaces_path_md(gui: Gui, test_client, helpers):
  44. resources_dir = pathlib.Path(__file__).parent.parent / "resources"
  45. test_file_path = resources_dir / "test file with spaces.txt"
  46. try:
  47. with open(test_file_path, "w") as f:
  48. f.write("Test content")
  49. gui._bind_var_val("content", str(test_file_path.resolve()))
  50. md_string = "<|{content}|file_download|>"
  51. expected_list = [
  52. "<FileDownload",
  53. 'defaultContent="/taipy-content/taipyStatic0/test%20file%20with%20spaces.txt"',
  54. ]
  55. helpers.test_control_md(gui, md_string, expected_list)
  56. finally:
  57. if test_file_path.exists():
  58. test_file_path.unlink()
  59. def test_file_download_any_file_md(gui: Gui, test_client, helpers):
  60. with open(os.path.abspath(__file__), "rb") as content:
  61. gui._bind_var_val("content", content.read())
  62. md_string = "<|{content}|file_download|>"
  63. expected_list = [
  64. "<FileDownload",
  65. 'defaultContent="data:text/x',
  66. "python;base64,",
  67. ]
  68. if not util.find_spec("magic"):
  69. expected_list = ["<FileDownload", 'defaultContent="/taipy-content/taipyStatic0/TaiPyContent.', ".bin"]
  70. helpers.test_control_md(gui, md_string, expected_list)
  71. def test_file_download_url_width_md(gui: Gui, test_client, helpers):
  72. gui._bind_var_val("content", "some_url")
  73. md_string = "<|{content}|file_download|width=70%|>"
  74. expected_list = [
  75. "<FileDownload",
  76. "content={_TpC_tpec_TpExPr_content_TPMDL_0}",
  77. 'defaultContent="some_url"',
  78. 'width="70%"',
  79. ]
  80. helpers.test_control_md(gui, md_string, expected_list)
  81. def test_file_download_url_html(gui: Gui, test_client, helpers):
  82. gui._bind_var_val("content", "some_url")
  83. html_string = '<taipy:file_download content="{content}" />'
  84. expected_list = [
  85. "<FileDownload",
  86. 'defaultContent="some_url"',
  87. ]
  88. helpers.test_control_html(gui, html_string, expected_list)