1 | ## $Id: test_export.py 12076 2014-11-28 06:56:38Z 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, get_customers) |
---|
34 | from waeup.ikoba.customers.interfaces import ICSVCustomerExporter |
---|
35 | from waeup.ikoba.customers.customer import Customer |
---|
36 | from waeup.ikoba.customers.documents import CustomerSampleDocument |
---|
37 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
38 | from waeup.ikoba.testing import FunctionalLayer |
---|
39 | |
---|
40 | curr_year = datetime.datetime.now().year |
---|
41 | year_range = range(curr_year - 9, curr_year + 1) |
---|
42 | year_range_str = ','.join([str(i) for i in year_range]) |
---|
43 | |
---|
44 | class ExportHelperTests(CustomerImportExportSetup): |
---|
45 | layer = FunctionalLayer |
---|
46 | def setUp(self): |
---|
47 | super(ExportHelperTests, self).setUp() |
---|
48 | customer = Customer() |
---|
49 | self.app['customers'].addCustomer(customer) |
---|
50 | customer = self.setup_customer(customer) |
---|
51 | notify(grok.ObjectModifiedEvent(customer)) |
---|
52 | self.customer = self.app['customers'][customer.customer_id] |
---|
53 | return |
---|
54 | |
---|
55 | def test_get_customers_plain(self): |
---|
56 | # without a filter we get all customers |
---|
57 | result = get_customers(self.app) |
---|
58 | self.assertEqual(len(list(result)), 1) |
---|
59 | return |
---|
60 | |
---|
61 | class CustomerExporterTest(CustomerImportExportSetup): |
---|
62 | |
---|
63 | layer = FunctionalLayer |
---|
64 | |
---|
65 | def setUp(self): |
---|
66 | super(CustomerExporterTest, self).setUp() |
---|
67 | self.setup_for_export() |
---|
68 | return |
---|
69 | |
---|
70 | def test_ifaces(self): |
---|
71 | # make sure we fullfill interface contracts |
---|
72 | obj = CustomerExporter() |
---|
73 | verifyObject(ICSVCustomerExporter, obj) |
---|
74 | verifyClass(ICSVCustomerExporter, CustomerExporter) |
---|
75 | return |
---|
76 | |
---|
77 | def test_get_as_utility(self): |
---|
78 | # we can get an customer exporter as utility |
---|
79 | result = queryUtility(ICSVExporter, name="customers") |
---|
80 | self.assertTrue(result is not None) |
---|
81 | return |
---|
82 | |
---|
83 | def test_export(self): |
---|
84 | # we can really export customers |
---|
85 | # set values we can expect in export file |
---|
86 | self.setup_customer(self.customer) |
---|
87 | exporter = CustomerExporter() |
---|
88 | exporter.export([self.customer], self.outfile) |
---|
89 | result = open(self.outfile, 'rb').read() |
---|
90 | self.assertTrue( |
---|
91 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
92 | 'reg_number,sex,suspended,suspended_comment,password,state,' |
---|
93 | 'history\r\n' |
---|
94 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
95 | '123,,0,,,created' |
---|
96 | in result |
---|
97 | ) |
---|
98 | return |
---|
99 | |
---|
100 | def test_export_all(self): |
---|
101 | # we can really export customers |
---|
102 | # set values we can expect in export file |
---|
103 | self.setup_customer(self.customer) |
---|
104 | exporter = CustomerExporter() |
---|
105 | exporter.export_all(self.app, self.outfile) |
---|
106 | result = open(self.outfile, 'rb').read() |
---|
107 | self.assertTrue( |
---|
108 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
109 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
110 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
111 | '123,,0,,,created' |
---|
112 | in result |
---|
113 | ) |
---|
114 | return |
---|
115 | |
---|
116 | def test_export_customer(self): |
---|
117 | # we can export a single customer |
---|
118 | self.setup_customer(self.customer) |
---|
119 | exporter = CustomerExporter() |
---|
120 | exporter.export_customer(self.customer, self.outfile) |
---|
121 | result = open(self.outfile, 'rb').read() |
---|
122 | self.assertTrue( |
---|
123 | 'customer_id,email,firstname,lastname,middlename,phone,reg_number,' |
---|
124 | 'sex,suspended,suspended_comment,password,state,history\r\n' |
---|
125 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
126 | '123,,0,,,created,' |
---|
127 | in result |
---|
128 | ) |
---|
129 | return |
---|
130 | |
---|
131 | class CustomerDocumentExporterTest(CustomerImportExportSetup): |
---|
132 | |
---|
133 | layer = FunctionalLayer |
---|
134 | |
---|
135 | def setUp(self): |
---|
136 | super(CustomerDocumentExporterTest, self).setUp() |
---|
137 | self.setup_for_export() |
---|
138 | return |
---|
139 | |
---|
140 | def test_ifaces(self): |
---|
141 | # make sure we fullfill interface contracts |
---|
142 | obj = CustomerDocumentExporter() |
---|
143 | verifyObject(ICSVCustomerExporter, obj) |
---|
144 | verifyClass(ICSVCustomerExporter, CustomerDocumentExporter) |
---|
145 | return |
---|
146 | |
---|
147 | def test_get_as_utility(self): |
---|
148 | # we can get a documents exporter as utility |
---|
149 | result = queryUtility(ICSVExporter, name="customerdocuments") |
---|
150 | self.assertTrue(result is not None) |
---|
151 | return |
---|
152 | |
---|
153 | def test_export_empty(self): |
---|
154 | # we can export a nearly empty document |
---|
155 | document = CustomerSampleDocument() |
---|
156 | exporter = CustomerDocumentExporter() |
---|
157 | exporter.export([document], self.outfile) |
---|
158 | result = open(self.outfile, 'rb').read() |
---|
159 | self.assertEqual( |
---|
160 | result, |
---|
161 | 'class_name,document_id,history,last_transition_date,state,title,customer_id\r\n' |
---|
162 | 'CustomerSampleDocument,d101,[],,,,\r\n' |
---|
163 | ) |
---|
164 | return |
---|
165 | |
---|
166 | def test_export(self): |
---|
167 | # we can really export customer documents. |
---|
168 | # set values we can expect in export file |
---|
169 | self.setup_customer(self.customer) |
---|
170 | document = self.customer['documents']['d101'] |
---|
171 | exporter = CustomerDocumentExporter() |
---|
172 | exporter.export([document], self.outfile) |
---|
173 | result = open(self.outfile, 'rb').read() |
---|
174 | self.assertTrue( |
---|
175 | 'class_name,document_id,history,last_transition_date,state,title,customer_id\r\n' |
---|
176 | in result |
---|
177 | ) |
---|
178 | self.assertMatches( |
---|
179 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
180 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
181 | 'created,My Document,A111111...', |
---|
182 | result |
---|
183 | ) |
---|
184 | return |
---|
185 | |
---|
186 | def test_export_all(self): |
---|
187 | # we can really export all documents |
---|
188 | # set values we can expect in export file |
---|
189 | self.setup_customer(self.customer) |
---|
190 | exporter = CustomerDocumentExporter() |
---|
191 | exporter.export_all(self.app, self.outfile) |
---|
192 | result = open(self.outfile, 'rb').read() |
---|
193 | self.assertTrue( |
---|
194 | 'class_name,document_id,history,last_transition_date,state,title,customer_id\r\n' |
---|
195 | in result) |
---|
196 | self.assertMatches( |
---|
197 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
198 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
199 | 'created,My Document,A111111...', |
---|
200 | result |
---|
201 | ) |
---|
202 | return |
---|
203 | |
---|
204 | def test_export_customer(self): |
---|
205 | # we can really export all documents of a certain customer |
---|
206 | # set values we can expect in export file |
---|
207 | self.setup_customer(self.customer) |
---|
208 | exporter = CustomerDocumentExporter() |
---|
209 | exporter.export_customer(self.customer, self.outfile) |
---|
210 | result = open(self.outfile, 'rb').read() |
---|
211 | self.assertTrue( |
---|
212 | 'class_name,document_id,history,last_transition_date,state,title,customer_id\r\n' |
---|
213 | in result) |
---|
214 | self.assertMatches( |
---|
215 | '...CustomerSampleDocument,d101,[u\'2014-11-25 06:57:24 UTC - ' |
---|
216 | 'Document created by system\'],2014-11-25 06:57:24.990308#,' |
---|
217 | 'created,My Document,A111111...', |
---|
218 | result |
---|
219 | ) |
---|
220 | return |
---|