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