1 | ## $Id: test_export.py 12146 2014-12-04 18:11:46Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | Test the customer exporter. |
---|
20 | """ |
---|
21 | |
---|
22 | import os |
---|
23 | import grok |
---|
24 | import datetime |
---|
25 | from cStringIO import StringIO |
---|
26 | from zope.component import queryUtility, getUtility |
---|
27 | from zope.event import notify |
---|
28 | from zope.interface.verify import verifyObject, verifyClass |
---|
29 | from waeup.ikoba.interfaces import ( |
---|
30 | ICSVExporter, IExtFileStore, IFileStoreNameChooser) |
---|
31 | from waeup.ikoba.customers.catalog import CustomersQuery |
---|
32 | from waeup.ikoba.customers.export import ( |
---|
33 | CustomerExporter, CustomerDocumentExporter, ContractExporter, |
---|
34 | get_customers) |
---|
35 | from waeup.ikoba.customers.interfaces import ICSVCustomerExporter |
---|
36 | from waeup.ikoba.customers.customer import Customer |
---|
37 | from waeup.ikoba.customers.documents import CustomerSampleDocument |
---|
38 | from waeup.ikoba.customers.contracts import SampleContract |
---|
39 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
40 | from waeup.ikoba.testing import FunctionalLayer |
---|
41 | |
---|
42 | curr_year = datetime.datetime.now().year |
---|
43 | year_range = range(curr_year - 9, curr_year + 1) |
---|
44 | year_range_str = ','.join([str(i) for i in year_range]) |
---|
45 | |
---|
46 | class ExportHelperTests(CustomerImportExportSetup): |
---|
47 | layer = FunctionalLayer |
---|
48 | def setUp(self): |
---|
49 | super(ExportHelperTests, self).setUp() |
---|
50 | customer = Customer() |
---|
51 | self.app['customers'].addCustomer(customer) |
---|
52 | customer = self.setup_customer(customer) |
---|
53 | notify(grok.ObjectModifiedEvent(customer)) |
---|
54 | self.customer = self.app['customers'][customer.customer_id] |
---|
55 | return |
---|
56 | |
---|
57 | def test_get_customers_plain(self): |
---|
58 | # without a filter we get all customers |
---|
59 | result = get_customers(self.app) |
---|
60 | self.assertEqual(len(list(result)), 1) |
---|
61 | return |
---|
62 | |
---|
63 | class CustomerExporterTest(CustomerImportExportSetup): |
---|
64 | |
---|
65 | layer = FunctionalLayer |
---|
66 | |
---|
67 | def setUp(self): |
---|
68 | super(CustomerExporterTest, self).setUp() |
---|
69 | self.setup_for_export() |
---|
70 | return |
---|
71 | |
---|
72 | def test_ifaces(self): |
---|
73 | # make sure we fullfill interface contracts |
---|
74 | obj = CustomerExporter() |
---|
75 | verifyObject(ICSVCustomerExporter, obj) |
---|
76 | verifyClass(ICSVCustomerExporter, CustomerExporter) |
---|
77 | return |
---|
78 | |
---|
79 | def test_get_as_utility(self): |
---|
80 | # we can get an customer exporter as utility |
---|
81 | result = queryUtility(ICSVExporter, name="customers") |
---|
82 | self.assertTrue(result is not None) |
---|
83 | return |
---|
84 | |
---|
85 | def test_export(self): |
---|
86 | # we can really export customers |
---|
87 | # set values we can expect in export file |
---|
88 | self.setup_customer(self.customer) |
---|
89 | exporter = CustomerExporter() |
---|
90 | exporter.export([self.customer], self.outfile) |
---|
91 | result = open(self.outfile, 'rb').read() |
---|
92 | self.assertMatches( |
---|
93 | 'customer_id,email,firstname,lastname,middlename,phone,reg_number,' |
---|
94 | 'sex,suspended,suspended_comment,password,state,history\r\n' |
---|
95 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,123,,0,,,' |
---|
96 | 'created,[u\'2014-12-04 11:25:35 UTC - Customer record created ' |
---|
97 | 'by system\']\r\n', |
---|
98 | result |
---|
99 | ) |
---|
100 | return |
---|
101 | |
---|
102 | def test_export_all(self): |
---|
103 | # we can really export customers |
---|
104 | # set values we can expect in export file |
---|
105 | self.setup_customer(self.customer) |
---|
106 | exporter = CustomerExporter() |
---|
107 | exporter.export_all(self.app, self.outfile) |
---|
108 | result = open(self.outfile, 'rb').read() |
---|
109 | self.assertTrue( |
---|
110 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
111 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
112 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
113 | '123,,0,,,created' |
---|
114 | in result |
---|
115 | ) |
---|
116 | return |
---|
117 | |
---|
118 | def test_export_customer(self): |
---|
119 | # we can export a single customer |
---|
120 | self.setup_customer(self.customer) |
---|
121 | exporter = CustomerExporter() |
---|
122 | exporter.export_customer(self.customer, self.outfile) |
---|
123 | result = open(self.outfile, 'rb').read() |
---|
124 | self.assertTrue( |
---|
125 | 'customer_id,email,firstname,lastname,middlename,phone,reg_number,' |
---|
126 | 'sex,suspended,suspended_comment,password,state,history\r\n' |
---|
127 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
128 | '123,,0,,,created,' |
---|
129 | in result |
---|
130 | ) |
---|
131 | return |
---|
132 | |
---|
133 | class CustomerDocumentExporterTest(CustomerImportExportSetup): |
---|
134 | |
---|
135 | layer = FunctionalLayer |
---|
136 | |
---|
137 | def setUp(self): |
---|
138 | super(CustomerDocumentExporterTest, self).setUp() |
---|
139 | self.setup_for_export() |
---|
140 | return |
---|
141 | |
---|
142 | def test_ifaces(self): |
---|
143 | # make sure we fullfill interface contracts |
---|
144 | obj = CustomerDocumentExporter() |
---|
145 | verifyObject(ICSVCustomerExporter, obj) |
---|
146 | verifyClass(ICSVCustomerExporter, CustomerDocumentExporter) |
---|
147 | return |
---|
148 | |
---|
149 | def test_get_as_utility(self): |
---|
150 | # we can get a documents exporter as utility |
---|
151 | result = queryUtility(ICSVExporter, name="customerdocuments") |
---|
152 | self.assertTrue(result is not None) |
---|
153 | return |
---|
154 | |
---|
155 | def test_export_empty(self): |
---|
156 | # we can export a nearly empty document |
---|
157 | document = CustomerSampleDocument() |
---|
158 | exporter = CustomerDocumentExporter() |
---|
159 | exporter.export([document], self.outfile) |
---|
160 | result = open(self.outfile, 'rb').read() |
---|
161 | self.assertEqual( |
---|
162 | result, |
---|
163 | 'class_name,document_id,history,last_transition_date,state,title,user_id\r\n' |
---|
164 | 'CustomerSampleDocument,d101,[],,,,\r\n' |
---|
165 | ) |
---|
166 | return |
---|
167 | |
---|
168 | def test_export(self): |
---|
169 | # we can really export customer documents. |
---|
170 | # set values we can expect in export file |
---|
171 | self.setup_customer(self.customer) |
---|
172 | document = self.customer['documents']['d101'] |
---|
173 | exporter = CustomerDocumentExporter() |
---|
174 | exporter.export([document], self.outfile) |
---|
175 | result = open(self.outfile, 'rb').read() |
---|
176 | self.assertTrue( |
---|
177 | 'class_name,document_id,history,last_transition_date,state,title,user_id\r\n' |
---|
178 | in result |
---|
179 | ) |
---|
180 | self.assertMatches( |
---|
181 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
182 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
183 | 'verified,My Document,A111111...', |
---|
184 | result |
---|
185 | ) |
---|
186 | return |
---|
187 | |
---|
188 | def test_export_all(self): |
---|
189 | # we can really export all documents |
---|
190 | # set values we can expect in export file |
---|
191 | self.setup_customer(self.customer) |
---|
192 | exporter = CustomerDocumentExporter() |
---|
193 | exporter.export_all(self.app, self.outfile) |
---|
194 | result = open(self.outfile, 'rb').read() |
---|
195 | self.assertTrue( |
---|
196 | 'class_name,document_id,history,last_transition_date,state,title,user_id\r\n' |
---|
197 | in result) |
---|
198 | self.assertMatches( |
---|
199 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
200 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
201 | 'verified,My Document,A111111...', |
---|
202 | result |
---|
203 | ) |
---|
204 | return |
---|
205 | |
---|
206 | def test_export_customer_document(self): |
---|
207 | # we can really export all documents of a certain customer |
---|
208 | # set values we can expect in export file |
---|
209 | self.setup_customer(self.customer) |
---|
210 | exporter = CustomerDocumentExporter() |
---|
211 | exporter.export_customer(self.customer, self.outfile) |
---|
212 | result = open(self.outfile, 'rb').read() |
---|
213 | self.assertTrue( |
---|
214 | 'class_name,document_id,history,last_transition_date,state,title,user_id\r\n' |
---|
215 | in result) |
---|
216 | self.assertMatches( |
---|
217 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
218 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
219 | 'verified,My Document,A111111...', |
---|
220 | result |
---|
221 | ) |
---|
222 | return |
---|
223 | |
---|
224 | |
---|
225 | class ContractExporterTest(CustomerImportExportSetup): |
---|
226 | |
---|
227 | layer = FunctionalLayer |
---|
228 | |
---|
229 | def setUp(self): |
---|
230 | super(ContractExporterTest, self).setUp() |
---|
231 | self.setup_for_export() |
---|
232 | return |
---|
233 | |
---|
234 | def test_ifaces(self): |
---|
235 | # make sure we fullfill interface contracts |
---|
236 | obj = ContractExporter() |
---|
237 | verifyObject(ICSVCustomerExporter, obj) |
---|
238 | verifyClass(ICSVCustomerExporter, ContractExporter) |
---|
239 | return |
---|
240 | |
---|
241 | def test_get_as_utility(self): |
---|
242 | # we can get a contracts exporter as utility |
---|
243 | result = queryUtility(ICSVExporter, name="contracts") |
---|
244 | self.assertTrue(result is not None) |
---|
245 | return |
---|
246 | |
---|
247 | def test_export_empty(self): |
---|
248 | # we can export a nearly empty contract |
---|
249 | contract = SampleContract() |
---|
250 | exporter = ContractExporter() |
---|
251 | exporter.export([contract], self.outfile) |
---|
252 | result = open(self.outfile, 'rb').read() |
---|
253 | self.assertEqual( |
---|
254 | result, |
---|
255 | 'class_name,contract_category,contract_id,document_object,' |
---|
256 | 'history,last_product_id,last_transition_date,' |
---|
257 | 'product_object,state,title\r\n' |
---|
258 | |
---|
259 | 'SampleContract,sample,c101,,[],,,,,\r\n' |
---|
260 | ) |
---|
261 | return |
---|
262 | |
---|
263 | def test_export(self): |
---|
264 | # we can really export customer contracts. |
---|
265 | # set values we can expect in export file |
---|
266 | self.setup_customer(self.customer) |
---|
267 | exporter = ContractExporter() |
---|
268 | exporter.export([self.contract], self.outfile) |
---|
269 | result = open(self.outfile, 'rb').read() |
---|
270 | self.assertMatches( |
---|
271 | 'class_name,contract_category,contract_id,document_object,' |
---|
272 | 'history,last_product_id,last_transition_date,' |
---|
273 | 'product_object,state,title\r\n' |
---|
274 | |
---|
275 | 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' |
---|
276 | 'Contract record created by system\'],,' |
---|
277 | '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', |
---|
278 | result |
---|
279 | ) |
---|
280 | return |
---|
281 | |
---|
282 | def test_export_all(self): |
---|
283 | # we can really export all contracts |
---|
284 | # set values we can expect in export file |
---|
285 | self.setup_customer(self.customer) |
---|
286 | exporter = ContractExporter() |
---|
287 | exporter.export_all(self.app, self.outfile) |
---|
288 | result = open(self.outfile, 'rb').read() |
---|
289 | self.assertMatches( |
---|
290 | 'class_name,contract_category,contract_id,document_object,' |
---|
291 | 'history,last_product_id,last_transition_date,' |
---|
292 | 'product_object,state,title\r\n' |
---|
293 | |
---|
294 | 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' |
---|
295 | 'Contract record created by system\'],,' |
---|
296 | '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', |
---|
297 | result |
---|
298 | ) |
---|
299 | return |
---|
300 | |
---|
301 | def test_export_contract(self): |
---|
302 | # we can really export all contracts of a certain customer |
---|
303 | # set values we can expect in export file |
---|
304 | self.setup_customer(self.customer) |
---|
305 | exporter = ContractExporter() |
---|
306 | exporter.export_customer(self.customer, self.outfile) |
---|
307 | result = open(self.outfile, 'rb').read() |
---|
308 | self.assertMatches( |
---|
309 | 'class_name,contract_category,contract_id,document_object,' |
---|
310 | 'history,last_product_id,last_transition_date,' |
---|
311 | 'product_object,state,title\r\n' |
---|
312 | |
---|
313 | 'SampleContract,sample,c101,d101,[u\'2014-12-04 12:10:46 UTC - ' |
---|
314 | 'Contract record created by system\'],,' |
---|
315 | '2014-12-04 12:10:46.333445#,SAM,created,My Contract\r\n', |
---|
316 | result |
---|
317 | ) |
---|
318 | return |
---|
319 | |
---|