testdef: add support for blocks (#73332)
gitea-wip/wcs/pipeline/pr-main This commit looks good
Details
gitea-wip/wcs/pipeline/pr-main This commit looks good
Details
This commit is contained in:
parent
e8a00e8835
commit
720df5f6a3
|
@ -4,6 +4,7 @@ import time
|
|||
import pytest
|
||||
|
||||
from wcs import fields
|
||||
from wcs.blocks import BlockDef
|
||||
from wcs.formdef import FormDef
|
||||
from wcs.qommon.http_request import HTTPRequest
|
||||
from wcs.qommon.upload_storage import PicklableUpload
|
||||
|
@ -440,6 +441,54 @@ def test_validation_file_field(pub):
|
|||
assert str(excinfo.value) == 'Invalid value "hop.pdf" for field "File": forbidden file type.'
|
||||
|
||||
|
||||
def test_validation_block_field(pub):
|
||||
block = BlockDef()
|
||||
block.name = 'foobar'
|
||||
block.fields = [
|
||||
fields.StringField(id='1', label='Text', varname='text'),
|
||||
fields.StringField(id='2', label='Hop'),
|
||||
]
|
||||
block.store()
|
||||
|
||||
formdef = FormDef()
|
||||
formdef.name = 'test title'
|
||||
formdef.fields = [
|
||||
fields.PageField(
|
||||
id='0',
|
||||
label='1st page',
|
||||
type='page',
|
||||
post_conditions=[
|
||||
{
|
||||
'condition': {'type': 'django', 'value': 'form_var_blockdata_1_text == "a"'},
|
||||
'error_message': '',
|
||||
}
|
||||
],
|
||||
),
|
||||
fields.BlockField(id='1', label='Block Data', varname='blockdata', type='block:foobar', max_items=3),
|
||||
]
|
||||
formdef.store()
|
||||
|
||||
formdata = formdef.data_class()()
|
||||
formdata.just_created()
|
||||
formdata.receipt_time = datetime.datetime(2021, 1, 1, 0, 0).timetuple()
|
||||
formdata.data['1'] = {'data': [{'1': 'a'}, {'1': 'b'}]}
|
||||
|
||||
testdef = TestDef.create_from_formdata(formdef, formdata)
|
||||
with pytest.raises(TestError) as excinfo:
|
||||
testdef.run(formdef)
|
||||
assert str(excinfo.value) == 'Empty value for field "Hop" (of field "Block Data"): required field.'
|
||||
|
||||
formdata.data['1'] = {'data': [{'1': 'a', '2': 'z'}, {'1': 'b', '2': 'z'}]}
|
||||
testdef = TestDef.create_from_formdata(formdef, formdata)
|
||||
with pytest.raises(TestError) as excinfo:
|
||||
testdef.run(formdef)
|
||||
assert str(excinfo.value) == 'Page 1 post condition was not met (form_var_blockdata_1_text == "a").'
|
||||
|
||||
formdata.data['1'] = {'data': [{'1': 'b', '2': 'z'}, {'1': 'a', '2': 'z'}]}
|
||||
testdef = TestDef.create_from_formdata(formdef, formdata)
|
||||
testdef.run(formdef)
|
||||
|
||||
|
||||
def test_computed_field_support(pub):
|
||||
formdef = FormDef()
|
||||
formdef.name = 'test title'
|
||||
|
|
|
@ -222,16 +222,26 @@ class TestDef(sql.TestDef):
|
|||
value = ','.join(set(value) - set(widget.value))
|
||||
widget.set_error(get_selection_error_text())
|
||||
|
||||
if widget.error:
|
||||
if widget.has_error():
|
||||
field_label = _('"%s"') % field.label
|
||||
if not widget.error:
|
||||
widget = self.get_error_subwidget(widget)
|
||||
value = widget.value
|
||||
field = widget.field
|
||||
field_label = _('"%(subfield)s" (of field %(field)s)') % {
|
||||
'subfield': field.label,
|
||||
'field': field_label,
|
||||
}
|
||||
|
||||
if field.convert_value_to_str:
|
||||
value = field.convert_value_to_str(value)
|
||||
|
||||
error_msg = _('Invalid value "%s"') % value if value else _('Empty value')
|
||||
raise TestError(
|
||||
_('%(error)s for field "%(label)s": %(details)s.')
|
||||
_('%(error)s for field %(label)s: %(details)s.')
|
||||
% {
|
||||
'error': error_msg,
|
||||
'label': field.label,
|
||||
'label': field_label,
|
||||
'details': widget.error,
|
||||
}
|
||||
)
|
||||
|
@ -257,6 +267,16 @@ class TestDef(sql.TestDef):
|
|||
formdata.data[field.id] = value
|
||||
get_publisher().substitutions.invalidate_cache()
|
||||
|
||||
def get_error_subwidget(self, widget):
|
||||
for widget in widget.get_widgets():
|
||||
if widget.error:
|
||||
return widget
|
||||
|
||||
if hasattr(widget, 'get_widgets'):
|
||||
widget = self.get_error_subwidget(widget)
|
||||
if widget:
|
||||
return widget
|
||||
|
||||
def export_to_json(self):
|
||||
return {
|
||||
'name': self.name,
|
||||
|
|
Loading…
Reference in New Issue