[Zope3-checkins] SVN: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/ Rewritten functional test for boolean radio widget.
Martijn Faassen
faassen at startifact.com
Wed Dec 30 19:31:22 EST 2009
Log message for revision 107414:
Rewritten functional test for boolean radio widget.
Changed:
U zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py
A zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_booleanradiowidget.py
-=-
Modified: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py 2009-12-31 00:31:07 UTC (rev 107413)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/support.py 2009-12-31 00:31:21 UTC (rev 107414)
@@ -15,6 +15,7 @@
$Id$
"""
+import re
class VerifyResults(object):
"""Mix-in for test classes with helpers for checking string data."""
@@ -32,3 +33,15 @@
for check in check_list:
self.assert_(result.find(check) < 0,
"%r unexpectedly found in %r" % (check, result))
+
+def patternExists(pattern, source, flags=0):
+ return re.search(pattern, source, flags) is not None
+
+def validationErrorExists(field, error_msg, source):
+ regex = re.compile(r'%s.*?name="form.(\w+)(?:\.[\w\.]+)?"' % (error_msg,),
+ re.DOTALL)
+ # compile it first because Python 2.3 doesn't allow flags in findall
+ return field in regex.findall(source)
+
+def missingInputErrorExists(field, source):
+ return validationErrorExists(field, 'Required input is missing.', source)
Copied: zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_booleanradiowidget.py (from rev 107362, zope.app.form/branches/faassen-zaf/src/zope/app/form/browser/tests/test_functional_booleanradiowidget.py)
===================================================================
--- zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_booleanradiowidget.py (rev 0)
+++ zope.formlib/branches/faassen-zaf/src/zope/formlib/tests/test_functional_booleanradiowidget.py 2009-12-31 00:31:21 UTC (rev 107414)
@@ -0,0 +1,204 @@
+##############################################################################
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Radio Widget Functional Tests
+
+$Id$
+"""
+import unittest
+
+from zope.interface import Interface, implements
+from zope.schema import Bool
+from zope.formlib import form
+from zope.publisher.browser import TestRequest
+from zope.formlib.tests.support import patternExists
+from zope.formlib.widgets import BooleanRadioWidget
+from zope.component.testing import setUp, tearDown
+from zope.component import provideAdapter
+from zope.i18n.testing import setUp as i18nSetUp
+from zope.publisher.interfaces.browser import IBrowserRequest
+import zope.formlib
+from zope.formlib.tests.test_formlib import requestToTZInfo
+from zope.formlib.exception import WidgetInputErrorView
+from zope.formlib.interfaces import IWidgetInputError, IWidgetInputErrorView
+import zope.interface
+from zope.formlib.errors import InvalidErrorView
+import zope.schema.interfaces
+from zope.formlib.interfaces import IInputWidget, IForm
+from zope.interface import Invalid
+from zope.browserpage.namedtemplate import NamedTemplateImplementation
+from zope.component import adapter
+import zope.i18n
+
+ at adapter(IForm)
+ at NamedTemplateImplementation
+def TestTemplate(self):
+ status = self.status
+ if status:
+ status = zope.i18n.translate(status,
+ context=self.request,
+ default=self.status)
+ if getattr(status, 'mapping', 0):
+ status = zope.i18n.interpolate(status, status.mapping)
+
+ result = []
+
+ if self.errors:
+ for error in self.errors:
+ result.append("%s: %s" % (error.__class__.__name__, error))
+
+ for w in self.widgets:
+ result.append(w())
+ error = w.error()
+ if error:
+ result.append(str(error))
+
+ for action in self.availableActions():
+ result.append(action.render())
+
+ return '\n'.join(result)
+
+def formSetUp(test):
+ setUp(test)
+ i18nSetUp(test)
+ provideAdapter(
+ BooleanRadioWidget,
+ (zope.schema.interfaces.IBool,
+ IBrowserRequest),
+ IInputWidget)
+
+ provideAdapter(
+ WidgetInputErrorView,
+ (IWidgetInputError,
+ IBrowserRequest),
+ IWidgetInputErrorView,
+ )
+ provideAdapter(
+ InvalidErrorView,
+ (Invalid,
+ IBrowserRequest),
+ IWidgetInputErrorView,
+ )
+
+ provideAdapter(TestTemplate, name='default')
+ provideAdapter(requestToTZInfo)
+ provideAdapter(
+ zope.formlib.form.render_submit_button, name='render')
+
+class IFoo(Interface):
+
+ bar = Bool(title=u'Bar')
+
+class Foo(object):
+
+ implements(IFoo)
+
+ def __init__(self):
+ self.bar = True
+
+class Form(form.EditForm):
+ form_fields = form.fields(IFoo)
+ form_fields['bar'].custom_widget = BooleanRadioWidget
+
+
+class Test(unittest.TestCase):
+ def setUp(self):
+ formSetUp(self)
+
+ def tearDown(self):
+ tearDown(self)
+
+ def test_display_editform(self):
+ foo = Foo()
+ request = TestRequest()
+ html = Form(foo, request)()
+
+ # bar field should be displayed as two radio buttons
+ self.assert_(patternExists(
+ '<input .*checked="checked".*name="form.bar".*type="radio".*'
+ 'value="on".* />',
+ html))
+ self.assert_(patternExists(
+ '<input .*name="form.bar".*type="radio".*value="off".* />',
+ html))
+
+ # a hidden element is used to note that the field is present
+ self.assert_(patternExists(
+ '<input name="form.bar-empty-marker" type="hidden" value="1".* />',
+ html))
+
+
+ def test_submit_editform(self):
+ foo = Foo()
+ request = TestRequest()
+ request.form['form.bar'] = 'off'
+ request.form['form.actions.apply'] = u''
+ html = Form(foo, request)()
+
+ self.assertEqual(foo.bar, False)
+
+ def test_missing_value(self):
+ foo = Foo()
+ request = TestRequest()
+
+ # temporarily make bar field not required
+ IFoo['bar'].required = False
+
+ # submit missing value for bar
+ request.form['form.bar-empty-marker'] = ''
+ request.form['form.actions.apply'] = u''
+
+ html = Form(foo, request)()
+
+ # confirm use of missing_value as new object value
+ self.assert_(IFoo['bar'].missing_value is None)
+ self.assert_(foo.bar is None)
+
+ # restore bar required state
+ IFoo['bar'].required = True
+
+
+ def test_required_validation(self):
+ foo = Foo()
+ request = TestRequest()
+
+ self.assert_(IFoo['bar'].required)
+
+ # submit missing value for bar
+ request.form['form.bar-empty-marker'] = ''
+ request.form['form.actions.apply'] = u''
+
+ html = Form(foo, request)()
+
+ # confirm error msgs
+ self.assert_('Required input is missing' in html)
+
+
+ def test_invalid_allowed_value(self):
+ foo = Foo()
+ request = TestRequest()
+
+ # submit a value for bar isn't allowed
+ request.form['form.bar'] = 'bogus'
+ request.form['form.actions.apply'] = u''
+ html = Form(foo, request)()
+
+ self.assert_('Invalid value' in html)
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(Test))
+ return suite
+
+if __name__=='__main__':
+ unittest.main(defaultTest='test_suite')
More information about the Zope3-Checkins
mailing list