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

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

Copy over valid_from and valid_to from products when creating contract objects.

Fix ContractProcessorBase?. Do really allow to import tc_dict, valid_from, title and valid_to.

  • Property svn:keywords set to Id
File size: 12.3 KB
Line 
1## $Id: test_export.py 12633 2015-02-27 17:39: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"""
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,comment,contract_category,contract_id,document_object,'
258            'history,last_product_id,'
259            'product_object,product_options,state,tc_dict,title,user_id,'
260            'valid_from,valid_to\r\n'
261
262            'SampleContract,,sample,CON1,,[],,,[],,{},,,,\r\n'
263            )
264        return
265
266    def test_export(self):
267        # we can really export customer contracts.
268        # set values we can expect in export file
269        self.setup_customer(self.customer)
270        exporter = SampleContractExporter()
271        exporter.export([self.contract], self.outfile)
272        result = open(self.outfile, 'rb').read()
273        self.assertMatches(
274            'class_name,comment,contract_category,contract_id,document_object,'
275            'history,last_product_id,'
276            'product_object,product_options,state,tc_dict,title,user_id,'
277            'valid_from,valid_to\r\n'
278
279            'SampleContract,,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
280            'Contract created by system\'],,'
281            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
282            'created,{\'en\': u\'Hello world\'},,A111111,'
283            '2014-02-04#,2014-12-04#\r\n',
284            result
285            )
286        return
287
288    def test_export_all(self):
289        # we can really export all contracts
290        # set values we can expect in export file
291        self.setup_customer(self.customer)
292        exporter = SampleContractExporter()
293        exporter.export_all(self.app, self.outfile)
294        result = open(self.outfile, 'rb').read()
295        self.assertMatches(
296            'class_name,comment,contract_category,contract_id,document_object,'
297            'history,last_product_id,'
298            'product_object,product_options,state,tc_dict,title,user_id,'
299            'valid_from,valid_to\r\n'
300
301            'SampleContract,,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
302            'Contract created by system\'],,'
303            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
304            'created,{\'en\': u\'Hello world\'},,A111111,'
305            '2014-02-04#,2014-12-04#\r\n',
306            result
307            )
308        return
309
310    def test_export_contract(self):
311        # we can really export all contracts of a certain customer
312        # set values we can expect in export file
313        self.setup_customer(self.customer)
314        exporter = SampleContractExporter()
315        exporter.export_customer(self.customer, self.outfile)
316        result = open(self.outfile, 'rb').read()
317        self.assertMatches(
318            'class_name,comment,contract_category,contract_id,document_object,'
319            'history,last_product_id,'
320            'product_object,product_options,state,tc_dict,title,user_id,'
321            'valid_from,valid_to\r\n'
322
323            'SampleContract,,sample,CON1,DOC1,[u\'2014-12-04 12:10:46 UTC - '
324            'Contract created by system\'],,'
325            'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",'
326            'created,{\'en\': u\'Hello world\'},,A111111,'
327            '2014-02-04#,2014-12-04#\r\n',
328            result
329            )
330        return
331
Note: See TracBrowser for help on using the repository browser.