1
0

test_upload.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. from pathlib import Path
  2. from typing import List
  3. from nicegui import events, ui
  4. from nicegui.testing import Screen
  5. test_path1 = Path('tests/test_upload.py').resolve()
  6. test_path2 = Path('tests/test_scene.py').resolve()
  7. def test_uploading_text_file(screen: Screen):
  8. results: List[events.UploadEventArguments] = []
  9. ui.upload(on_upload=results.append, label='Test Title')
  10. screen.open('/')
  11. screen.should_contain('Test Title')
  12. screen.find_by_class('q-uploader__input').send_keys(str(test_path1))
  13. screen.wait(0.1)
  14. screen.click('cloud_upload')
  15. screen.wait(0.1)
  16. assert len(results) == 1
  17. assert results[0].name == test_path1.name
  18. assert results[0].type in {'text/x-python', 'text/x-python-script'}
  19. assert results[0].content.read() == test_path1.read_bytes()
  20. def test_two_upload_elements(screen: Screen):
  21. results: List[events.UploadEventArguments] = []
  22. ui.upload(on_upload=results.append, auto_upload=True, label='Test Title 1')
  23. ui.upload(on_upload=results.append, auto_upload=True, label='Test Title 2')
  24. screen.open('/')
  25. screen.should_contain('Test Title 1')
  26. screen.should_contain('Test Title 2')
  27. screen.find_all_by_class('q-uploader__input')[0].send_keys(str(test_path1))
  28. screen.find_all_by_class('q-uploader__input')[1].send_keys(str(test_path2))
  29. screen.wait(0.1)
  30. assert len(results) == 2
  31. assert results[0].name == test_path1.name
  32. assert results[1].name == test_path2.name
  33. def test_uploading_from_two_tabs(screen: Screen):
  34. @ui.page('/')
  35. def page():
  36. ui.upload(on_upload=lambda e: ui.label(f'uploaded {e.name}'), auto_upload=True)
  37. screen.open('/')
  38. screen.switch_to(1)
  39. screen.open('/')
  40. screen.should_not_contain(test_path1.name)
  41. screen.find_by_class('q-uploader__input').send_keys(str(test_path1))
  42. screen.should_contain(f'uploaded {test_path1.name}')
  43. screen.switch_to(0)
  44. screen.should_not_contain(f'uploaded {test_path1.name}')
  45. def test_upload_with_header_slot(screen: Screen):
  46. with ui.upload().add_slot('header'):
  47. ui.label('Header')
  48. screen.open('/')
  49. screen.should_contain('Header')
  50. def test_replace_upload(screen: Screen):
  51. with ui.row() as container:
  52. ui.upload(label='A')
  53. def replace():
  54. container.clear()
  55. with container:
  56. ui.upload(label='B')
  57. ui.button('Replace', on_click=replace)
  58. screen.open('/')
  59. screen.should_contain('A')
  60. screen.click('Replace')
  61. screen.wait(0.5)
  62. screen.should_contain('B')
  63. screen.should_not_contain('A')
  64. def test_reset_upload(screen: Screen):
  65. upload = ui.upload()
  66. ui.button('Reset', on_click=upload.reset)
  67. screen.open('/')
  68. screen.find_by_class('q-uploader__input').send_keys(str(test_path1))
  69. screen.should_contain(test_path1.name)
  70. screen.click('Reset')
  71. screen.wait(0.5)
  72. screen.should_not_contain(test_path1.name)
  73. def test_multi_upload_event(screen: Screen):
  74. results: List[events.MultiUploadEventArguments] = []
  75. ui.upload(on_multi_upload=results.append, multiple=True)
  76. screen.open('/')
  77. screen.find_by_class('q-uploader__input').send_keys(f'{test_path1}\n{test_path2}')
  78. screen.wait(0.1)
  79. screen.click('cloud_upload')
  80. screen.wait(0.1)
  81. assert len(results) == 1
  82. assert results[0].names == [test_path1.name, test_path2.name]
  83. assert results[0].contents[0].read() == test_path1.read_bytes()
  84. assert results[0].contents[1].read() == test_path2.read_bytes()