1 | # Tests for datetimewidgets |
---|
2 | import datetime |
---|
3 | import pytz |
---|
4 | from zope import schema |
---|
5 | from zope.component import getGlobalSiteManager |
---|
6 | from zope.formlib import form |
---|
7 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
8 | from zope.formlib.tests.test_functional_textwidget import( |
---|
9 | FunctionalWidgetTestCase,) |
---|
10 | from zope.formlib.textwidgets import TextWidget |
---|
11 | from zope.interface import Interface, implements |
---|
12 | from zope.interface.verify import verifyClass, verifyObject |
---|
13 | from zope.publisher.browser import TestRequest |
---|
14 | from waeup.kofa.interfaces import IKofaUtils |
---|
15 | from waeup.kofa.widgets.datetimewidget import PytzDatetimeWidget |
---|
16 | |
---|
17 | class IContent(Interface): |
---|
18 | my_dt = schema.Datetime( |
---|
19 | title = u'A datetime.', |
---|
20 | ) |
---|
21 | |
---|
22 | class Content(object): |
---|
23 | implements(IContent) |
---|
24 | my_dt = None |
---|
25 | |
---|
26 | class SampleForm(form.EditForm): |
---|
27 | form_fields = form.fields(IContent) |
---|
28 | form_fields['my_dt'].custom_widget = PytzDatetimeWidget |
---|
29 | |
---|
30 | class FakeUtils(object): |
---|
31 | # Fake app-wide set timezone. |
---|
32 | implements(IKofaUtils) |
---|
33 | tzinfo = pytz.timezone('America/Sao_Paulo') |
---|
34 | |
---|
35 | |
---|
36 | class PytzDatetimeWidgetTests(FunctionalWidgetTestCase): |
---|
37 | |
---|
38 | widgets = [ |
---|
39 | (schema.interfaces.IDatetime, TextWidget), |
---|
40 | ] |
---|
41 | |
---|
42 | def test_iface(self): |
---|
43 | # make sure we fullfill interface contracts |
---|
44 | obj = PytzDatetimeWidget(IContent['my_dt'], TestRequest()) |
---|
45 | verifyClass(IInputWidget, PytzDatetimeWidget) |
---|
46 | verifyObject(IInputWidget, obj) |
---|
47 | return |
---|
48 | |
---|
49 | def test_to_value(self): |
---|
50 | # we get always UTC-based values |
---|
51 | content = Content() |
---|
52 | request = TestRequest() |
---|
53 | |
---|
54 | request.form['form.my_dt'] = u'2012-01-02 12:10:19 GMT+1' |
---|
55 | request.form['form.actions.apply'] = u'' |
---|
56 | SampleForm(content, request)() |
---|
57 | |
---|
58 | self.assertEqual( content.my_dt, datetime.datetime( |
---|
59 | 2012, 1, 2, 11, 10, 19, tzinfo=pytz.utc)) |
---|
60 | self.assertTrue(content.my_dt.tzinfo is pytz.utc) |
---|
61 | return |
---|
62 | |
---|
63 | def test_export_values(self): |
---|
64 | # make sure we understand values as exported |
---|
65 | content = Content() |
---|
66 | request = TestRequest() |
---|
67 | |
---|
68 | request.form['form.my_dt'] = u'2012-01-02 12:10:19+00:00' |
---|
69 | request.form['form.actions.apply'] = u'' |
---|
70 | SampleForm(content, request)() |
---|
71 | |
---|
72 | self.assertEqual( content.my_dt, datetime.datetime( |
---|
73 | 2012, 1, 2, 12, 10, 19, tzinfo=pytz.utc)) |
---|
74 | self.assertTrue(content.my_dt.tzinfo is pytz.utc) |
---|
75 | return |
---|
76 | |
---|
77 | def setUp(self): |
---|
78 | super(PytzDatetimeWidgetTests, self).setUp() |
---|
79 | self.gsm = getGlobalSiteManager() |
---|
80 | self.utils = FakeUtils() |
---|
81 | self.gsm.registerUtility(self.utils) |
---|
82 | return |
---|
83 | |
---|
84 | def tearDown(self): |
---|
85 | super(PytzDatetimeWidgetTests, self).tearDown() |
---|
86 | self.gsm.unregisterUtility(self.utils) |
---|
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 |
---|