test_file_download.py 2.8 KB

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