forked from streamlit/streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathst_file_uploader_test.py
457 lines (343 loc) · 15.4 KB
/
st_file_uploader_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# Copyright (c) Streamlit Inc. (2018-2022) Snowflake Inc. (2022-2024)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from playwright.sync_api import Page, expect
from e2e_playwright.conftest import ImageCompareFunction, rerun_app, wait_for_app_run
from e2e_playwright.shared.app_utils import check_top_level_class, get_element_by_key
def test_file_uploader_render_correctly(
themed_app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that the file uploader render as expected via screenshot matching."""
file_uploaders = themed_app.get_by_test_id("stFileUploader")
expect(file_uploaders).to_have_count(7)
assert_snapshot(file_uploaders.nth(0), name="st_file_uploader-single_file")
assert_snapshot(file_uploaders.nth(1), name="st_file_uploader-disabled")
assert_snapshot(file_uploaders.nth(2), name="st_file_uploader-multiple_files")
assert_snapshot(file_uploaders.nth(4), name="st_file_uploader-hidden_label")
assert_snapshot(file_uploaders.nth(5), name="st_file_uploader-collapsed_label")
def test_file_uploader_error_message_disallowed_files(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that shows error message for disallowed files."""
file_name1 = "example.json"
file_content1 = b"{}"
uploader_index = 0
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(
files=[
{
"name": file_name1,
"mimeType": "application/json",
"buffer": file_content1,
}
]
)
wait_for_app_run(app)
expect(
app.get_by_test_id("stFileUploaderFileErrorMessage").nth(uploader_index)
).to_have_text("application/json files are not allowed.", use_inner_text=True)
file_uploader_in_error_state = app.get_by_test_id("stFileUploader").nth(
uploader_index
)
assert_snapshot(file_uploader_in_error_state, name="st_file_uploader-error")
def test_uploads_and_deletes_single_file_only(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that uploading a file for single file uploader works as expected."""
file_name1 = "file1.txt"
file_content1 = b"file1content"
file_name2 = "file2.txt"
file_content2 = b"file2content"
uploader_index = 0
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(
files=[{"name": file_name1, "mimeType": "text/plain", "buffer": file_content1}]
)
wait_for_app_run(app)
expect(app.get_by_test_id("stFileUploaderFileName")).to_have_text(
file_name1, use_inner_text=True
)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
str(file_content1), use_inner_text=True
)
file_uploader_uploaded_state = app.get_by_test_id("stFileUploader").nth(
uploader_index
)
assert_snapshot(
file_uploader_uploaded_state, name="st_file_uploader-single_file_uploaded"
)
expect(
app.get_by_test_id("stMarkdownContainer").nth(uploader_index + 1)
).to_have_text("True", use_inner_text=True)
# Upload a second file. This one will replace the first.
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(
files=[{"name": file_name2, "mimeType": "text/plain", "buffer": file_content2}]
)
wait_for_app_run(app)
expect(app.get_by_test_id("stFileUploaderFileName")).to_have_text(
file_name2, use_inner_text=True
)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
str(file_content2), use_inner_text=True
)
expect(
app.get_by_test_id("stMarkdownContainer").nth(uploader_index + 1)
).to_have_text("True", use_inner_text=True)
rerun_app(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
str(file_content2), use_inner_text=True
)
app.get_by_test_id("stFileUploaderDeleteBtn").nth(uploader_index).click()
wait_for_app_run(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"No upload", use_inner_text=True
)
def test_uploads_and_deletes_multiple_files(
app: Page, assert_snapshot: ImageCompareFunction
):
"""Test that uploading multiple files at once works correctly."""
file_name1 = "file1.txt"
file_content1 = b"file1content"
file_name2 = "file2.txt"
file_content2 = b"file2content"
files = [
{"name": file_name1, "mimeType": "text/plain", "buffer": file_content1},
{"name": file_name2, "mimeType": "text/plain", "buffer": file_content2},
]
uploader_index = 2
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(files=files)
wait_for_app_run(app, wait_delay=500)
uploaded_file_names = app.get_by_test_id("stFileUploaderFileName")
# The widget should show the names of the uploaded files in reverse order
file_names = [files[1]["name"], files[0]["name"]]
for i, element in enumerate(uploaded_file_names.all()):
expect(element).to_have_text(file_names[i], use_inner_text=True)
# The script should have printed the contents of the two files into a st.text.
# This tests that the upload actually went through.
content = "\n".join(
[
files[0]["buffer"].decode("utf-8"),
files[1]["buffer"].decode("utf-8"),
]
)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
content, use_inner_text=True
)
file_uploader = app.get_by_test_id("stFileUploader").nth(uploader_index)
assert_snapshot(file_uploader, name="st_file_uploader-multi_file_uploaded")
# Delete the second file. The second file is on top because it was
# most recently uploaded. The first file should still exist.
app.get_by_test_id("stFileUploaderDeleteBtn").first.click()
wait_for_app_run(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
files[0]["buffer"].decode("utf-8"), use_inner_text=True
)
expect(app.get_by_test_id("stMarkdownContainer").nth(5)).to_have_text(
"True", use_inner_text=True
)
def test_uploads_multiple_files_one_by_one_quickly(app: Page):
"""Test that uploads and deletes multiple files quickly works correctly."""
file_name1 = "file1.txt"
file_content1 = b"file1content"
file_name2 = "file2.txt"
file_content2 = b"file2content"
files = [
{"name": file_name1, "mimeType": "text/plain", "buffer": file_content1},
{"name": file_name2, "mimeType": "text/plain", "buffer": file_content2},
]
uploader_index = 2
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(files=files[0])
# The widget should show the name of the uploaded file
expect(app.get_by_test_id("stFileUploaderFileName")).to_have_text(
file_name1, use_inner_text=True
)
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
with app.expect_request("**/upload_file/**"):
file_chooser.set_files(files=files[1])
uploaded_file_names = app.get_by_test_id("stFileUploaderFileName")
# The widget should show the names of the uploaded files in reverse order
file_names = [files[1]["name"], files[0]["name"]]
for i, element in enumerate(uploaded_file_names.all()):
expect(element).to_have_text(file_names[i], use_inner_text=True)
# The script should have printed the contents of the two files into a st.text.
# This tests that the upload actually went through.
content = "\n".join(
[
files[0]["buffer"].decode("utf-8"),
files[1]["buffer"].decode("utf-8"),
]
)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
content, use_inner_text=True
)
# Delete the second file. The second file is on top because it was
# most recently uploaded. The first file should still exist.
app.get_by_test_id("stFileUploaderDeleteBtn").first.click()
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
files[0]["buffer"].decode("utf-8"), use_inner_text=True
)
expect(app.get_by_test_id("stMarkdownContainer").nth(5)).to_have_text(
"True", use_inner_text=True
)
# NOTE: This test is essentially identical to the one above. The only
# difference is that we add a short delay to uploading the two files to
# ensure that two script runs happen separately (sufficiently rapid widget
# changes will often be batched into a single script run) to test for the
# failure mode in https://github.com/streamlit/streamlit/issues/3531.
def test_uploads_multiple_files_one_by_one_slowly(app: Page):
"""Test that uploads and deletes multiple files slowly works."""
file_name1 = "file1.txt"
file_content1 = b"file1content"
file_name2 = "file2.txt"
file_content2 = b"file2content"
files = [
{"name": file_name1, "mimeType": "text/plain", "buffer": file_content1},
{"name": file_name2, "mimeType": "text/plain", "buffer": file_content2},
]
uploader_index = 2
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
# Here we wait for the first file to be uploaded before uploading the second
with app.expect_request("**/upload_file/**"):
file_chooser.set_files(files=files[0])
# The widget should show the name of the uploaded file
expect(app.get_by_test_id("stFileUploaderFileName")).to_have_text(
file_name1, use_inner_text=True
)
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
with app.expect_request("**/upload_file/**"):
file_chooser.set_files(files=files[1])
uploaded_file_names = app.get_by_test_id("stFileUploaderFileName")
# The widget should show the names of the uploaded files in reverse order
file_names = [files[1]["name"], files[0]["name"]]
for i, element in enumerate(uploaded_file_names.all()):
expect(element).to_have_text(file_names[i], use_inner_text=True)
# The script should have printed the contents of the two files into a st.text.
# This tests that the upload actually went through.
content = "\n".join(
[
files[0]["buffer"].decode("utf-8"),
files[1]["buffer"].decode("utf-8"),
]
)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
content, use_inner_text=True
)
# Delete the second file. The second file is on top because it was
# most recently uploaded. The first file should still exist.
app.get_by_test_id("stFileUploaderDeleteBtn").first.click()
wait_for_app_run(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
files[0]["buffer"].decode("utf-8"), use_inner_text=True
)
expect(app.get_by_test_id("stMarkdownContainer").nth(5)).to_have_text(
"True", use_inner_text=True
)
def test_does_not_call_callback_when_not_changed(app: Page):
"""Test that the file uploader does not call a callback when not changed."""
file_name1 = "example5.txt"
file_content1 = b"Hello world!"
uploader_index = 6
# Script contains counter variable stored in session_state with
# default value 0. We increment counter inside file_uploader callback
# Since callback did not called at this moment, counter value should
# be equal 0
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"0", use_inner_text=True
)
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(
files=[
{
"name": file_name1,
"mimeType": "application/json",
"buffer": file_content1,
}
]
)
wait_for_app_run(app)
# Make sure callback called
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"1", use_inner_text=True
)
rerun_app(app)
# Counter should be still equal 1
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"1", use_inner_text=True
)
def test_works_inside_form(app: Page):
"""Test that uploading a file inside form works as expected."""
file_name1 = "form_file1.txt"
file_content1 = b"form_file1content"
uploader_index = 3
with app.expect_file_chooser() as fc_info:
app.get_by_test_id("stFileUploaderDropzone").nth(uploader_index).click()
file_chooser = fc_info.value
file_chooser.set_files(
files=[{"name": file_name1, "mimeType": "text/plain", "buffer": file_content1}]
)
wait_for_app_run(app)
# We should be showing the uploaded file name
expect(app.get_by_test_id("stFileUploaderFileName")).to_have_text(
file_name1, use_inner_text=True
)
# But our uploaded text should contain nothing yet, as we haven't submitted.
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"No upload", use_inner_text=True
)
# Submit the form
app.get_by_test_id("stFormSubmitButton").first.locator("button").click()
wait_for_app_run(app)
# Now we should see the file's contents
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
str(file_content1), use_inner_text=True
)
# Press the delete button. Again, nothing should happen - we
# should still see the file's contents.
app.get_by_test_id("stFileUploaderDeleteBtn").first.click()
wait_for_app_run(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
str(file_content1), use_inner_text=True
)
# Submit again. Now the file should be gone.
app.get_by_test_id("stFormSubmitButton").first.locator("button").click()
wait_for_app_run(app)
expect(app.get_by_test_id("stText").nth(uploader_index)).to_have_text(
"No upload", use_inner_text=True
)
def test_check_top_level_class(app: Page):
"""Check that the top level class is correctly set."""
check_top_level_class(app, "stFileUploader")
def test_custom_css_class_via_key(app: Page):
"""Test that the element can have a custom css class via the key argument."""
expect(get_element_by_key(app, "single")).to_be_visible()