[12076] | 1 | ## $Id: test_export.py 12663 2015-03-05 07:28:31Z 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 | Test the customer exporter. |
---|
| 20 | """ |
---|
| 21 | |
---|
[11978] | 22 | import os |
---|
| 23 | import grok |
---|
| 24 | import datetime |
---|
| 25 | from cStringIO import StringIO |
---|
| 26 | from zope.component import queryUtility, getUtility |
---|
| 27 | from zope.event import notify |
---|
| 28 | from zope.interface.verify import verifyObject, verifyClass |
---|
| 29 | from waeup.ikoba.interfaces import ( |
---|
| 30 | ICSVExporter, IExtFileStore, IFileStoreNameChooser) |
---|
| 31 | from waeup.ikoba.customers.catalog import CustomersQuery |
---|
| 32 | from waeup.ikoba.customers.export import ( |
---|
[12279] | 33 | CustomerExporter, CustomerSampleDocumentExporter, SampleContractExporter, |
---|
[12143] | 34 | get_customers) |
---|
[11978] | 35 | from waeup.ikoba.customers.interfaces import ICSVCustomerExporter |
---|
| 36 | from waeup.ikoba.customers.customer import Customer |
---|
[12057] | 37 | from waeup.ikoba.customers.documents import CustomerSampleDocument |
---|
[12143] | 38 | from waeup.ikoba.customers.contracts import SampleContract |
---|
[11978] | 39 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
| 40 | from waeup.ikoba.testing import FunctionalLayer |
---|
| 41 | |
---|
| 42 | curr_year = datetime.datetime.now().year |
---|
| 43 | year_range = range(curr_year - 9, curr_year + 1) |
---|
| 44 | year_range_str = ','.join([str(i) for i in year_range]) |
---|
| 45 | |
---|
| 46 | class 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 | |
---|
[12076] | 63 | class CustomerExporterTest(CustomerImportExportSetup): |
---|
[11978] | 64 | |
---|
| 65 | layer = FunctionalLayer |
---|
| 66 | |
---|
| 67 | def setUp(self): |
---|
[12076] | 68 | super(CustomerExporterTest, self).setUp() |
---|
[11978] | 69 | self.setup_for_export() |
---|
| 70 | return |
---|
| 71 | |
---|
| 72 | def test_ifaces(self): |
---|
| 73 | # make sure we fullfill interface contracts |
---|
[12076] | 74 | obj = CustomerExporter() |
---|
[11978] | 75 | verifyObject(ICSVCustomerExporter, obj) |
---|
[12076] | 76 | verifyClass(ICSVCustomerExporter, CustomerExporter) |
---|
[11978] | 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) |
---|
[12076] | 89 | exporter = CustomerExporter() |
---|
[11978] | 90 | exporter.export([self.customer], self.outfile) |
---|
| 91 | result = open(self.outfile, 'rb').read() |
---|
[12143] | 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,,,' |
---|
[12221] | 96 | 'created,[u\'2014-12-04 11:25:35 UTC - Customer created ' |
---|
[12143] | 97 | 'by system\']\r\n', |
---|
| 98 | result |
---|
[11978] | 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) |
---|
[12076] | 106 | exporter = CustomerExporter() |
---|
[11978] | 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) |
---|
[12076] | 121 | exporter = CustomerExporter() |
---|
[11978] | 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 |
---|
[12007] | 132 | |
---|
[12279] | 133 | class CustomerSampleDocumentExporterTest(CustomerImportExportSetup): |
---|
[12007] | 134 | |
---|
| 135 | layer = FunctionalLayer |
---|
| 136 | |
---|
| 137 | def setUp(self): |
---|
[12279] | 138 | super(CustomerSampleDocumentExporterTest, self).setUp() |
---|
[12007] | 139 | self.setup_for_export() |
---|
| 140 | return |
---|
| 141 | |
---|
| 142 | def test_ifaces(self): |
---|
| 143 | # make sure we fullfill interface contracts |
---|
[12279] | 144 | obj = CustomerSampleDocumentExporter() |
---|
[12007] | 145 | verifyObject(ICSVCustomerExporter, obj) |
---|
[12279] | 146 | verifyClass(ICSVCustomerExporter, CustomerSampleDocumentExporter) |
---|
[12007] | 147 | return |
---|
| 148 | |
---|
| 149 | def test_get_as_utility(self): |
---|
| 150 | # we can get a documents exporter as utility |
---|
[12279] | 151 | result = queryUtility(ICSVExporter, name="customersampledocuments") |
---|
[12007] | 152 | self.assertTrue(result is not None) |
---|
| 153 | return |
---|
| 154 | |
---|
| 155 | def test_export_empty(self): |
---|
| 156 | # we can export a nearly empty document |
---|
[12057] | 157 | document = CustomerSampleDocument() |
---|
[12256] | 158 | document.document_id = u'DOC1' |
---|
[12279] | 159 | exporter = CustomerSampleDocumentExporter() |
---|
[12007] | 160 | exporter.export([document], self.outfile) |
---|
| 161 | result = open(self.outfile, 'rb').read() |
---|
| 162 | self.assertEqual( |
---|
| 163 | result, |
---|
[12210] | 164 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
[12256] | 165 | 'CustomerSampleDocument,DOC1,[],,,\r\n' |
---|
[12007] | 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) |
---|
[12256] | 173 | document = self.customer['documents']['DOC1'] |
---|
[12279] | 174 | exporter = CustomerSampleDocumentExporter() |
---|
[12007] | 175 | exporter.export([document], self.outfile) |
---|
| 176 | result = open(self.outfile, 'rb').read() |
---|
| 177 | self.assertTrue( |
---|
[12210] | 178 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
[12007] | 179 | in result |
---|
| 180 | ) |
---|
[12053] | 181 | self.assertMatches( |
---|
[12256] | 182 | '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - ' |
---|
[12210] | 183 | 'Document created by system\'],' |
---|
[12131] | 184 | 'verified,My Document,A111111...', |
---|
[12053] | 185 | result |
---|
[12007] | 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) |
---|
[12279] | 193 | exporter = CustomerSampleDocumentExporter() |
---|
[12007] | 194 | exporter.export_all(self.app, self.outfile) |
---|
| 195 | result = open(self.outfile, 'rb').read() |
---|
| 196 | self.assertTrue( |
---|
[12210] | 197 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
[12007] | 198 | in result) |
---|
[12053] | 199 | self.assertMatches( |
---|
[12256] | 200 | '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - ' |
---|
[12210] | 201 | 'Document created by system\'],' |
---|
[12131] | 202 | 'verified,My Document,A111111...', |
---|
[12053] | 203 | result |
---|
| 204 | ) |
---|
[12007] | 205 | return |
---|
| 206 | |
---|
[12143] | 207 | def test_export_customer_document(self): |
---|
[12007] | 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) |
---|
[12279] | 211 | exporter = CustomerSampleDocumentExporter() |
---|
[12007] | 212 | exporter.export_customer(self.customer, self.outfile) |
---|
| 213 | result = open(self.outfile, 'rb').read() |
---|
| 214 | self.assertTrue( |
---|
[12210] | 215 | 'class_name,document_id,history,state,title,user_id\r\n' |
---|
[12007] | 216 | in result) |
---|
[12053] | 217 | self.assertMatches( |
---|
[12256] | 218 | '...CustomerSampleDocument,DOC1,[u\'2014-11-25 06:57:24 UTC - ' |
---|
[12210] | 219 | 'Document created by system\'],' |
---|
[12131] | 220 | 'verified,My Document,A111111...', |
---|
[12053] | 221 | result |
---|
| 222 | ) |
---|
[12007] | 223 | return |
---|
[12143] | 224 | |
---|
| 225 | |
---|
[12279] | 226 | class SampleContractExporterTest(CustomerImportExportSetup): |
---|
[12143] | 227 | |
---|
| 228 | layer = FunctionalLayer |
---|
| 229 | |
---|
| 230 | def setUp(self): |
---|
[12279] | 231 | super(SampleContractExporterTest, self).setUp() |
---|
[12143] | 232 | self.setup_for_export() |
---|
| 233 | return |
---|
| 234 | |
---|
| 235 | def test_ifaces(self): |
---|
| 236 | # make sure we fullfill interface contracts |
---|
[12279] | 237 | obj = SampleContractExporter() |
---|
[12143] | 238 | verifyObject(ICSVCustomerExporter, obj) |
---|
[12279] | 239 | verifyClass(ICSVCustomerExporter, SampleContractExporter) |
---|
[12143] | 240 | return |
---|
| 241 | |
---|
| 242 | def test_get_as_utility(self): |
---|
| 243 | # we can get a contracts exporter as utility |
---|
[12279] | 244 | result = queryUtility(ICSVExporter, name="samplecontracts") |
---|
[12143] | 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() |
---|
[12259] | 251 | contract.contract_id = u'CON1' |
---|
[12279] | 252 | exporter = SampleContractExporter() |
---|
[12143] | 253 | exporter.export([contract], self.outfile) |
---|
| 254 | result = open(self.outfile, 'rb').read() |
---|
| 255 | self.assertEqual( |
---|
| 256 | result, |
---|
[12500] | 257 | 'class_name,comment,contract_category,contract_id,document_object,' |
---|
[12663] | 258 | 'fee_based,history,last_product_id,' |
---|
| 259 | 'product_object,product_options,state,tc_dict,title,' |
---|
| 260 | 'user_id,valid_from,valid_to\r\n' |
---|
[12143] | 261 | |
---|
[12663] | 262 | 'SampleContract,,sample,CON1,,0,[],,,[],,{},,,,\r\n' |
---|
[12143] | 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) |
---|
[12279] | 270 | exporter = SampleContractExporter() |
---|
[12143] | 271 | exporter.export([self.contract], self.outfile) |
---|
| 272 | result = open(self.outfile, 'rb').read() |
---|
| 273 | self.assertMatches( |
---|
[12500] | 274 | 'class_name,comment,contract_category,contract_id,document_object,' |
---|
[12663] | 275 | 'fee_based,history,last_product_id,' |
---|
| 276 | 'product_object,product_options,state,tc_dict,title,' |
---|
| 277 | 'user_id,valid_from,valid_to\r\n' |
---|
[12143] | 278 | |
---|
[12663] | 279 | 'SampleContract,,sample,CON1,DOC1,1,[u\'2014-12-04 12:10:46 UTC - ' |
---|
[12221] | 280 | 'Contract created by system\'],,' |
---|
[12342] | 281 | 'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",' |
---|
[12633] | 282 | 'created,{\'en\': u\'Hello world\'},,A111111,' |
---|
| 283 | '2014-02-04#,2014-12-04#\r\n', |
---|
[12143] | 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) |
---|
[12279] | 292 | exporter = SampleContractExporter() |
---|
[12143] | 293 | exporter.export_all(self.app, self.outfile) |
---|
| 294 | result = open(self.outfile, 'rb').read() |
---|
| 295 | self.assertMatches( |
---|
[12500] | 296 | 'class_name,comment,contract_category,contract_id,document_object,' |
---|
[12663] | 297 | 'fee_based,history,last_product_id,' |
---|
| 298 | 'product_object,product_options,state,tc_dict,title,' |
---|
| 299 | 'user_id,valid_from,valid_to\r\n' |
---|
[12143] | 300 | |
---|
[12663] | 301 | 'SampleContract,,sample,CON1,DOC1,1,[u\'2014-12-04 12:10:46 UTC - ' |
---|
[12221] | 302 | 'Contract created by system\'],,' |
---|
[12342] | 303 | 'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",' |
---|
[12633] | 304 | 'created,{\'en\': u\'Hello world\'},,A111111,' |
---|
| 305 | '2014-02-04#,2014-12-04#\r\n', |
---|
[12143] | 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) |
---|
[12279] | 314 | exporter = SampleContractExporter() |
---|
[12143] | 315 | exporter.export_customer(self.customer, self.outfile) |
---|
| 316 | result = open(self.outfile, 'rb').read() |
---|
| 317 | self.assertMatches( |
---|
[12500] | 318 | 'class_name,comment,contract_category,contract_id,document_object,' |
---|
[12663] | 319 | 'fee_based,history,last_product_id,' |
---|
| 320 | 'product_object,product_options,state,tc_dict,title,' |
---|
| 321 | 'user_id,valid_from,valid_to\r\n' |
---|
[12143] | 322 | |
---|
[12663] | 323 | 'SampleContract,,sample,CON1,DOC1,1,[u\'2014-12-04 12:10:46 UTC - ' |
---|
[12221] | 324 | 'Contract created by system\'],,' |
---|
[12342] | 325 | 'SAM,"[(u\'Base Fee\', u\'800.6\', u\'USD\')]",' |
---|
[12633] | 326 | 'created,{\'en\': u\'Hello world\'},,A111111,' |
---|
| 327 | '2014-02-04#,2014-12-04#\r\n', |
---|
[12143] | 328 | result |
---|
| 329 | ) |
---|
| 330 | return |
---|
| 331 | |
---|