source: main/waeup.kofa/branches/uli-stud-utils-cleanup/src/waeup/kofa/widgets/tests/test_datetimewidget.py @ 13621

Last change on this file since 13621 was 8208, checked in by uli, 13 years ago

Move setUp/tearDown to top.

File size: 3.3 KB
Line 
1# Tests for datetimewidgets
2import datetime
3import pytz
4from zope import schema
5from zope.component import getGlobalSiteManager
6from zope.formlib import form
7from zope.formlib.interfaces import IInputWidget, IDisplayWidget
8from zope.formlib.tests.test_functional_textwidget import(
9    FunctionalWidgetTestCase,)
10from zope.formlib.textwidgets import TextWidget
11from zope.interface import Interface, implements
12from zope.interface.verify import verifyClass, verifyObject
13from zope.publisher.browser import TestRequest
14from waeup.kofa.interfaces import IKofaUtils
15from waeup.kofa.widgets.datetimewidget import PytzDatetimeWidget
16
17class IContent(Interface):
18    my_dt = schema.Datetime(
19        title = u'A datetime.',
20        )
21
22class Content(object):
23    implements(IContent)
24    my_dt = None
25
26class SampleForm(form.EditForm):
27    form_fields = form.fields(IContent)
28    form_fields['my_dt'].custom_widget = PytzDatetimeWidget
29
30class FakeUtils(object):
31    # Fake app-wide set timezone.
32    implements(IKofaUtils)
33    tzinfo = pytz.timezone('America/Sao_Paulo')
34
35
36class PytzDatetimeWidgetTests(FunctionalWidgetTestCase):
37
38    widgets = [
39        (schema.interfaces.IDatetime, TextWidget),
40        ]
41
42    def setUp(self):
43        super(PytzDatetimeWidgetTests, self).setUp()
44        self.gsm = getGlobalSiteManager()
45        self.utils = FakeUtils()
46        self.gsm.registerUtility(self.utils)
47        return
48
49    def tearDown(self):
50        super(PytzDatetimeWidgetTests, self).tearDown()
51        self.gsm.unregisterUtility(self.utils)
52        return
53
54    def test_iface(self):
55        # make sure we fullfill interface contracts
56        obj = PytzDatetimeWidget(IContent['my_dt'], TestRequest())
57        verifyClass(IInputWidget, PytzDatetimeWidget)
58        verifyObject(IInputWidget, obj)
59        return
60
61    def test_to_value(self):
62        # we get always UTC-based values
63        content = Content()
64        request = TestRequest()
65
66        request.form['form.my_dt'] = u'2012-01-02 12:10:19 GMT+1'
67        request.form['form.actions.apply'] = u''
68        SampleForm(content, request)()
69
70        self.assertEqual( content.my_dt, datetime.datetime(
71            2012, 1, 2, 11, 10, 19, tzinfo=pytz.utc))
72        self.assertTrue(content.my_dt.tzinfo is pytz.utc)
73        return
74
75    def test_export_values(self):
76        # make sure we understand values as exported
77        content = Content()
78        request = TestRequest()
79
80        request.form['form.my_dt'] = u'2012-01-02 12:10:19+00:00'
81        request.form['form.actions.apply'] = u''
82        SampleForm(content, request)()
83
84        self.assertEqual( content.my_dt, datetime.datetime(
85            2012, 1, 2, 12, 10, 19, tzinfo=pytz.utc))
86        self.assertTrue(content.my_dt.tzinfo is pytz.utc)
87        return
88
89    def test_datetimes_wo_tz(self):
90        # Datetimes w/o tz are considered to be meant in app-wide timezone.
91        content = Content()
92        request = TestRequest()
93
94        request.form['form.my_dt'] = u'2011-11-11 11:11:11'
95        request.form['form.actions.apply'] = u''
96        SampleForm(content, request)()
97
98        # Sao Paulo was two hours back UTC on 2011-11-11
99        self.assertEqual(content.my_dt, datetime.datetime(
100            2011, 11, 11, 13, 11, 11, tzinfo=pytz.utc))
101        self.assertTrue(content.my_dt.tzinfo is pytz.utc)
102        return
Note: See TracBrowser for help on using the repository browser.