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

Last change on this file since 11102 was 7501, checked in by uli, 13 years ago

Fix deprecation warnings with Python 2.7

  • Property svn:keywords set to Id
File size: 4.9 KB
Line 
1## $Id: test_datewidget.py 7501 2012-01-23 12:35:12Z 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.sirp.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 test_hasInput(self):
59        del self._widget.request.form['field.foo']
60        self.assertFalse(self._widget.hasInput())
61        self._widget.request.form['field.foo'] = u''
62        self.assertTrue(self._widget.hasInput())
63        self._widget.request.form['field.foo'] = u'2003-03-26'
64        self.assertTrue(self._widget.hasInput())
65
66    def test_getDefaultInputValue(self,
67            value=u'2004-03-26',
68            check_value=datetime.date(2004, 3, 26)):
69        self._widget.request.form['field.foo'] = u''
70        self.assertRaises(WidgetInputError, self._widget.getInputValue)
71        self._widget.request.form['field.foo'] = value
72        self.assertEquals(self._widget.getInputValue(), check_value)
73        self._widget.request.form['field.foo'] = u'abc'
74        self.assertRaises(ConversionError, self._widget.getInputValue)
75
76    def test_getCustomInputValue_de(self):
77        self._widget.date_format = "%d.%m.%y"
78        self.test_getDefaultInputValue(u'26.03.04')
79
80    def test_getCustomInputValue_de2(self):
81        self._widget.date_format = "%d.%m.%Y"
82        self.test_getDefaultInputValue(u'26.03.2004')
83
84    def test_getCustomInputValue_us(self):
85        self._widget.date_format = "%m/%d/%Y"
86        self.test_getDefaultInputValue(u'03/26/2004')
87
88class FormattedDateDisplayWidgetTest(BrowserWidgetTest):
89    """The FormatterdDisplayDateWidget complies with IDisplayWidget.
90
91        >>> verifyClass(IDisplayWidget, FormattedDateDisplayWidget)
92        True
93    """
94
95    _WidgetFactory = FormattedDateDisplayWidget
96    expected_class = "date"
97
98    def setUp(self):
99        super(FormattedDateDisplayWidgetTest, self).setUp()
100        self._value = datetime.date(2004, 12, 01)
101
102    def testDefaultDisplayStyle(self):
103        self.assertFalse(self._widget.displayStyle)
104
105    def testRenderDefault(self):
106        self._widget.setRenderedValue(self._value)
107        self.verifyResult(self._widget(),
108                          ["<span",
109                           'class="%s"' % self.expected_class,
110                           "2004-12-01",
111                           "</span"])
112
113    def testRenderCustom(self):
114        self._widget.setRenderedValue(self._value)
115        self._widget.date_format = '%m/%d/%Y'
116        self.verifyResult(self._widget(),
117                          ["<span",
118                           'class="%s"' % self.expected_class,
119                           "12/01/2004",
120                           "</span"])
121
122    def testRenderNone(self):
123        self._widget.setRenderedValue(None)
124        self._widget.date_format = '%m/%d/%Y'
125        self.assertEqual(self._widget(), '')
126
127    def testNoValueSet(self):
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.