1 | ## $Id: test_browser.py 12335 2014-12-29 06:52:30Z 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 | Document browser and functional tests. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import grok |
---|
23 | import datetime |
---|
24 | from cStringIO import StringIO |
---|
25 | from zope.component import queryUtility, getUtility, createObject |
---|
26 | from zope.event import notify |
---|
27 | from zope.interface.verify import verifyObject, verifyClass |
---|
28 | from zope.testbrowser.testing import Browser |
---|
29 | from hurry.workflow.interfaces import IWorkflowState |
---|
30 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
31 | from ikobacustom.skeleton.customers.export import ( |
---|
32 | SkeletonCustomerExporter, |
---|
33 | SkeletonCustomerDocumentExporter, |
---|
34 | SkeletonContractExporter) |
---|
35 | from ikobacustom.skeleton.customers.batching import ( |
---|
36 | SkeletonCustomerProcessor, |
---|
37 | SkeletonCustomerDocumentProcessor, |
---|
38 | SkeletonContractProcessor) |
---|
39 | from ikobacustom.skeleton.testing import FunctionalLayer |
---|
40 | |
---|
41 | |
---|
42 | class CustomerImportExportTest(CustomerImportExportSetup): |
---|
43 | |
---|
44 | layer = FunctionalLayer |
---|
45 | |
---|
46 | def setup_for_export(self): |
---|
47 | customer = createObject(u'waeup.Customer') |
---|
48 | customer.firstname = u'Beate' |
---|
49 | customer.lastname = u'Mueller' |
---|
50 | customer.reg_number = u'123' |
---|
51 | customer.sex = u'f' |
---|
52 | IWorkflowState(customer).setState('started') |
---|
53 | self.app['customers'].addCustomer(customer) |
---|
54 | document = createObject(u'waeup.SkeletonCustomerDocument') |
---|
55 | document.title = u'My first document' |
---|
56 | customer['documents'].addDocument(document) |
---|
57 | contract = createObject(u'waeup.SkeletonContract') |
---|
58 | contract .title = u'My first contract' |
---|
59 | customer['contracts'].addContract(contract) |
---|
60 | self.customer = customer |
---|
61 | self.document = document |
---|
62 | self.contract = contract |
---|
63 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
64 | return |
---|
65 | |
---|
66 | def test_export_reimport_customers(self): |
---|
67 | # we can export all customers in a portal |
---|
68 | # set values we can expect in export file |
---|
69 | self.setup_for_export() |
---|
70 | exporter = SkeletonCustomerExporter() |
---|
71 | exporter.export_all(self.app, self.outfile) |
---|
72 | result = open(self.outfile, 'rb').read() |
---|
73 | self.assertEqual(result, |
---|
74 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
75 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
76 | 'K1000000,,Beate,Mueller,,,123,f,0,,,started,[]\r\n') |
---|
77 | # We can reimport the file ... |
---|
78 | processor = SkeletonCustomerProcessor() |
---|
79 | result = processor.doImport( |
---|
80 | self.outfile, |
---|
81 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
82 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
83 | mode='create') |
---|
84 | num, num_fail, finished_path, failed_path = result |
---|
85 | self.assertEqual(num_fail,1) |
---|
86 | # ... if we remove the original customer. |
---|
87 | del self.app['customers']['K1000000'] |
---|
88 | result = processor.doImport( |
---|
89 | self.outfile, |
---|
90 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
91 | 'reg_number','sex','suspended','suspended_comment','password','state'], |
---|
92 | mode='create') |
---|
93 | num_succ, num_fail, finished_path, failed_path = result |
---|
94 | self.assertEqual(num_fail,0) |
---|
95 | # We can import the same file in update mode if we ignore the reg_number. |
---|
96 | result = processor.doImport( |
---|
97 | self.outfile, |
---|
98 | ['customer_id','email','firstname','lastname','middlename','phone', |
---|
99 | 'xx_reg_number','sex','suspended','suspended_comment','password','state'], |
---|
100 | mode='update') |
---|
101 | num_succ, num_fail, finished_path, failed_path = result |
---|
102 | self.assertEqual(num_succ,1) |
---|
103 | self.assertEqual(num_fail,0) |
---|
104 | return |
---|
105 | |
---|
106 | def test_export_reimport_documents(self): |
---|
107 | # we can export all documents in a portal |
---|
108 | # set values we can expect in export file |
---|
109 | self.setup_for_export() |
---|
110 | exporter = SkeletonCustomerDocumentExporter() |
---|
111 | exporter.export_all(self.app, self.outfile) |
---|
112 | result = open(self.outfile, 'rb').read() |
---|
113 | self.assertMatches(result, |
---|
114 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
115 | 'SkeletonCustomerDocument,%s,' |
---|
116 | '[u\'2014-12-21 17:00:36 WAT - Document created by system\'],' |
---|
117 | 'created,My first document,K1000000\r\n' |
---|
118 | % self.document.document_id) |
---|
119 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
120 | processor = SkeletonCustomerDocumentProcessor() |
---|
121 | open(self.outfile, 'wb').write( |
---|
122 | 'customer_id,class_name,document_id,state,title\r\n' |
---|
123 | 'K1000000,SkeletonCustomerDocument,%s,started,My first title\r\n' |
---|
124 | % self.document.document_id) |
---|
125 | result = processor.doImport( |
---|
126 | self.outfile, |
---|
127 | ['customer_id','class_name','document_id','state','title'], |
---|
128 | mode='create') |
---|
129 | num, num_fail, finished_path, failed_path = result |
---|
130 | # The object exists. |
---|
131 | self.assertEqual(num_fail,1) |
---|
132 | # We remove the original document. |
---|
133 | del self.customer['documents'][self.document.document_id] |
---|
134 | result = processor.doImport( |
---|
135 | self.outfile, |
---|
136 | ['customer_id','class_name','document_id','state','title'], |
---|
137 | mode='create') |
---|
138 | num_succ, num_fail, finished_path, failed_path = result |
---|
139 | self.assertEqual(num_fail,0) |
---|
140 | # We can import the same file in update mode. |
---|
141 | result = processor.doImport( |
---|
142 | self.outfile, |
---|
143 | ['customer_id','class_name','document_id','state','title'], |
---|
144 | mode='update') |
---|
145 | num_succ, num_fail, finished_path, failed_path = result |
---|
146 | self.assertEqual(num_succ,1) |
---|
147 | self.assertEqual(num_fail,0) |
---|
148 | return |
---|
149 | |
---|
150 | def test_export_reimport_contracts(self): |
---|
151 | # we can export all contracts in a portal |
---|
152 | # set values we can expect in export file |
---|
153 | self.setup_for_export() |
---|
154 | exporter = SkeletonContractExporter() |
---|
155 | exporter.export_all(self.app, self.outfile) |
---|
156 | result = open(self.outfile, 'rb').read() |
---|
157 | self.assertMatches(result, |
---|
158 | 'class_name,contract_category,contract_id,document_object,' |
---|
159 | 'history,last_product_id,product_object,product_options,' |
---|
160 | 'state,title,user_id\r\n' |
---|
161 | 'SkeletonContract,sample,%s,,' |
---|
162 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
163 | ',,,[],created,My first contract,K1000000\r\n' |
---|
164 | % self.contract.contract_id) |
---|
165 | # We can reimport the file if we change the header (user_id -> customer_id) |
---|
166 | processor = SkeletonContractProcessor() |
---|
167 | open(self.outfile, 'wb').write( |
---|
168 | 'class_name,contract_category,contract_id,document_object,' |
---|
169 | 'history,last_product_id,product_object,product_options,' |
---|
170 | 'state,title,user_id\r\n' |
---|
171 | 'SkeletonContract,sample,%s,,' |
---|
172 | '[u\'2014-12-21 22:26:00 WAT - Contract created by system\']' |
---|
173 | ',,,[],created,My first contract,K1000000\r\n' |
---|
174 | % self.contract.contract_id) |
---|
175 | result = processor.doImport( |
---|
176 | self.outfile, |
---|
177 | ['class_name','contract_category','contract_id','document_object', |
---|
178 | 'history','last_product_id','product_object','product_options', |
---|
179 | 'state','title','customer_id'], |
---|
180 | mode='create') |
---|
181 | num, num_fail, finished_path, failed_path = result |
---|
182 | # The object exists. |
---|
183 | self.assertEqual(num_fail,1) |
---|
184 | # We remove the original contract. |
---|
185 | del self.customer['contracts'][self.contract.contract_id] |
---|
186 | result = processor.doImport( |
---|
187 | self.outfile, |
---|
188 | ['class_name','contract_category','contract_id','document_object', |
---|
189 | 'history','last_product_id','product_object','product_options', |
---|
190 | 'state','title','customer_id'], |
---|
191 | mode='create') |
---|
192 | num_succ, num_fail, finished_path, failed_path = result |
---|
193 | self.assertEqual(num_fail,0) |
---|
194 | # We can import the same file in update mode. |
---|
195 | result = processor.doImport( |
---|
196 | self.outfile, |
---|
197 | ['class_name','contract_category','contract_id','document_object', |
---|
198 | 'history','last_product_id','product_object','product_options', |
---|
199 | 'state','title','customer_id'], |
---|
200 | mode='update') |
---|
201 | num_succ, num_fail, finished_path, failed_path = result |
---|
202 | self.assertEqual(num_succ,1) |
---|
203 | self.assertEqual(num_fail,0) |
---|
204 | return |
---|