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 CustomerDocument |
---|
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 = CustomerDocument() |
---|
135 | exporter = CustomerDocumentsExporter() |
---|
136 | exporter.export([document], self.outfile) |
---|
137 | result = open(self.outfile, 'rb').read() |
---|
138 | self.assertEqual( |
---|
139 | result, |
---|
140 | 'doctype,document_id,history,title,customer_id\r\n' |
---|
141 | ',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 | 'doctype,document_id,history,title,customer_id\r\n' |
---|
155 | in result |
---|
156 | ) |
---|
157 | self.assertTrue( |
---|
158 | 'Document created by system\'],My Document,A111111\r\n' |
---|
159 | in result |
---|
160 | ) |
---|
161 | return |
---|
162 | |
---|
163 | def test_export_all(self): |
---|
164 | # we can really export all documents |
---|
165 | # set values we can expect in export file |
---|
166 | self.setup_customer(self.customer) |
---|
167 | exporter = CustomerDocumentsExporter() |
---|
168 | exporter.export_all(self.app, self.outfile) |
---|
169 | result = open(self.outfile, 'rb').read() |
---|
170 | self.assertTrue( |
---|
171 | 'doctype,document_id,history,title,customer_id\r\n' |
---|
172 | in result) |
---|
173 | self.assertTrue( |
---|
174 | ' Document created by system\'],My Document,A111111\r\n' |
---|
175 | in result) |
---|
176 | return |
---|
177 | |
---|
178 | def test_export_customer(self): |
---|
179 | # we can really export all documents of a certain customer |
---|
180 | # set values we can expect in export file |
---|
181 | self.setup_customer(self.customer) |
---|
182 | exporter = CustomerDocumentsExporter() |
---|
183 | exporter.export_customer(self.customer, self.outfile) |
---|
184 | result = open(self.outfile, 'rb').read() |
---|
185 | self.assertTrue( |
---|
186 | 'doctype,document_id,history,title,customer_id\r\n' |
---|
187 | in result) |
---|
188 | self.assertTrue( |
---|
189 | ' Document created by system\'],My Document,A111111\r\n' |
---|
190 | in result) |
---|
191 | return |
---|