source: main/waeup.kofa/trunk/src/waeup/kofa/widgets/tests/test_datetimewidget.py @ 8201

Last change on this file since 8201 was 8199, checked in by uli, 13 years ago

Add test to make sure we interpret datetime values as exported by the
applicants containers exporter correctly.

File size: 2.1 KB
Line 
1# Tests for datetimewidgets
2import datetime
3import pytz
4from zope import schema
5from zope.formlib import form
6from zope.formlib.interfaces import IInputWidget, IDisplayWidget
7from zope.formlib.tests.test_functional_textwidget import(
8    FunctionalWidgetTestCase,)
9from zope.formlib.textwidgets import TextWidget
10from zope.interface import Interface, implements
11from zope.interface.verify import verifyClass, verifyObject
12from zope.publisher.browser import TestRequest
13from waeup.kofa.widgets.datetimewidget import PytzDatetimeWidget
14
15class IContent(Interface):
16    my_dt = schema.Datetime(
17        title = u'A datetime.',
18        )
19
20class Content(object):
21    implements(IContent)
22    my_dt = None
23
24class SampleForm(form.EditForm):
25    form_fields = form.fields(IContent)
26    form_fields['my_dt'].custom_widget = PytzDatetimeWidget
27
28class PytzDatetimeWidgetTests(FunctionalWidgetTestCase):
29
30    widgets = [
31        (schema.interfaces.IDatetime, TextWidget),
32        ]
33
34    def test_iface(self):
35        # make sure we fullfill interface contracts
36        obj = PytzDatetimeWidget(IContent['my_dt'], TestRequest())
37        verifyClass(IInputWidget, PytzDatetimeWidget)
38        verifyObject(IInputWidget, obj)
39        return
40
41    def test_to_value(self):
42        # we get always UTC-based values
43        content = Content()
44        request = TestRequest()
45
46        request.form['form.my_dt'] = u'2012-01-02 12:10:19 GMT+1'
47        request.form['form.actions.apply'] = u''
48        SampleForm(content, request)()
49
50        self.assertEqual( content.my_dt, datetime.datetime(
51            2012, 1, 2, 11, 10, 19, tzinfo=pytz.utc))
52        self.assertTrue(content.my_dt.tzinfo is pytz.utc)
53        return
54
55    def test_export_values(self):
56        # make sure we understand values as exported
57        content = Content()
58        request = TestRequest()
59
60        request.form['form.my_dt'] = u'2012-01-02 12:10:19+00:00'
61        request.form['form.actions.apply'] = u''
62        SampleForm(content, request)()
63
64        self.assertEqual( content.my_dt, datetime.datetime(
65            2012, 1, 2, 12, 10, 19, tzinfo=pytz.utc))
66        self.assertTrue(content.my_dt.tzinfo is pytz.utc)
67        return
Note: See TracBrowser for help on using the repository browser.