source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_export.py @ 12342

Last change on this file since 12342 was 12342, checked in by Henrik Bettermann, 10 years ago

Fix currency keys.

  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1## $Id: test_export.py 12342 2014-12-30 10:19:47Z 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"""
19Test the customer exporter.
20"""
21
22import os
23import grok
24import datetime
25from cStringIO import StringIO
26from zope.component import queryUtility, getUtility
27from zope.event import notify
28from zope.interface.verify import verifyObject, verifyClass
29from waeup.ikoba.interfaces import (
30    ICSVExporter, IExtFileStore, IFileStoreNameChooser)
31from waeup.ikoba.customers.catalog import CustomersQuery
32from waeup.ikoba.customers.export import (
33    CustomerExporter, CustomerSampleDocumentExporter, SampleContractExporter,
34    get_customers)
35from waeup.ikoba.customers.interfaces import ICSVCustomerExporter
36from waeup.ikoba.customers.customer import Customer
37from waeup.ikoba.customers.documents import CustomerSampleDocument
38from waeup.ikoba.customers.contracts import SampleContract
39from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
40from waeup.ikoba.testing import FunctionalLayer
41
42curr_year = datetime.datetime.now().year
43year_range = range(curr_year - 9, curr_year + 1)
44year_range_str = ','.join([str(i) for i in year_range])
45
46class 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
63class 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 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
133class CustomerSampleDocumentExporterTest(CustomerImportExportSetup):
134
135    layer = FunctionalLayer
136
137    def setUp(self):
138        super(CustomerSampleDocumentExporterTest, self).setUp()
139        self.setup_for_export()
140        return
141
142    def test_ifaces(self):
143        # make sure we fullfill interface contracts
144        obj = CustomerSampleDocumentExporter()
145        verifyObject(ICSVCustomerExporter, obj)
146        verifyClass(ICSVCustomerExporter, CustomerSampleDocumentExporter)
147        return
148
149    def test_get_as_utility(self):
150        # we can get a documents exporter as utility
151        result = queryUtility(ICSVExporter, name="customersampledocuments")
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        document.document_id = u'DOC1'
159        exporter = CustomerSampleDocumentExporter()
160        exporter.export([document], self.outfile)
161        result = open(self.outfile, 'rb').read()
162        self.assertEqual(
163            result,
164            'class_name,document_id,history,state,title,user_id\r\n'
165            'CustomerSampleDocument,DOC1,[],,,\r\n'
166            )
167        return
168
169    def test_export(self):
170        # we can really export customer documents.
171        # set values we can expect in export file
172        self.setup_customer(self.customer)
173        document = self.customer['documents']['DOC1']
174        exporter = CustomerSampleDocumentExporter()
175        exporter.export([document], self.outfile)
176        result = open(self.outfile, 'rb').read()
177        self.assertTrue(
178            'class_name,document_id,history,state,title,user_id\r\n'
179            in result
180            )
181        self.assertMatches(
182            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
183            'Document created by system\'],'
184            'verified,My Document,A111111...',
185            result
186            )
187        return
188
189    def test_export_all(self):
190        # we can really export all documents
191        # set values we can expect in export file
192        self.setup_customer(self.customer)
193        exporter = CustomerSampleDocumentExporter()
194        exporter.export_all(self.app, self.outfile)
195        result = open(self.outfile, 'rb').read()
196        self.assertTrue(
197            'class_name,document_id,history,state,title,user_id\r\n'
198            in result)
199        self.assertMatches(
200            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
201            'Document created by system\'],'
202            'verified,My Document,A111111...',
203            result
204            )
205        return
206
207    def test_export_customer_document(self):
208        # we can really export all documents of a certain customer
209        # set values we can expect in export file
210        self.setup_customer(self.customer)
211        exporter = CustomerSampleDocumentExporter()
212        exporter.export_customer(self.customer, self.outfile)
213        result = open(self.outfile, 'rb').read()
214        self.assertTrue(
215            'class_name,document_id,history,state,title,user_id\r\n'
216            in result)
217        self.assertMatches(
218            '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - '
219            'Document created by system\'],'
220            'verified,My Document,A111111...',
221            result
222            )
223        return
224
225
226class SampleContractExporterTest(CustomerImportExportSetup):
227
228    layer = FunctionalLayer
229
230    def setUp(self):
231        super(SampleContractExporterTest, self).setUp()
232        self.setup_for_export()
233        return
234
235    def test_ifaces(self):
236        # make sure we fullfill interface contracts
237        obj = SampleContractExporter()
238        verifyObject(ICSVCustomerExporter, obj)
239        verifyClass(ICSVCustomerExporter, SampleContractExporter)
240        return
241
242    def test_get_as_utility(self):
243        # we can get a contracts exporter as utility
244        result = queryUtility(ICSVExporter, name="samplecontracts")
245        self.assertTrue(result is not None)
246        return
247
248    def test_export_empty(self):
249        # we can export a nearly empty contract
250        contract = SampleContract()
251        contract.contract_id = u'CON1'
252        exporter = SampleContractExporter()
253        exporter.export([contract], self.outfile)
254        result = open(self.outfile, 'rb').read()
255        self.assertEqual(
256            result,
257            'class_name,contract_category,contract_id,document_object,'
258            'history,last_product_id,'
259            'product_object,product_options,state,title,user_id\r\n'
260
261            'SampleContract,sample,CON1,,[],,,[],,,\r\n'
262            )
263        return
264
265    def test_export(self):
266        # we can really export customer contracts.
267        # set values we can expect in export file
268        self.setup_customer(self.customer)
269        exporter = SampleContractExporter()
270        exporter.export([self.contract], self.outfile)
271        result = open(self.outfile, 'rb').read()
272        self.assertMatches(
273            'class_name,contract_category,contract_id,document_object,'
274            'history,last_product_id,'
275            'product_object,product_options,state,title,user_id\r\n'
276
277            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
278            'Contract created by system\'],,'
279            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
280            'created,Our Sample Product,A111111\r\n',
281            result
282            )
283        return
284
285    def test_export_all(self):
286        # we can really export all contracts
287        # set values we can expect in export file
288        self.setup_customer(self.customer)
289        exporter = SampleContractExporter()
290        exporter.export_all(self.app, self.outfile)
291        result = open(self.outfile, 'rb').read()
292        self.assertMatches(
293            'class_name,contract_category,contract_id,document_object,'
294            'history,last_product_id,'
295            'product_object,product_options,state,title,user_id\r\n'
296
297            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
298            'Contract created by system\'],,'
299            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
300            'created,Our Sample Product,A111111\r\n',
301            result
302            )
303        return
304
305    def test_export_contract(self):
306        # we can really export all contracts of a certain customer
307        # set values we can expect in export file
308        self.setup_customer(self.customer)
309        exporter = SampleContractExporter()
310        exporter.export_customer(self.customer, self.outfile)
311        result = open(self.outfile, 'rb').read()
312        self.assertMatches(
313            'class_name,contract_category,contract_id,document_object,'
314            'history,last_product_id,'
315            'product_object,product_options,state,title,user_id\r\n'
316
317            'SampleContract,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
318            'Contract created by system\'],,'
319            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
320            'created,Our Sample Product,A111111\r\n',
321            result
322            )
323        return
324
Note: See TracBrowser for help on using the repository browser.