1 | ## |
---|
2 | ## test_converters.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Tue May 31 08:34:44 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | """ |
---|
23 | Tests for converterts. |
---|
24 | """ |
---|
25 | import datetime |
---|
26 | import os |
---|
27 | import shutil |
---|
28 | import tempfile |
---|
29 | import unittest |
---|
30 | from zope.app.testing.functional import FunctionalTestCase |
---|
31 | from zope.component.hooks import setSite, clearSite |
---|
32 | from zope.formlib import form |
---|
33 | from zope.interface.verify import verifyClass, verifyObject |
---|
34 | |
---|
35 | from waeup.sirp.app import University |
---|
36 | from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module |
---|
37 | from waeup.sirp.utils.converters import IObjectConverter |
---|
38 | |
---|
39 | from zope import schema |
---|
40 | from zope.interface import Interface, implements, invariant, Invalid |
---|
41 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
42 | |
---|
43 | class IContact(Interface): |
---|
44 | name = schema.TextLine( |
---|
45 | title = u'Name', |
---|
46 | default = u'Manfred' |
---|
47 | ) |
---|
48 | age = schema.Int( |
---|
49 | title = u'Age', |
---|
50 | default = 23, |
---|
51 | required = True, |
---|
52 | ) |
---|
53 | city = schema.TextLine( |
---|
54 | title = u'City', |
---|
55 | required = True, |
---|
56 | ) |
---|
57 | vip = schema.Bool( |
---|
58 | title = u'Celebrity', |
---|
59 | default = False, |
---|
60 | required = True, |
---|
61 | ) |
---|
62 | birthday = schema.Date( |
---|
63 | title = u'Birthday', |
---|
64 | default = None, |
---|
65 | ) |
---|
66 | @invariant |
---|
67 | def kevinIsYoung(contact): |
---|
68 | if contact.age > 16 and contact.name == 'Kevin': |
---|
69 | raise Invalid('Kevins are age 16 or below.') |
---|
70 | |
---|
71 | class Contact(object): |
---|
72 | implements(IContact) |
---|
73 | name = None |
---|
74 | age = None |
---|
75 | Contact = attrs_to_fields(Contact) |
---|
76 | |
---|
77 | form_fields_select = form.Fields(IContact).select('name', 'vip') |
---|
78 | form_fields_omit = form.Fields(IContact).omit('name', 'vip') |
---|
79 | |
---|
80 | class ConverterTests(FunctionalTestCase): |
---|
81 | |
---|
82 | layer = FunctionalLayer |
---|
83 | |
---|
84 | def setUp(self): |
---|
85 | super(ConverterTests, self).setUp() |
---|
86 | |
---|
87 | # Setup a sample site for each test |
---|
88 | app = University() |
---|
89 | self.dc_root = tempfile.mkdtemp() |
---|
90 | app['datacenter'].setStoragePath(self.dc_root) |
---|
91 | |
---|
92 | # Prepopulate the ZODB... |
---|
93 | self.getRootFolder()['app'] = app |
---|
94 | self.app = self.getRootFolder()['app'] |
---|
95 | |
---|
96 | self.workdir = tempfile.mkdtemp() |
---|
97 | return |
---|
98 | |
---|
99 | def tearDown(self): |
---|
100 | super(ConverterTests, self).tearDown() |
---|
101 | shutil.rmtree(self.workdir) |
---|
102 | shutil.rmtree(self.dc_root) |
---|
103 | clearSite() |
---|
104 | return |
---|
105 | |
---|
106 | def test_valid_data(self): |
---|
107 | contact = Contact() |
---|
108 | contact.age = 33 |
---|
109 | input_data = dict(name='Rudi', age='99') |
---|
110 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
111 | err, inv_err, new_contact = converter.applyRowData( |
---|
112 | input_data, contact) |
---|
113 | assert new_contact is contact |
---|
114 | assert contact.name == 'Rudi' |
---|
115 | assert contact.age == 99 |
---|
116 | return |
---|
117 | |
---|
118 | def test_bool(self): |
---|
119 | contact1 = Contact() |
---|
120 | contact2 = Contact() |
---|
121 | input_data1 = dict(vip='on') |
---|
122 | input_data2 = dict(vip='') |
---|
123 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
124 | err1, inv_err1, new_contact1 = converter.applyRowData( |
---|
125 | input_data1, contact1) |
---|
126 | err2, inv_err2, new_contact2 = converter.applyRowData( |
---|
127 | input_data2, contact2) |
---|
128 | assert contact1.vip is True |
---|
129 | assert contact2.vip is False |
---|
130 | |
---|
131 | def test_int(self): |
---|
132 | contact = Contact() |
---|
133 | input_data = dict(age='99') |
---|
134 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
135 | err, inv_err, new_contact = converter.applyRowData( |
---|
136 | input_data, contact) |
---|
137 | assert contact.age == 99 |
---|
138 | return |
---|
139 | |
---|
140 | def test_int_invalid(self): |
---|
141 | contact = Contact() |
---|
142 | input_data = dict(age='sweet sixteen') |
---|
143 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
144 | err, inv_err, new_contact = converter.applyRowData( |
---|
145 | input_data, contact) |
---|
146 | self.assertEqual(err, [('age', u'Invalid integer data')]) |
---|
147 | return |
---|
148 | |
---|
149 | def test_textline(self): |
---|
150 | contact = Contact() |
---|
151 | input_data = dict(name='Rudi') |
---|
152 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
153 | err, inv_err, new_contact = converter.applyRowData( |
---|
154 | input_data, contact) |
---|
155 | self.assertEqual(contact.name, u'Rudi') |
---|
156 | assert isinstance(contact.name, unicode) |
---|
157 | return |
---|
158 | |
---|
159 | def test_invariant(self): |
---|
160 | contact = Contact() |
---|
161 | input_data = dict(name='Kevin', age='22') |
---|
162 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
163 | err, inv_err, new_contact = converter.applyRowData( |
---|
164 | input_data, contact) |
---|
165 | self.assertEqual(inv_err, ['Kevins are age 16 or below.']) |
---|
166 | return |
---|
167 | |
---|
168 | def test_date(self): |
---|
169 | contact = Contact() |
---|
170 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
171 | err, inv_err, new_contact = converter.applyRowData( |
---|
172 | dict(birthday='1945/12/23'), contact) |
---|
173 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
174 | assert isinstance(contact.birthday, datetime.date) |
---|
175 | |
---|
176 | err, inv_err, new_contact = converter.applyRowData( |
---|
177 | dict(birthday='1945/23/12'), contact) |
---|
178 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
179 | |
---|
180 | err, inv_err, new_contact = converter.applyRowData( |
---|
181 | dict(birthday='23/12/1945'), contact) |
---|
182 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
183 | |
---|
184 | err, inv_err, new_contact = converter.applyRowData( |
---|
185 | dict(birthday='23.12.1945'), contact) |
---|
186 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
187 | |
---|
188 | err, inv_err, new_contact = converter.applyRowData( |
---|
189 | dict(birthday='23-12-1945'), contact) |
---|
190 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
191 | return |
---|
192 | |
---|
193 | def test_date_invalid(self): |
---|
194 | contact = Contact() |
---|
195 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
196 | err, inv_err, new_contact = converter.applyRowData( |
---|
197 | dict(birthday='not-a-date'), contact) |
---|
198 | self.assertEqual(err, [('birthday', u'Invalid datetime data')]) |
---|
199 | |
---|
200 | def test_inject_formfields_select(self): |
---|
201 | # We can use our own formfields and select only a subset of fields |
---|
202 | contact = Contact() |
---|
203 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
204 | input_data = dict(name='Bruno', age='99', vip='on') |
---|
205 | err, inv_err, new_contact = converter.applyRowData( |
---|
206 | input_data, contact, form_fields=form_fields_select) |
---|
207 | self.assertEqual(contact.name, 'Bruno') |
---|
208 | self.assertEqual(contact.age, 23) |
---|
209 | self.assertEqual(contact.vip, True) |
---|
210 | return |
---|
211 | |
---|
212 | def test_inject_formfields_omit(self): |
---|
213 | # We can use our own formfields and omit some fields |
---|
214 | contact = Contact() |
---|
215 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
216 | input_data = dict(name='Bruno', age='99', vip='on') |
---|
217 | err, inv_err, new_contact = converter.applyRowData( |
---|
218 | input_data, contact, form_fields=form_fields_omit) |
---|
219 | self.assertEqual(contact.name, 'Manfred') |
---|
220 | self.assertEqual(contact.age, 99) |
---|
221 | self.assertEqual(contact.vip, False) |
---|
222 | return |
---|
223 | |
---|
224 | def test_suite(): |
---|
225 | suite = unittest.TestSuite() |
---|
226 | for testcase in [ |
---|
227 | ConverterTests, |
---|
228 | ]: |
---|
229 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
230 | testcase |
---|
231 | ) |
---|
232 | ) |
---|
233 | return suite |
---|
234 | |
---|