1 | ## $Id: test_datewidget.py 7196 2011-11-25 07:44:52Z henrik $ |
---|
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 | """ |
---|
20 | import datetime |
---|
21 | import unittest |
---|
22 | import doctest |
---|
23 | from zope.schema import Date |
---|
24 | from zope.interface.verify import verifyClass |
---|
25 | |
---|
26 | from zope.formlib.tests.test_browserwidget import ( |
---|
27 | SimpleInputWidgetTest, BrowserWidgetTest, ) |
---|
28 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
29 | from waeup.sirp.widgets.datewidget import ( |
---|
30 | FormattedDateWidget, FormattedDateDisplayWidget, ) |
---|
31 | from zope.formlib.widgets import DateI18nWidget |
---|
32 | from zope.formlib.interfaces import ConversionError, WidgetInputError |
---|
33 | |
---|
34 | |
---|
35 | class 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.failIf(self._widget.hasInput()) |
---|
61 | self._widget.request.form['field.foo'] = u'' |
---|
62 | self.failUnless(self._widget.hasInput()) |
---|
63 | self._widget.request.form['field.foo'] = u'2003-03-26' |
---|
64 | self.failUnless(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 | |
---|
88 | class 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.failIf(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 | |
---|
131 | def test_suite(): |
---|
132 | return unittest.TestSuite(( |
---|
133 | unittest.makeSuite(FormattedDateWidgetTest), |
---|
134 | unittest.makeSuite(FormattedDateDisplayWidgetTest), |
---|
135 | doctest.DocTestSuite(), |
---|
136 | )) |
---|
137 | |
---|