1 | # Tests for datetimewidgets |
---|
2 | import datetime |
---|
3 | import pytz |
---|
4 | from zope import schema |
---|
5 | from zope.formlib import form |
---|
6 | from zope.formlib.interfaces import IInputWidget, IDisplayWidget |
---|
7 | from zope.formlib.tests.test_functional_textwidget import( |
---|
8 | FunctionalWidgetTestCase,) |
---|
9 | from zope.formlib.textwidgets import TextWidget |
---|
10 | from zope.interface import Interface, implements |
---|
11 | from zope.interface.verify import verifyClass, verifyObject |
---|
12 | from zope.publisher.browser import TestRequest |
---|
13 | from waeup.kofa.widgets.datetimewidget import PytzDatetimeWidget |
---|
14 | |
---|
15 | class IContent(Interface): |
---|
16 | my_dt = schema.Datetime( |
---|
17 | title = u'A datetime.', |
---|
18 | ) |
---|
19 | |
---|
20 | class Content(object): |
---|
21 | implements(IContent) |
---|
22 | my_dt = None |
---|
23 | |
---|
24 | class SampleForm(form.EditForm): |
---|
25 | form_fields = form.fields(IContent) |
---|
26 | form_fields['my_dt'].custom_widget = PytzDatetimeWidget |
---|
27 | |
---|
28 | class 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 |
---|