test_upload.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from reflex.components.core.upload import (
  2. Upload,
  3. _on_drop_spec, # type: ignore
  4. cancel_upload,
  5. get_upload_url,
  6. )
  7. from reflex.event import EventSpec
  8. from reflex.state import State
  9. from reflex.vars import Var
  10. class TestUploadState(State):
  11. """Test upload state."""
  12. def drop_handler(self, files):
  13. """Handle the drop event.
  14. Args:
  15. files: The files dropped.
  16. """
  17. pass
  18. def not_drop_handler(self, not_files):
  19. """Handle the drop event without defining the files argument.
  20. Args:
  21. not_files: The files dropped.
  22. """
  23. pass
  24. def test_cancel_upload():
  25. spec = cancel_upload("foo_id")
  26. assert isinstance(spec, EventSpec)
  27. def test_get_upload_url():
  28. url = get_upload_url("foo_file")
  29. assert isinstance(url, Var)
  30. def test__on_drop_spec():
  31. assert isinstance(_on_drop_spec(Var.create([])), list)
  32. def test_upload_create():
  33. up_comp_1 = Upload.create()
  34. assert isinstance(up_comp_1, Upload)
  35. assert up_comp_1.is_used
  36. # reset is_used
  37. Upload.is_used = False
  38. up_comp_2 = Upload.create(
  39. id="foo_id",
  40. on_drop=TestUploadState.drop_handler([]), # type: ignore
  41. )
  42. assert isinstance(up_comp_2, Upload)
  43. assert up_comp_2.is_used
  44. # reset is_used
  45. Upload.is_used = False
  46. up_comp_3 = Upload.create(
  47. id="foo_id",
  48. on_drop=TestUploadState.drop_handler,
  49. )
  50. assert isinstance(up_comp_3, Upload)
  51. assert up_comp_3.is_used
  52. # reset is_used
  53. Upload.is_used = False
  54. up_comp_4 = Upload.create(
  55. id="foo_id",
  56. on_drop=TestUploadState.not_drop_handler([]), # type: ignore
  57. )
  58. assert isinstance(up_comp_4, Upload)
  59. assert up_comp_4.is_used