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, get_customers) |
---|
13 | from waeup.ikoba.customers.interfaces import ICSVCustomerExporter |
---|
14 | from waeup.ikoba.customers.customer import Customer |
---|
15 | from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup |
---|
16 | from waeup.ikoba.testing import FunctionalLayer |
---|
17 | |
---|
18 | curr_year = datetime.datetime.now().year |
---|
19 | year_range = range(curr_year - 9, curr_year + 1) |
---|
20 | year_range_str = ','.join([str(i) for i in year_range]) |
---|
21 | |
---|
22 | class ExportHelperTests(CustomerImportExportSetup): |
---|
23 | layer = FunctionalLayer |
---|
24 | def setUp(self): |
---|
25 | super(ExportHelperTests, self).setUp() |
---|
26 | customer = Customer() |
---|
27 | self.app['customers'].addCustomer(customer) |
---|
28 | customer = self.setup_customer(customer) |
---|
29 | notify(grok.ObjectModifiedEvent(customer)) |
---|
30 | self.customer = self.app['customers'][customer.customer_id] |
---|
31 | return |
---|
32 | |
---|
33 | def test_get_customers_plain(self): |
---|
34 | # without a filter we get all customers |
---|
35 | result = get_customers(self.app) |
---|
36 | self.assertEqual(len(list(result)), 1) |
---|
37 | return |
---|
38 | |
---|
39 | class CustomersExporterTest(CustomerImportExportSetup): |
---|
40 | |
---|
41 | layer = FunctionalLayer |
---|
42 | |
---|
43 | def setUp(self): |
---|
44 | super(CustomersExporterTest, self).setUp() |
---|
45 | self.setup_for_export() |
---|
46 | return |
---|
47 | |
---|
48 | def test_ifaces(self): |
---|
49 | # make sure we fullfill interface contracts |
---|
50 | obj = CustomersExporter() |
---|
51 | verifyObject(ICSVCustomerExporter, obj) |
---|
52 | verifyClass(ICSVCustomerExporter, CustomersExporter) |
---|
53 | return |
---|
54 | |
---|
55 | def test_get_as_utility(self): |
---|
56 | # we can get an customer exporter as utility |
---|
57 | result = queryUtility(ICSVExporter, name="customers") |
---|
58 | self.assertTrue(result is not None) |
---|
59 | return |
---|
60 | |
---|
61 | def test_export(self): |
---|
62 | # we can really export customers |
---|
63 | # set values we can expect in export file |
---|
64 | self.setup_customer(self.customer) |
---|
65 | exporter = CustomersExporter() |
---|
66 | exporter.export([self.customer], self.outfile) |
---|
67 | result = open(self.outfile, 'rb').read() |
---|
68 | self.assertTrue( |
---|
69 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
70 | 'reg_number,sex,suspended,suspended_comment,password,state,' |
---|
71 | 'history\r\n' |
---|
72 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
73 | '123,,0,,,created' |
---|
74 | in result |
---|
75 | ) |
---|
76 | return |
---|
77 | |
---|
78 | def test_export_all(self): |
---|
79 | # we can really export customers |
---|
80 | # set values we can expect in export file |
---|
81 | self.setup_customer(self.customer) |
---|
82 | exporter = CustomersExporter() |
---|
83 | exporter.export_all(self.app, self.outfile) |
---|
84 | result = open(self.outfile, 'rb').read() |
---|
85 | self.assertTrue( |
---|
86 | 'customer_id,email,firstname,lastname,middlename,phone,' |
---|
87 | 'reg_number,sex,suspended,suspended_comment,password,state,history\r\n' |
---|
88 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
89 | '123,,0,,,created' |
---|
90 | in result |
---|
91 | ) |
---|
92 | return |
---|
93 | |
---|
94 | def test_export_customer(self): |
---|
95 | # we can export a single customer |
---|
96 | self.setup_customer(self.customer) |
---|
97 | exporter = CustomersExporter() |
---|
98 | exporter.export_customer(self.customer, self.outfile) |
---|
99 | result = open(self.outfile, 'rb').read() |
---|
100 | self.assertTrue( |
---|
101 | 'customer_id,email,firstname,lastname,middlename,phone,reg_number,' |
---|
102 | 'sex,suspended,suspended_comment,password,state,history\r\n' |
---|
103 | 'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,' |
---|
104 | '123,,0,,,created,' |
---|
105 | in result |
---|
106 | ) |
---|
107 | return |
---|