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

Last change on this file since 8412 was 8147, checked in by uli, 13 years ago

Improve datewidget to set date format from bound schema field.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1## $Id: test_datewidget.py 8147 2012-04-13 16:51:25Z 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.verify import verifyClass
25
26from zope.formlib.tests.test_browserwidget import (
27    SimpleInputWidgetTest, BrowserWidgetTest, )
28from zope.formlib.interfaces import IInputWidget, IDisplayWidget
29from waeup.kofa.widgets.datewidget import (
30    FormattedDateWidget, FormattedDateDisplayWidget, )
31from zope.formlib.widgets import DateI18nWidget
32from zope.formlib.interfaces import ConversionError, WidgetInputError
33
34
35class FormattedDateWidgetTest(SimpleInputWidgetTest):
36    """Documents and tests the formatted date widget.
37
38        >>> verifyClass(IInputWidget, FormattedDateWidget)
39        True
40    """
41
42    _FieldFactory = Date
43    _WidgetFactory = FormattedDateWidget
44
45    def testRender(self):
46        super(FormattedDateWidgetTest, self).testRender(
47            datetime.date(2003, 3, 26),
48            ('type="text"', 'id="field.foo"', 'name="field.foo"',
49                'value="2003-03-26"'))
50
51    def testRenderCustomFormat(self):
52        self._widget.date_format = "%d/%m/%y"
53        super(FormattedDateWidgetTest, self).testRender(
54            datetime.datetime(2004, 3, 26, 12, 58, 59),
55            ('type="text"', 'id="field.foo"', 'name="field.foo"',
56                'value="26/03/04"'))
57
58    def testProperties(self):
59        self.assertEqual(self._widget.tag, 'input')
60        self.assertEqual(self._widget.type, 'text')
61        # By default the date format is ISO and the rendered CSS 'datepicker'
62        self.assertEqual(self._widget.cssClass, 'datepicker')
63        self.assertEqual(self._widget.extra, '')
64
65    def test_hasInput(self):
66        del self._widget.request.form['field.foo']
67        self.assertFalse(self._widget.hasInput())
68        self._widget.request.form['field.foo'] = u''
69        self.assertTrue(self._widget.hasInput())
70        self._widget.request.form['field.foo'] = u'2003-03-26'
71        self.assertTrue(self._widget.hasInput())
72
73    def test_getDefaultInputValue(self,
74            value=u'2004-03-26',
75            check_value=datetime.date(2004, 3, 26)):
76        self._widget.request.form['field.foo'] = u''
77        self.assertRaises(WidgetInputError, self._widget.getInputValue)
78        self._widget.request.form['field.foo'] = value
79        self.assertEquals(self._widget.getInputValue(), check_value)
80        self._widget.request.form['field.foo'] = u'abc'
81        self.assertRaises(ConversionError, self._widget.getInputValue)
82
83    def test_getCustomInputValue_de(self):
84        self._widget.date_format = "%d.%m.%y"
85        self.test_getDefaultInputValue(u'26.03.04')
86
87    def test_getCustomInputValue_de2(self):
88        self._widget.date_format = "%d.%m.%Y"
89        self.test_getDefaultInputValue(u'26.03.2004')
90
91    def test_getCustomInputValue_us(self):
92        self._widget.date_format = "%m/%d/%Y"
93        self.test_getDefaultInputValue(u'03/26/2004')
94
95class FormattedDateDisplayWidgetTest(BrowserWidgetTest):
96    """The FormatterdDisplayDateWidget complies with IDisplayWidget.
97
98        >>> verifyClass(IDisplayWidget, FormattedDateDisplayWidget)
99        True
100    """
101
102    _WidgetFactory = FormattedDateDisplayWidget
103    expected_class = "date"
104
105    def setUp(self):
106        super(FormattedDateDisplayWidgetTest, self).setUp()
107        self._value = datetime.date(2004, 12, 01)
108
109    def testDefaultDisplayStyle(self):
110        self.assertFalse(self._widget.displayStyle)
111
112    def testRenderDefault(self):
113        self._widget.setRenderedValue(self._value)
114        self.verifyResult(self._widget(),
115                          ["<span",
116                           'class="%s"' % self.expected_class,
117                           "2004-12-01",
118                           "</span"])
119
120    def testRenderCustom(self):
121        self._widget.setRenderedValue(self._value)
122        self._widget.date_format = '%m/%d/%Y'
123        self.verifyResult(self._widget(),
124                          ["<span",
125                           'class="%s"' % self.expected_class,
126                           "12/01/2004",
127                           "</span"])
128
129    def testRenderNone(self):
130        self._widget.setRenderedValue(None)
131        self._widget.date_format = '%m/%d/%Y'
132        self.assertEqual(self._widget(), '')
133
134    def testNoValueSet(self):
135        self._widget.date_format = '%m/%d/%Y'
136        self.assertEqual(self._widget(), '')
137
138def test_suite():
139    return unittest.TestSuite((
140        unittest.makeSuite(FormattedDateWidgetTest),
141        unittest.makeSuite(FormattedDateDisplayWidgetTest),
142        doctest.DocTestSuite(),
143        ))
144
Note: See TracBrowser for help on using the repository browser.