Ignore:
Timestamp:
2 Nov 2012, 02:19:06 (12 years ago)
Author:
uli
Message:

Check that minimal values are respected in date widgets.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_datewidget.py

    r8147 r9499  
    2222import doctest
    2323from zope.schema import Date
     24from zope.interface import Interface, implementer
    2425from zope.interface.verify import verifyClass
     26from zope.publisher.browser import TestRequest
    2527
    2628from zope.formlib.tests.test_browserwidget import (
     
    4244    _FieldFactory = Date
    4345    _WidgetFactory = FormattedDateWidget
     46
     47    def setUpContent(self, desc=u'', title=u'Foo Title'):
     48        # same as in base class but with a min value of date(1910, 1, 1)
     49        field = self._FieldFactory(
     50            __name__='foo', title=title, description=desc,
     51            min=datetime.date(1910, 1, 1))
     52        class ITestContent(Interface):
     53            foo = field
     54        @implementer(ITestContent)
     55        class TestObject:
     56            pass
     57        self.content = TestObject()
     58        field = ITestContent['foo']
     59        self.field = field.bind(self.content)
     60        request = TestRequest(HTTP_ACCEPT_LANGUAGE='ru')
     61        request.form['field.foo'] = u'Foo Value'
     62        self._widget = self._WidgetFactory(field, request)
    4463
    4564    def testRender(self):
     
    93112        self.test_getDefaultInputValue(u'03/26/2004')
    94113
     114    def test_minimal_value_respected(self):
     115        # we did set up the date field to require dates >= 1900-01-01
     116        request = TestRequest()
     117        # setting a date > 1900-01-01 is okay
     118        request.form['field.foo'] = '1912-03-27'
     119        widget = FormattedDateWidget(self.field, request)
     120        self.assertEqual(
     121            widget.getInputValue(), datetime.date(1912, 3, 27))
     122
     123        # setting a date < 1900-01-01 will fail
     124        request.form['field.foo'] = '1812-03-27'
     125        widget = FormattedDateWidget(self.field, request)
     126        self.assertRaises(
     127            WidgetInputError,
     128            widget.getInputValue)
     129        # check the correct exception message
     130        try:
     131            widget.getInputValue()
     132        except WidgetInputError as exc:
     133            # just catch the exception
     134            pass
     135        exc = '%r' % exc  # turn exception into string
     136        self.assertEqual(
     137            exc,
     138            "WidgetInputError('foo', u'Foo Title', "
     139            "TooSmall(datetime.date(1812, 3, 27), datetime.date(1910, 1, 1)))"
     140            )
     141        return
     142
    95143class FormattedDateDisplayWidgetTest(BrowserWidgetTest):
    96144    """The FormatterdDisplayDateWidget complies with IDisplayWidget.
Note: See TracChangeset for help on using the changeset viewer.