source: main/waeup.sirp/trunk/src/waeup/sirp/widgets/tests/test_datewidget.py @ 6222

Last change on this file since 6222 was 6055, checked in by uli, 14 years ago

Add tests for formattable date widget.

File size: 4.9 KB
Line 
1##
2## test_datewidget.py
3## Login : <uli@pu.smp.net>
4## Started on  Thu May 12 02:17:06 2011 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2011 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""Formatted Date widget tests.
23"""
24import datetime
25import unittest
26import doctest
27from zope.schema import Date
28from zope.interface.verify import verifyClass
29
30from zope.formlib.tests.test_browserwidget import (
31    SimpleInputWidgetTest, BrowserWidgetTest, )
32from zope.formlib.interfaces import IInputWidget, IDisplayWidget
33from waeup.sirp.widgets.datewidget import (
34    FormattedDateWidget, FormattedDateDisplayWidget, )
35from zope.formlib.widgets import DateI18nWidget
36from zope.formlib.interfaces import ConversionError, WidgetInputError
37
38
39class FormattedDateWidgetTest(SimpleInputWidgetTest):
40    """Documents and tests the formatted date widget.
41
42        >>> verifyClass(IInputWidget, FormattedDateWidget)
43        True
44    """
45
46    _FieldFactory = Date
47    _WidgetFactory = FormattedDateWidget
48
49    def testRender(self):
50        super(FormattedDateWidgetTest, self).testRender(
51            datetime.date(2003, 3, 26),
52            ('type="text"', 'id="field.foo"', 'name="field.foo"',
53                'value="2003-03-26"'))
54
55    def testRenderCustomFormat(self):
56        self._widget.date_format = "%d/%m/%y"
57        super(FormattedDateWidgetTest, self).testRender(
58            datetime.datetime(2004, 3, 26, 12, 58, 59),
59            ('type="text"', 'id="field.foo"', 'name="field.foo"',
60                'value="26/03/04"'))
61
62    def test_hasInput(self):
63        del self._widget.request.form['field.foo']
64        self.failIf(self._widget.hasInput())
65        self._widget.request.form['field.foo'] = u''
66        self.failUnless(self._widget.hasInput())
67        self._widget.request.form['field.foo'] = u'2003-03-26'
68        self.failUnless(self._widget.hasInput())
69
70    def test_getDefaultInputValue(self,
71            value=u'2004-03-26',
72            check_value=datetime.date(2004, 3, 26)):
73        self._widget.request.form['field.foo'] = u''
74        self.assertRaises(WidgetInputError, self._widget.getInputValue)
75        self._widget.request.form['field.foo'] = value
76        self.assertEquals(self._widget.getInputValue(), check_value)
77        self._widget.request.form['field.foo'] = u'abc'
78        self.assertRaises(ConversionError, self._widget.getInputValue)
79
80    def test_getCustomInputValue_de(self):
81        self._widget.date_format = "%d.%m.%y"
82        self.test_getDefaultInputValue(u'26.03.04')
83
84    def test_getCustomInputValue_de2(self):
85        self._widget.date_format = "%d.%m.%Y"
86        self.test_getDefaultInputValue(u'26.03.2004')
87
88    def test_getCustomInputValue_us(self):
89        self._widget.date_format = "%m/%d/%Y"
90        self.test_getDefaultInputValue(u'03/26/2004')
91
92class FormattedDateDisplayWidgetTest(BrowserWidgetTest):
93    """The FormatterdDisplayDateWidget complies with IDisplayWidget.
94
95        >>> verifyClass(IDisplayWidget, FormattedDateDisplayWidget)
96        True
97    """
98
99    _WidgetFactory = FormattedDateDisplayWidget
100    expected_class = "date"
101
102    def setUp(self):
103        super(FormattedDateDisplayWidgetTest, self).setUp()
104        self._value = datetime.date(2004, 12, 01)
105
106    def testDefaultDisplayStyle(self):
107        self.failIf(self._widget.displayStyle)
108
109    def testRenderDefault(self):
110        self._widget.setRenderedValue(self._value)
111        self.verifyResult(self._widget(),
112                          ["<span",
113                           'class="%s"' % self.expected_class,
114                           "2004-12-01",
115                           "</span"])
116
117    def testRenderCustom(self):
118        self._widget.setRenderedValue(self._value)
119        self._widget.date_format = '%m/%d/%Y'
120        self.verifyResult(self._widget(),
121                          ["<span",
122                           'class="%s"' % self.expected_class,
123                           "12/01/2004",
124                           "</span"])
125
126    def testRenderNone(self):
127        self._widget.setRenderedValue(None)
128        self._widget.date_format = '%m/%d/%Y'
129        self.assertEqual(self._widget(), '')
130
131def test_suite():
132    return unittest.TestSuite((
133        unittest.makeSuite(FormattedDateWidgetTest),
134        unittest.makeSuite(FormattedDateDisplayWidgetTest),
135        doctest.DocTestSuite(),
136        ))
137
Note: See TracBrowser for help on using the repository browser.