ObjectWidget single widgets integration tests¶
Checking components on the highest possible level.
>>> from datetime import date >>> from z3c.form import form >>> from z3c.form import field >>> from z3c.form import testing>>> from z3c.form.object import registerFactoryAdapter >>> registerFactoryAdapter(testing.IObjectWidgetSingleSubIntegration, ... testing.ObjectWidgetSingleSubIntegration)>>> request = testing.TestRequest()>> from z3c.form.object import registerFactoryAdapter >> registerFactoryAdapter(testing.IObjectWidgetSingleSubIntegration, .. testing.ObjectWidgetSingleSubIntegration)
>>> class EForm(form.EditForm): ... form.extends(form.EditForm) ... fields = field.Fields(testing.IObjectWidgetSingleIntegration)
Our single content object:
>>> obj = testing.ObjectWidgetSingleIntegration()
We recreate the form each time, to stay as close as possible. In real life the form gets instantiated and destroyed with each request.
>>> def getForm(request, fname=None):
... frm = EForm(obj, request)
... testing.addTemplate(frm, 'integration_edit.pt')
... frm.update()
... content = frm.render()
... if fname is not None:
... testing.saveHtml(content, fname)
... return content
Empty¶
All blank and empty values:
>>> content = getForm(request, 'ObjectWidget_single_edit_empty.html')
>>> print(testing.plainText(content))
Object label
Int label *
[]
Bool label *
( ) yes ( ) no
Choice label *
[[ ]]
ChoiceOpt label
[No value]
TextLine label *
[]
Date label *
[]
ReadOnly label *
[]
[Apply]
Some valid default values¶
>>> obj.subobj = testing.ObjectWidgetSingleSubIntegration(
... singleInt=-100,
... singleBool=False,
... singleChoice='two',
... singleChoiceOpt='six',
... singleTextLine='some text one',
... singleDate=date(2014, 6, 20),
... singleReadOnly='some R/O text')
>>> content = getForm(request, 'ObjectWidget_single_edit_simple.html')
>>> print(testing.plainText(content))
Object label
Int label *
[-100]
Bool label *
( ) yes (O) no
Choice label *
[two]
ChoiceOpt label
[six]
TextLine label *
[some text one]
Date label *
[14/06/20]
ReadOnly label *
some R/O text
[Apply]
Wrong values¶
Set wrong values:
>>> submit = testing.getSubmitValues(content)
>>> submit['form.widgets.subobj.widgets.singleInt'] = 'foobar'
>>> submit['form.widgets.subobj.widgets.singleTextLine'] = 'foo\nbar'
>>> submit['form.widgets.subobj.widgets.singleDate'] = 'foobar'
>>> submit['form.buttons.apply'] = 'Apply'
>>> request = testing.TestRequest(form=submit)
We should get lots of errors:
>>> content = getForm(request, 'ObjectWidget_single_edit_submit_wrong.html')
>>> print(testing.plainText(content,
... './/ul[@id="form-errors"]'))
* Object label: The entered value is not a valid integer literal.
Constraint not satisfied
The datetime string did not match the pattern 'yy/MM/dd'.
>>> print(testing.plainText(content,
... './/div[@id="row-form-widgets-subobj"]/b/div[@class="error"]'))
The entered value is not a valid integer literal.
Constraint not satisfied
The datetime string did not match the pattern 'yy/MM/dd'.
>>> print(testing.plainText(content,
... './/div[@id="row-form-widgets-subobj"]'))
The entered value is not a valid integer literal.
Constraint not satisfied
The datetime string did not match the pattern 'yy/MM/dd'. Object label
Int label *
The entered value is not a valid integer literal.
[foobar]
Bool label *
( ) yes (O) no
Choice label *
[two]
ChoiceOpt label
[six]
TextLine label *
Constraint not satisfied
[foo
bar]
Date label *
The datetime string did not match the pattern 'yy/MM/dd'.
[foobar]
ReadOnly label *
some R/O text
Let’s fix the values:
>>> submit = testing.getSubmitValues(content)
>>> submit['form.widgets.subobj.widgets.singleInt'] = '1042'
>>> submit['form.widgets.subobj.widgets.singleBool'] = 'true'
>>> submit['form.widgets.subobj.widgets.singleChoice:list'] = 'three'
>>> submit['form.widgets.subobj.widgets.singleChoiceOpt:list'] = 'four'
>>> submit['form.widgets.subobj.widgets.singleTextLine'] = 'foobar'
>>> submit['form.widgets.subobj.widgets.singleDate'] = '14/06/21'
>>> submit['form.buttons.apply'] = 'Apply'
>>> request = testing.TestRequest(form=submit)
>>> content = getForm(request, 'ObjectWidget_single_edit_submit_fixed.html')
>>> print(testing.plainText(content))
Data successfully updated.
Object label
Int label *
[1,042]
Bool label *
(O) yes ( ) no
Choice label *
[three]
ChoiceOpt label
[four]
TextLine label *
[foobar]
Date label *
[14/06/21]
ReadOnly label *
some R/O text
[Apply]
Bool was misbehaving
>>> submit = testing.getSubmitValues(content)
>>> submit['form.widgets.subobj.widgets.singleBool'] = 'false'
>>> submit['form.buttons.apply'] = 'Apply'
>>> request = testing.TestRequest(form=submit)
>>> content = getForm(request, 'ObjectWidget_single_edit_submit_bool1.html')
>>> print(testing.plainText(content))
Data successfully updated.
...
>>> pprint(obj.subobj)
<ObjectWidgetSingleSubIntegration
singleBool: False
singleChoice: 'three'
singleChoiceOpt: 'four'
singleDate: datetime.date(2014, 6, 21)
singleInt: 1042
singleReadOnly: 'some R/O text'
singleTextLine: 'foobar'>
>>> submit = testing.getSubmitValues(content)
>>> submit['form.widgets.subobj.widgets.singleBool'] = 'true'
>>> submit['form.buttons.apply'] = 'Apply'
>>> request = testing.TestRequest(form=submit)
>>> content = getForm(request, 'ObjectWidget_single_edit_submit_bool2.html')
>>> print(testing.plainText(content))
Data successfully updated.
...
>>> pprint(obj.subobj)
<ObjectWidgetSingleSubIntegration
singleBool: True
singleChoice: 'three'
singleChoiceOpt: 'four'
singleDate: datetime.date(2014, 6, 21)
singleInt: 1042
singleReadOnly: 'some R/O text'
singleTextLine: 'foobar'>