source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_datewidget.py @ 10097

Last change on this file since 10097 was 9499, checked in by uli, 12 years ago

Check that minimal values are respected in date widgets.

  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1## $Id: test_datewidget.py 9499 2012-11-02 02:19:06Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Formatted Date widget tests.
19"""
20import datetime
21import unittest
22import doctest
23from zope.schema import Date
24from zope.interface import Interface, implementer
25from zope.interface.verify import verifyClass
26from zope.publisher.browser import TestRequest
27
28from zope.formlib.tests.test_browserwidget import (
29    SimpleInputWidgetTest, BrowserWidgetTest, )
30from zope.formlib.interfaces import IInputWidget, IDisplayWidget
31from waeup.kofa.widgets.datewidget import (
32    FormattedDateWidget, FormattedDateDisplayWidget, )
33from zope.formlib.widgets import DateI18nWidget
34from zope.formlib.interfaces import ConversionError, WidgetInputError
35
36
37class FormattedDateWidgetTest(SimpleInputWidgetTest):
38    """Documents and tests the formatted date widget.
39
40        >>> verifyClass(IInputWidget, FormattedDateWidget)
41        True
42    """
43
44    _FieldFactory = Date
45    _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)
63
64    def testRender(self):
65        super(FormattedDateWidgetTest, self).testRender(
66            datetime.date(2003, 3, 26),
67            ('type="text"', 'id="field.foo"', 'name="field.foo"',
68                'value="2003-03-26"'))
69
70    def testRenderCustomFormat(self):
71        self._widget.date_format = "%d/%m/%y"
72        super(FormattedDateWidgetTest, self).testRender(
73            datetime.datetime(2004, 3, 26, 12, 58, 59),
74            ('type="text"', 'id="field.foo"', 'name="field.foo"',
75                'value="26/03/04"'))
76
77    def testProperties(self):
78        self.assertEqual(self._widget.tag, 'input')
79        self.assertEqual(self._widget.type, 'text')
80        # By default the date format is ISO and the rendered CSS 'datepicker'
81        self.assertEqual(self._widget.cssClass, 'datepicker')
82        self.assertEqual(self._widget.extra, '')
83
84    def test_hasInput(self):
85        del self._widget.request.form['field.foo']
86        self.assertFalse(self._widget.hasInput())
87        self._widget.request.form['field.foo'] = u''
88        self.assertTrue(self._widget.hasInput())
89        self._widget.request.form['field.foo'] = u'2003-03-26'
90        self.assertTrue(self._widget.hasInput())
91
92    def test_getDefaultInputValue(self,
93            value=u'2004-03-26',
94            check_value=datetime.date(2004, 3, 26)):
95        self._widget.request.form['field.foo'] = u''
96        self.assertRaises(WidgetInputError, self._widget.getInputValue)
97        self._widget.request.form['field.foo'] = value
98        self.assertEquals(self._widget.getInputValue(), check_value)
99        self._widget.request.form['field.foo'] = u'abc'
100        self.assertRaises(ConversionError, self._widget.getInputValue)
101
102    def test_getCustomInputValue_de(self):
103        self._widget.date_format = "%d.%m.%y"
104        self.test_getDefaultInputValue(u'26.03.04')
105
106    def test_getCustomInputValue_de2(self):
107        self._widget.date_format = "%d.%m.%Y"
108        self.test_getDefaultInputValue(u'26.03.2004')
109
110    def test_getCustomInputValue_us(self):
111        self._widget.date_format = "%m/%d/%Y"
112        self.test_getDefaultInputValue(u'03/26/2004')
113
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
143class FormattedDateDisplayWidgetTest(BrowserWidgetTest):
144    """The FormatterdDisplayDateWidget complies with IDisplayWidget.
145
146        >>> verifyClass(IDisplayWidget, FormattedDateDisplayWidget)
147        True
148    """
149
150    _WidgetFactory = FormattedDateDisplayWidget
151    expected_class = "date"
152
153    def setUp(self):
154        super(FormattedDateDisplayWidgetTest, self).setUp()
155        self._value = datetime.date(2004, 12, 01)
156
157    def testDefaultDisplayStyle(self):
158        self.assertFalse(self._widget.displayStyle)
159
160    def testRenderDefault(self):
161        self._widget.setRenderedValue(self._value)
162        self.verifyResult(self._widget(),
163                          ["<span",
164                           'class="%s"' % self.expected_class,
165                           "2004-12-01",
166                           "</span"])
167
168    def testRenderCustom(self):
169        self._widget.setRenderedValue(self._value)
170        self._widget.date_format = '%m/%d/%Y'
171        self.verifyResult(self._widget(),
172                          ["<span",
173                           'class="%s"' % self.expected_class,
174                           "12/01/2004",
175                           "</span"])
176
177    def testRenderNone(self):
178        self._widget.setRenderedValue(None)
179        self._widget.date_format = '%m/%d/%Y'
180        self.assertEqual(self._widget(), '')
181
182    def testNoValueSet(self):
183        self._widget.date_format = '%m/%d/%Y'
184        self.assertEqual(self._widget(), '')
185
186def test_suite():
187    return unittest.TestSuite((
188        unittest.makeSuite(FormattedDateWidgetTest),
189        unittest.makeSuite(FormattedDateDisplayWidgetTest),
190        doctest.DocTestSuite(),
191        ))
192
Note: See TracBrowser for help on using the repository browser.