1 | """Formatted Datetime widget tests. |
---|
2 | """ |
---|
3 | import datetime |
---|
4 | import unittest |
---|
5 | import doctest |
---|
6 | from zope.schema import Datetime |
---|
7 | from zope.interface.verify import verifyClass |
---|
8 | |
---|
9 | from zope.datetime import parseDatetimetz |
---|
10 | from zope.formlib.tests.test_browserwidget import ( |
---|
11 | SimpleInputWidgetTest, BrowserWidgetTest, ) |
---|
12 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
13 | from waeup.kofa.widgets.datetimewidget import ( |
---|
14 | FormattedDatetimeWidget, FormattedDatetimeDisplayWidget, ) |
---|
15 | from zope.formlib.widgets import DateI18nWidget |
---|
16 | from zope.formlib.interfaces import ConversionError, WidgetInputError |
---|
17 | |
---|
18 | |
---|
19 | class FormattedDatetimeWidgetTest(SimpleInputWidgetTest): |
---|
20 | """Documents and tests the formatted datetime widget. |
---|
21 | |
---|
22 | >>> verifyClass(IInputWidget, FormattedDatetimeWidget) |
---|
23 | True |
---|
24 | """ |
---|
25 | |
---|
26 | _FieldFactory = Datetime |
---|
27 | _WidgetFactory = FormattedDatetimeWidget |
---|
28 | |
---|
29 | def testRender(self): |
---|
30 | super(FormattedDatetimeWidgetTest, self).testRender( |
---|
31 | datetime.datetime(2003, 3, 26, 12, 10, 20), |
---|
32 | ('type="text"', 'id="field.foo"', 'name="field.foo"', |
---|
33 | 'value="2003-03-26 12:10:20"')) |
---|
34 | |
---|
35 | def testRenderCustomFormat(self): |
---|
36 | self._widget.date_format = "%d/%m/%y %H:%M:%S" |
---|
37 | super(FormattedDatetimeWidgetTest, self).testRender( |
---|
38 | datetime.datetime(2004, 3, 26, 12, 10, 20), |
---|
39 | ('type="text"', 'id="field.foo"', 'name="field.foo"', |
---|
40 | 'value="26/03/04 12:10:20"')) |
---|
41 | |
---|
42 | def testProperties(self): |
---|
43 | self.assertEqual(self._widget.tag, 'input') |
---|
44 | self.assertEqual(self._widget.type, 'text') |
---|
45 | # By default the datetime format is ISO and the rendered CSS |
---|
46 | # 'datetimepicker' |
---|
47 | self.assertEqual(self._widget.cssClass, 'datetimepicker') |
---|
48 | self.assertEqual(self._widget.extra, '') |
---|
49 | |
---|
50 | def test_hasInput(self): |
---|
51 | del self._widget.request.form['field.foo'] |
---|
52 | self.assertFalse(self._widget.hasInput()) |
---|
53 | self._widget.request.form['field.foo'] = u'' |
---|
54 | self.assertTrue(self._widget.hasInput()) |
---|
55 | self._widget.request.form['field.foo'] = u'2003-03-26 12:10:23' |
---|
56 | self.assertTrue(self._widget.hasInput()) |
---|
57 | |
---|
58 | def test_getDefaultInputValue(self, |
---|
59 | value=u'2004-03-26 12:10:20', |
---|
60 | check_value = parseDatetimetz('2004-03-26 12:10:20 GMT')): |
---|
61 | self._widget.request.form['field.foo'] = u'' |
---|
62 | self.assertRaises(WidgetInputError, self._widget.getInputValue) |
---|
63 | self._widget.request.form['field.foo'] = value |
---|
64 | self.assertEquals(self._widget.getInputValue(), check_value) |
---|
65 | self._widget.request.form['field.foo'] = u'abc' |
---|
66 | self.assertRaises(ConversionError, self._widget.getInputValue) |
---|
67 | |
---|
68 | def test_getCustomInputValue_de(self): |
---|
69 | self._widget.date_format = "%d.%m.%y %H:%M:%S" |
---|
70 | self.test_getDefaultInputValue(u'26.03.04 12:10:20') |
---|
71 | |
---|
72 | def test_getCustomInputValue_de2(self): |
---|
73 | self._widget.date_format = "%d.%m.%Y %H:%M:%S" |
---|
74 | self.test_getDefaultInputValue(u'26.03.2004 12:10:20') |
---|
75 | |
---|
76 | def test_getCustomInputValue_us(self): |
---|
77 | self._widget.date_format = "%m/%d/%Y %H:%M:%S" |
---|
78 | self.test_getDefaultInputValue(u'03/26/2004 12:10:20') |
---|
79 | |
---|
80 | class FormattedDatetimeDisplayWidgetTest(BrowserWidgetTest): |
---|
81 | """The FormatterdDisplayDatetimeWidget complies with IDisplayWidget. |
---|
82 | |
---|
83 | >>> verifyClass(IDisplayWidget, FormattedDatetimeDisplayWidget) |
---|
84 | True |
---|
85 | """ |
---|
86 | |
---|
87 | _WidgetFactory = FormattedDatetimeDisplayWidget |
---|
88 | expected_class = "dateTime" |
---|
89 | |
---|
90 | def setUp(self): |
---|
91 | super(FormattedDatetimeDisplayWidgetTest, self).setUp() |
---|
92 | self._value = datetime.date(2004, 12, 01) |
---|
93 | |
---|
94 | def testDefaultDisplayStyle(self): |
---|
95 | self.assertFalse(self._widget.displayStyle) |
---|
96 | |
---|
97 | def testRenderDefault(self): |
---|
98 | self._widget.setRenderedValue(self._value) |
---|
99 | self.verifyResult(self._widget(), |
---|
100 | ["<span", |
---|
101 | 'class="%s"' % self.expected_class, |
---|
102 | "2004-12-01", |
---|
103 | "</span"]) |
---|
104 | |
---|
105 | def testRenderCustom(self): |
---|
106 | self._widget.setRenderedValue(self._value) |
---|
107 | self._widget.date_format = '%m/%d/%Y' |
---|
108 | self.verifyResult(self._widget(), |
---|
109 | ["<span", |
---|
110 | 'class="%s"' % self.expected_class, |
---|
111 | "12/01/2004", |
---|
112 | "</span"]) |
---|
113 | |
---|
114 | def testRenderNone(self): |
---|
115 | self._widget.setRenderedValue(None) |
---|
116 | self._widget.date_format = '%m/%d/%Y' |
---|
117 | self.assertEqual(self._widget(), '') |
---|
118 | |
---|
119 | def testNoValueSet(self): |
---|
120 | self._widget.date_format = '%m/%d/%Y' |
---|
121 | self.assertEqual(self._widget(), '') |
---|
122 | |
---|
123 | def test_suite(): |
---|
124 | return unittest.TestSuite(( |
---|
125 | unittest.makeSuite(FormattedDatetimeWidgetTest), |
---|
126 | unittest.makeSuite(FormattedDatetimeDisplayWidgetTest), |
---|
127 | doctest.DocTestSuite(), |
---|
128 | )) |
---|