testdef: handle empty file fields (#75616) #178

Merged
fpeters merged 1 commits from wip/75616-test-empty-filefield into main 2023-03-27 16:31:19 +02:00
2 changed files with 9 additions and 1 deletions

View File

@ -632,6 +632,14 @@ def test_validation_file_field(pub):
testdef = TestDef.create_from_formdata(formdef, formdata)
testdef.run(formdef)
# test against empty value
formdata.data['1'] = None
testdef = TestDef.create_from_formdata(formdef, formdata)
with pytest.raises(TestError) as excinfo:
testdef.run(formdef)
assert str(excinfo.value) == 'Empty value for field "File": required field.'
# test with filename that will negate next field condition
upload = PicklableUpload('hop.pdf', 'application/pdf', 'ascii')
upload.receive([b'first line', b'second line'])
formdata.data['1'] = upload

View File

@ -79,7 +79,7 @@ class TestDef(sql.TestDef):
if field.id in formdata.data:
value = formdata.data[field.id]
if hasattr(field, 'get_json_value'):
if value and hasattr(field, 'get_json_value'):
value = field.get_json_value(value)
field_data[field.id] = value