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

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

Add second customer document class.
Select document factory when adding documents.
Add last_transition_date attribute and further property attributes to documents.

  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1import os
2import grok
3import datetime
4from cStringIO import StringIO
5from zope.component import queryUtility, getUtility
6from zope.event import notify
7from zope.interface.verify import verifyObject, verifyClass
8from waeup.ikoba.interfaces import (
9    ICSVExporter, IExtFileStore, IFileStoreNameChooser)
10from waeup.ikoba.customers.catalog import CustomersQuery
11from waeup.ikoba.customers.export import (
12    CustomersExporter, CustomerDocumentsExporter, get_customers)
13from waeup.ikoba.customers.interfaces import ICSVCustomerExporter
14from waeup.ikoba.customers.customer import Customer
15from waeup.ikoba.customers.documents import CustomerDocument
16from waeup.ikoba.customers.tests.test_batching import CustomerImportExportSetup
17from waeup.ikoba.testing import FunctionalLayer
18
19curr_year = datetime.datetime.now().year
20year_range = range(curr_year - 9, curr_year + 1)
21year_range_str = ','.join([str(i) for i in year_range])
22
23class ExportHelperTests(CustomerImportExportSetup):
24    layer = FunctionalLayer
25    def setUp(self):
26        super(ExportHelperTests, self).setUp()
27        customer = Customer()
28        self.app['customers'].addCustomer(customer)
29        customer = self.setup_customer(customer)
30        notify(grok.ObjectModifiedEvent(customer))
31        self.customer = self.app['customers'][customer.customer_id]
32        return
33
34    def test_get_customers_plain(self):
35        # without a filter we get all customers
36        result = get_customers(self.app)
37        self.assertEqual(len(list(result)), 1)
38        return
39
40class CustomersExporterTest(CustomerImportExportSetup):
41
42    layer = FunctionalLayer
43
44    def setUp(self):
45        super(CustomersExporterTest, self).setUp()
46        self.setup_for_export()
47        return
48
49    def test_ifaces(self):
50        # make sure we fullfill interface contracts
51        obj = CustomersExporter()
52        verifyObject(ICSVCustomerExporter, obj)
53        verifyClass(ICSVCustomerExporter, CustomersExporter)
54        return
55
56    def test_get_as_utility(self):
57        # we can get an customer exporter as utility
58        result = queryUtility(ICSVExporter, name="customers")
59        self.assertTrue(result is not None)
60        return
61
62    def test_export(self):
63        # we can really export customers
64        # set values we can expect in export file
65        self.setup_customer(self.customer)
66        exporter = CustomersExporter()
67        exporter.export([self.customer], self.outfile)
68        result = open(self.outfile, 'rb').read()
69        self.assertTrue(
70            'customer_id,email,firstname,lastname,middlename,phone,'
71            'reg_number,sex,suspended,suspended_comment,password,state,'
72            'history\r\n'
73            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'
74            '123,,0,,,created'
75            in result
76            )
77        return
78
79    def test_export_all(self):
80        # we can really export customers
81        # set values we can expect in export file
82        self.setup_customer(self.customer)
83        exporter = CustomersExporter()
84        exporter.export_all(self.app, self.outfile)
85        result = open(self.outfile, 'rb').read()
86        self.assertTrue(
87            'customer_id,email,firstname,lastname,middlename,phone,'
88            'reg_number,sex,suspended,suspended_comment,password,state,history\r\n'
89            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'
90            '123,,0,,,created'
91            in result
92            )
93        return
94
95    def test_export_customer(self):
96        # we can export a single customer
97        self.setup_customer(self.customer)
98        exporter = CustomersExporter()
99        exporter.export_customer(self.customer, self.outfile)
100        result = open(self.outfile, 'rb').read()
101        self.assertTrue(
102            'customer_id,email,firstname,lastname,middlename,phone,reg_number,'
103            'sex,suspended,suspended_comment,password,state,history\r\n'
104            'A111111,anna@sample.com,Anna,Tester,M.,+234-123-12345#,'
105            '123,,0,,,created,'
106            in result
107            )
108        return
109
110class CustomerDocumentsExporterTest(CustomerImportExportSetup):
111
112    layer = FunctionalLayer
113
114    def setUp(self):
115        super(CustomerDocumentsExporterTest, self).setUp()
116        self.setup_for_export()
117        return
118
119    def test_ifaces(self):
120        # make sure we fullfill interface contracts
121        obj = CustomerDocumentsExporter()
122        verifyObject(ICSVCustomerExporter, obj)
123        verifyClass(ICSVCustomerExporter, CustomerDocumentsExporter)
124        return
125
126    def test_get_as_utility(self):
127        # we can get a documents exporter as utility
128        result = queryUtility(ICSVExporter, name="customerdocuments")
129        self.assertTrue(result is not None)
130        return
131
132    def test_export_empty(self):
133        # we can export a nearly empty document
134        document = CustomerDocument()
135        exporter = CustomerDocumentsExporter()
136        exporter.export([document], self.outfile)
137        result = open(self.outfile, 'rb').read()
138        self.assertEqual(
139            result,
140            'classname,document_id,history,last_transition_date,state,title,customer_id\r\n'
141            'CustomerDocument,d101,[],,,,\r\n'
142            )
143        return
144
145    def test_export(self):
146        # we can really export customer documents.
147        # set values we can expect in export file
148        self.setup_customer(self.customer)
149        document = self.customer['documents']['d101']
150        exporter = CustomerDocumentsExporter()
151        exporter.export([document], self.outfile)
152        result = open(self.outfile, 'rb').read()
153        self.assertTrue(
154            'classname,document_id,history,last_transition_date,state,title,customer_id\r\n'
155            in result
156            )
157        self.assertMatches(
158            '...CustomerDocument,d101,[u\'2014-11-25 06:57:24 UTC - '
159            'Document created by system\'],2014-11-25 06:57:24.990308#,'
160            'created,My Document,A111111...',
161            result
162            )
163        return
164
165    def test_export_all(self):
166        # we can really export all documents
167        # set values we can expect in export file
168        self.setup_customer(self.customer)
169        exporter = CustomerDocumentsExporter()
170        exporter.export_all(self.app, self.outfile)
171        result = open(self.outfile, 'rb').read()
172        self.assertTrue(
173            'classname,document_id,history,last_transition_date,state,title,customer_id\r\n'
174            in result)
175        self.assertMatches(
176            '...CustomerDocument,d101,[u\'2014-11-25 06:57:24 UTC - '
177            'Document created by system\'],2014-11-25 06:57:24.990308#,'
178            'created,My Document,A111111...',
179            result
180            )
181        return
182
183    def test_export_customer(self):
184        # we can really export all documents of a certain customer
185        # set values we can expect in export file
186        self.setup_customer(self.customer)
187        exporter = CustomerDocumentsExporter()
188        exporter.export_customer(self.customer, self.outfile)
189        result = open(self.outfile, 'rb').read()
190        self.assertTrue(
191            'classname,document_id,history,last_transition_date,state,title,customer_id\r\n'
192            in result)
193        self.assertMatches(
194            '...CustomerDocument,d101,[u\'2014-11-25 06:57:24 UTC - '
195            'Document created by system\'],2014-11-25 06:57:24.990308#,'
196            'created,My Document,A111111...',
197            result
198            )
199        return
Note: See TracBrowser for help on using the repository browser.