[7196] | 1 | ## $Id: test_datewidget.py 9499 2012-11-02 02:19:06Z uli $ |
---|
[6055] | 2 | ## |
---|
[7196] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[6055] | 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. |
---|
[7196] | 8 | ## |
---|
[6055] | 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. |
---|
[7196] | 13 | ## |
---|
[6055] | 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 |
---|
[9499] | 24 | from zope.interface import Interface, implementer |
---|
[6055] | 25 | from zope.interface.verify import verifyClass |
---|
[9499] | 26 | from zope.publisher.browser import TestRequest |
---|
[6055] | 27 | |
---|
| 28 | from zope.formlib.tests.test_browserwidget import ( |
---|
| 29 | SimpleInputWidgetTest, BrowserWidgetTest, ) |
---|
| 30 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
[7811] | 31 | from waeup.kofa.widgets.datewidget import ( |
---|
[6055] | 32 | FormattedDateWidget, FormattedDateDisplayWidget, ) |
---|
| 33 | from zope.formlib.widgets import DateI18nWidget |
---|
| 34 | from zope.formlib.interfaces import ConversionError, WidgetInputError |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | class FormattedDateWidgetTest(SimpleInputWidgetTest): |
---|
| 38 | """Documents and tests the formatted date widget. |
---|
| 39 | |
---|
| 40 | >>> verifyClass(IInputWidget, FormattedDateWidget) |
---|
| 41 | True |
---|
| 42 | """ |
---|
| 43 | |
---|
| 44 | _FieldFactory = Date |
---|
| 45 | _WidgetFactory = FormattedDateWidget |
---|
| 46 | |
---|
[9499] | 47 | def setUpContent(self, desc=u'', title=u'Foo Title'): |
---|
| 48 | # same as in base class but with a min value of date(1910, 1, 1) |
---|
| 49 | field = self._FieldFactory( |
---|
| 50 | __name__='foo', title=title, description=desc, |
---|
| 51 | min=datetime.date(1910, 1, 1)) |
---|
| 52 | class ITestContent(Interface): |
---|
| 53 | foo = field |
---|
| 54 | @implementer(ITestContent) |
---|
| 55 | class TestObject: |
---|
| 56 | pass |
---|
| 57 | self.content = TestObject() |
---|
| 58 | field = ITestContent['foo'] |
---|
| 59 | self.field = field.bind(self.content) |
---|
| 60 | request = TestRequest(HTTP_ACCEPT_LANGUAGE='ru') |
---|
| 61 | request.form['field.foo'] = u'Foo Value' |
---|
| 62 | self._widget = self._WidgetFactory(field, request) |
---|
| 63 | |
---|
[6055] | 64 | def testRender(self): |
---|
| 65 | super(FormattedDateWidgetTest, self).testRender( |
---|
| 66 | datetime.date(2003, 3, 26), |
---|
| 67 | ('type="text"', 'id="field.foo"', 'name="field.foo"', |
---|
| 68 | 'value="2003-03-26"')) |
---|
| 69 | |
---|
| 70 | def testRenderCustomFormat(self): |
---|
| 71 | self._widget.date_format = "%d/%m/%y" |
---|
| 72 | super(FormattedDateWidgetTest, self).testRender( |
---|
| 73 | datetime.datetime(2004, 3, 26, 12, 58, 59), |
---|
| 74 | ('type="text"', 'id="field.foo"', 'name="field.foo"', |
---|
| 75 | 'value="26/03/04"')) |
---|
| 76 | |
---|
[8147] | 77 | def testProperties(self): |
---|
| 78 | self.assertEqual(self._widget.tag, 'input') |
---|
| 79 | self.assertEqual(self._widget.type, 'text') |
---|
| 80 | # By default the date format is ISO and the rendered CSS 'datepicker' |
---|
| 81 | self.assertEqual(self._widget.cssClass, 'datepicker') |
---|
| 82 | self.assertEqual(self._widget.extra, '') |
---|
| 83 | |
---|
[6055] | 84 | def test_hasInput(self): |
---|
| 85 | del self._widget.request.form['field.foo'] |
---|
[7501] | 86 | self.assertFalse(self._widget.hasInput()) |
---|
[6055] | 87 | self._widget.request.form['field.foo'] = u'' |
---|
[7501] | 88 | self.assertTrue(self._widget.hasInput()) |
---|
[6055] | 89 | self._widget.request.form['field.foo'] = u'2003-03-26' |
---|
[7501] | 90 | self.assertTrue(self._widget.hasInput()) |
---|
[6055] | 91 | |
---|
| 92 | def test_getDefaultInputValue(self, |
---|
| 93 | value=u'2004-03-26', |
---|
| 94 | check_value=datetime.date(2004, 3, 26)): |
---|
| 95 | self._widget.request.form['field.foo'] = u'' |
---|
| 96 | self.assertRaises(WidgetInputError, self._widget.getInputValue) |
---|
| 97 | self._widget.request.form['field.foo'] = value |
---|
| 98 | self.assertEquals(self._widget.getInputValue(), check_value) |
---|
| 99 | self._widget.request.form['field.foo'] = u'abc' |
---|
| 100 | self.assertRaises(ConversionError, self._widget.getInputValue) |
---|
| 101 | |
---|
| 102 | def test_getCustomInputValue_de(self): |
---|
| 103 | self._widget.date_format = "%d.%m.%y" |
---|
| 104 | self.test_getDefaultInputValue(u'26.03.04') |
---|
| 105 | |
---|
| 106 | def test_getCustomInputValue_de2(self): |
---|
| 107 | self._widget.date_format = "%d.%m.%Y" |
---|
| 108 | self.test_getDefaultInputValue(u'26.03.2004') |
---|
| 109 | |
---|
| 110 | def test_getCustomInputValue_us(self): |
---|
| 111 | self._widget.date_format = "%m/%d/%Y" |
---|
| 112 | self.test_getDefaultInputValue(u'03/26/2004') |
---|
| 113 | |
---|
[9499] | 114 | def test_minimal_value_respected(self): |
---|
| 115 | # we did set up the date field to require dates >= 1900-01-01 |
---|
| 116 | request = TestRequest() |
---|
| 117 | # setting a date > 1900-01-01 is okay |
---|
| 118 | request.form['field.foo'] = '1912-03-27' |
---|
| 119 | widget = FormattedDateWidget(self.field, request) |
---|
| 120 | self.assertEqual( |
---|
| 121 | widget.getInputValue(), datetime.date(1912, 3, 27)) |
---|
| 122 | |
---|
| 123 | # setting a date < 1900-01-01 will fail |
---|
| 124 | request.form['field.foo'] = '1812-03-27' |
---|
| 125 | widget = FormattedDateWidget(self.field, request) |
---|
| 126 | self.assertRaises( |
---|
| 127 | WidgetInputError, |
---|
| 128 | widget.getInputValue) |
---|
| 129 | # check the correct exception message |
---|
| 130 | try: |
---|
| 131 | widget.getInputValue() |
---|
| 132 | except WidgetInputError as exc: |
---|
| 133 | # just catch the exception |
---|
| 134 | pass |
---|
| 135 | exc = '%r' % exc # turn exception into string |
---|
| 136 | self.assertEqual( |
---|
| 137 | exc, |
---|
| 138 | "WidgetInputError('foo', u'Foo Title', " |
---|
| 139 | "TooSmall(datetime.date(1812, 3, 27), datetime.date(1910, 1, 1)))" |
---|
| 140 | ) |
---|
| 141 | return |
---|
| 142 | |
---|
[6055] | 143 | class FormattedDateDisplayWidgetTest(BrowserWidgetTest): |
---|
| 144 | """The FormatterdDisplayDateWidget complies with IDisplayWidget. |
---|
| 145 | |
---|
| 146 | >>> verifyClass(IDisplayWidget, FormattedDateDisplayWidget) |
---|
| 147 | True |
---|
| 148 | """ |
---|
| 149 | |
---|
| 150 | _WidgetFactory = FormattedDateDisplayWidget |
---|
| 151 | expected_class = "date" |
---|
| 152 | |
---|
| 153 | def setUp(self): |
---|
| 154 | super(FormattedDateDisplayWidgetTest, self).setUp() |
---|
| 155 | self._value = datetime.date(2004, 12, 01) |
---|
| 156 | |
---|
| 157 | def testDefaultDisplayStyle(self): |
---|
[7501] | 158 | self.assertFalse(self._widget.displayStyle) |
---|
[6055] | 159 | |
---|
| 160 | def testRenderDefault(self): |
---|
| 161 | self._widget.setRenderedValue(self._value) |
---|
| 162 | self.verifyResult(self._widget(), |
---|
| 163 | ["<span", |
---|
| 164 | 'class="%s"' % self.expected_class, |
---|
| 165 | "2004-12-01", |
---|
| 166 | "</span"]) |
---|
| 167 | |
---|
| 168 | def testRenderCustom(self): |
---|
| 169 | self._widget.setRenderedValue(self._value) |
---|
| 170 | self._widget.date_format = '%m/%d/%Y' |
---|
| 171 | self.verifyResult(self._widget(), |
---|
| 172 | ["<span", |
---|
| 173 | 'class="%s"' % self.expected_class, |
---|
| 174 | "12/01/2004", |
---|
| 175 | "</span"]) |
---|
| 176 | |
---|
| 177 | def testRenderNone(self): |
---|
| 178 | self._widget.setRenderedValue(None) |
---|
| 179 | self._widget.date_format = '%m/%d/%Y' |
---|
| 180 | self.assertEqual(self._widget(), '') |
---|
| 181 | |
---|
[6558] | 182 | def testNoValueSet(self): |
---|
| 183 | self._widget.date_format = '%m/%d/%Y' |
---|
| 184 | self.assertEqual(self._widget(), '') |
---|
| 185 | |
---|
[6055] | 186 | def test_suite(): |
---|
| 187 | return unittest.TestSuite(( |
---|
| 188 | unittest.makeSuite(FormattedDateWidgetTest), |
---|
| 189 | unittest.makeSuite(FormattedDateDisplayWidgetTest), |
---|
| 190 | doctest.DocTestSuite(), |
---|
| 191 | )) |
---|
| 192 | |
---|