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.interface.verify import verifyClass, verifyObject |
---|
33 | |
---|
34 | from waeup.sirp.app import University |
---|
35 | from waeup.sirp.testing import FunctionalLayer, doctestsuite_for_module |
---|
36 | from waeup.sirp.utils.converters import IObjectConverter |
---|
37 | |
---|
38 | from zope import schema |
---|
39 | from zope.interface import Interface, implements, invariant, Invalid |
---|
40 | from waeup.sirp.utils.helpers import attrs_to_fields |
---|
41 | |
---|
42 | class IContact(Interface): |
---|
43 | name = schema.TextLine( |
---|
44 | title = u'Name', |
---|
45 | default = u'Manfred' |
---|
46 | ) |
---|
47 | age = schema.Int( |
---|
48 | title = u'Age', |
---|
49 | default = 23, |
---|
50 | required = True, |
---|
51 | ) |
---|
52 | city = schema.TextLine( |
---|
53 | title = u'City', |
---|
54 | required = True, |
---|
55 | ) |
---|
56 | vip = schema.Bool( |
---|
57 | title = u'Celebrity', |
---|
58 | default = False, |
---|
59 | required = True, |
---|
60 | ) |
---|
61 | birthday = schema.Date( |
---|
62 | title = u'Birthday', |
---|
63 | default = None, |
---|
64 | ) |
---|
65 | @invariant |
---|
66 | def kevinIsYoung(contact): |
---|
67 | if contact.age > 16 and contact.name == 'Kevin': |
---|
68 | raise Invalid('Kevins are age 16 or below.') |
---|
69 | |
---|
70 | class Contact(object): |
---|
71 | implements(IContact) |
---|
72 | name = None |
---|
73 | age = None |
---|
74 | Contact = attrs_to_fields(Contact) |
---|
75 | |
---|
76 | class ConverterTests(FunctionalTestCase): |
---|
77 | |
---|
78 | layer = FunctionalLayer |
---|
79 | |
---|
80 | def setUp(self): |
---|
81 | super(ConverterTests, self).setUp() |
---|
82 | |
---|
83 | # Setup a sample site for each test |
---|
84 | app = University() |
---|
85 | self.dc_root = tempfile.mkdtemp() |
---|
86 | app['datacenter'].setStoragePath(self.dc_root) |
---|
87 | |
---|
88 | # Prepopulate the ZODB... |
---|
89 | self.getRootFolder()['app'] = app |
---|
90 | self.app = self.getRootFolder()['app'] |
---|
91 | |
---|
92 | self.workdir = tempfile.mkdtemp() |
---|
93 | return |
---|
94 | |
---|
95 | def tearDown(self): |
---|
96 | super(ConverterTests, self).tearDown() |
---|
97 | shutil.rmtree(self.workdir) |
---|
98 | shutil.rmtree(self.dc_root) |
---|
99 | clearSite() |
---|
100 | return |
---|
101 | |
---|
102 | def test_valid_data(self): |
---|
103 | contact = Contact() |
---|
104 | contact.age = 33 |
---|
105 | input_data = dict(name='Rudi', age='99') |
---|
106 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
107 | err, inv_err, new_contact = converter.applyRowData( |
---|
108 | input_data, contact) |
---|
109 | assert new_contact is contact |
---|
110 | assert contact.name == 'Rudi' |
---|
111 | assert contact.age == 99 |
---|
112 | return |
---|
113 | |
---|
114 | def test_bool(self): |
---|
115 | contact1 = Contact() |
---|
116 | contact2 = Contact() |
---|
117 | input_data1 = dict(vip='on') |
---|
118 | input_data2 = dict(vip='') |
---|
119 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
120 | err1, inv_err1, new_contact1 = converter.applyRowData( |
---|
121 | input_data1, contact1) |
---|
122 | err2, inv_err2, new_contact2 = converter.applyRowData( |
---|
123 | input_data2, contact2) |
---|
124 | assert contact1.vip is True |
---|
125 | assert contact2.vip is False |
---|
126 | |
---|
127 | def test_int(self): |
---|
128 | contact = Contact() |
---|
129 | input_data = dict(age='99') |
---|
130 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
131 | err, inv_err, new_contact = converter.applyRowData( |
---|
132 | input_data, contact) |
---|
133 | assert contact.age == 99 |
---|
134 | return |
---|
135 | |
---|
136 | def test_int_invalid(self): |
---|
137 | contact = Contact() |
---|
138 | input_data = dict(age='sweet sixteen') |
---|
139 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
140 | err, inv_err, new_contact = converter.applyRowData( |
---|
141 | input_data, contact) |
---|
142 | self.assertEqual(err, [('age', u'Invalid integer data')]) |
---|
143 | return |
---|
144 | |
---|
145 | def test_textline(self): |
---|
146 | contact = Contact() |
---|
147 | input_data = dict(name='Rudi') |
---|
148 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
149 | err, inv_err, new_contact = converter.applyRowData( |
---|
150 | input_data, contact) |
---|
151 | self.assertEqual(contact.name, u'Rudi') |
---|
152 | assert isinstance(contact.name, unicode) |
---|
153 | return |
---|
154 | |
---|
155 | def test_invariant(self): |
---|
156 | contact = Contact() |
---|
157 | input_data = dict(name='Kevin', age='22') |
---|
158 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
159 | err, inv_err, new_contact = converter.applyRowData( |
---|
160 | input_data, contact) |
---|
161 | self.assertEqual(inv_err, ['Kevins are age 16 or below.']) |
---|
162 | return |
---|
163 | |
---|
164 | def test_date(self): |
---|
165 | contact = Contact() |
---|
166 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
167 | err, inv_err, new_contact = converter.applyRowData( |
---|
168 | dict(birthday='1945/12/23'), contact) |
---|
169 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
170 | assert isinstance(contact.birthday, datetime.date) |
---|
171 | |
---|
172 | err, inv_err, new_contact = converter.applyRowData( |
---|
173 | dict(birthday='1945/23/12'), contact) |
---|
174 | assert contact.birthday == datetime.date(1945, 12, 23) |
---|
175 | |
---|
176 | err, inv_err, new_contact = converter.applyRowData( |
---|
177 | dict(birthday='23/12/1945'), 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 | return |
---|
188 | |
---|
189 | def test_date_invalid(self): |
---|
190 | contact = Contact() |
---|
191 | converter = IObjectConverter(IContact) # a converter to IContact |
---|
192 | err, inv_err, new_contact = converter.applyRowData( |
---|
193 | dict(birthday='not-a-date'), contact) |
---|
194 | self.assertEqual(err, [('birthday', u'Invalid datetime data')]) |
---|
195 | |
---|
196 | def test_suite(): |
---|
197 | suite = unittest.TestSuite() |
---|
198 | for testcase in [ |
---|
199 | ConverterTests, |
---|
200 | ]: |
---|
201 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
202 | testcase |
---|
203 | ) |
---|
204 | ) |
---|
205 | return suite |
---|
206 | |
---|