1 | ## $Id: test_export.py 12800 2015-03-20 13:07:29Z uli $ |
---|
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 payment exporter. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import shutil |
---|
23 | import tempfile |
---|
24 | import unittest |
---|
25 | import grok |
---|
26 | import datetime |
---|
27 | from decimal import Decimal |
---|
28 | from cStringIO import StringIO |
---|
29 | from zope.component import queryUtility, getUtility, createObject |
---|
30 | from zope.component.hooks import setSite, clearSite |
---|
31 | from zope.event import notify |
---|
32 | from zope.interface.verify import verifyObject, verifyClass |
---|
33 | from waeup.ikoba.interfaces import ( |
---|
34 | ICSVExporter, IExtFileStore, IFileStoreNameChooser) |
---|
35 | from waeup.ikoba.app import Company |
---|
36 | from waeup.ikoba.products.productoptions import ProductOption |
---|
37 | from waeup.ikoba.payments.export import PaymentExporter |
---|
38 | from waeup.ikoba.payments.payment import Payment |
---|
39 | from waeup.ikoba.payments.interfaces import IPayer, IPayable, STATE_PAID |
---|
40 | from waeup.ikoba.testing import FunctionalLayer, FunctionalTestCase |
---|
41 | |
---|
42 | |
---|
43 | class PaymentExporterTest(FunctionalTestCase): |
---|
44 | |
---|
45 | layer = FunctionalLayer |
---|
46 | |
---|
47 | def setUp(self): |
---|
48 | super(PaymentExporterTest, self).setUp() |
---|
49 | self.dc_root = tempfile.mkdtemp() |
---|
50 | self.workdir = tempfile.mkdtemp() |
---|
51 | app = Company() |
---|
52 | app['datacenter'].setStoragePath(self.dc_root) |
---|
53 | self.getRootFolder()['app'] = app |
---|
54 | self.app = self.getRootFolder()['app'] |
---|
55 | setSite(app) |
---|
56 | self.logfile = os.path.join( |
---|
57 | self.app['datacenter'].storage, 'logs', 'main.log') |
---|
58 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
59 | |
---|
60 | # Setup customer with contract and payment |
---|
61 | customer = createObject('waeup.Customer') |
---|
62 | self.app['customers'].addCustomer(customer) |
---|
63 | contract = createObject('waeup.SampleContract') |
---|
64 | contract.contract_id = u'CON1' |
---|
65 | contract.title = u'My Contract' |
---|
66 | prodoption = ProductOption() |
---|
67 | prodoption.title = u'Any product option' |
---|
68 | prodoption.fee = Decimal('88.8') |
---|
69 | prodoption.currency = 'EUR' |
---|
70 | contract.product_options = [prodoption, ] |
---|
71 | customer['contracts'].addContract(contract) |
---|
72 | payer = IPayer(customer) |
---|
73 | payable = IPayable(contract) |
---|
74 | self.payment = Payment(payer, payable) |
---|
75 | self.payment.gateway_service = 'demo_creditcard' |
---|
76 | self.payment.state = STATE_PAID |
---|
77 | self.app['payments'][self.payment.payment_id] = self.payment |
---|
78 | return |
---|
79 | |
---|
80 | def tearDown(self): |
---|
81 | super(PaymentExporterTest, self).tearDown() |
---|
82 | shutil.rmtree(self.workdir) |
---|
83 | shutil.rmtree(self.dc_root) |
---|
84 | clearSite() |
---|
85 | return |
---|
86 | |
---|
87 | def test_ifaces(self): |
---|
88 | # make sure we fullfill interface contracts |
---|
89 | obj = PaymentExporter() |
---|
90 | verifyObject(ICSVExporter, obj) |
---|
91 | verifyClass(ICSVExporter,PaymentExporter) |
---|
92 | return |
---|
93 | |
---|
94 | def test_get_as_utility(self): |
---|
95 | # we can get an payment exporter as utility |
---|
96 | result = queryUtility(ICSVExporter, name="payments") |
---|
97 | self.assertTrue(result is not None) |
---|
98 | return |
---|
99 | |
---|
100 | def test_export(self): |
---|
101 | # we can really export a payment |
---|
102 | exporter = PaymentExporter() |
---|
103 | exporter.export([self.payment], self.outfile) |
---|
104 | result = open(self.outfile, 'rb').read() |
---|
105 | self.assertTrue( |
---|
106 | 'creation_date,currency,gateway_service,payable_id,payee_id,' |
---|
107 | 'payer_id,payment_date,payment_id,payment_items,state,title\r\n' |
---|
108 | in result) |
---|
109 | self.assertMatches( |
---|
110 | '...2015-03-16 16:07:33.273297#,EUR,demo_creditcard,CON1,' |
---|
111 | ',K1000000,,%s,"[(u\'Any product option\', u\'88.8\')]",' |
---|
112 | '64,My Contract...' % self.payment.payment_id, |
---|
113 | result |
---|
114 | ) |
---|
115 | return |
---|
116 | |
---|
117 | def test_export_all(self): |
---|
118 | # we can really export all payments |
---|
119 | exporter = PaymentExporter() |
---|
120 | exporter.export_all(self.app, self.outfile) |
---|
121 | result = open(self.outfile, 'rb').read() |
---|
122 | self.assertTrue( |
---|
123 | 'creation_date,currency,gateway_service,payable_id,payee_id,' |
---|
124 | 'payer_id,payment_date,payment_id,payment_items,state,title\r\n' |
---|
125 | in result) |
---|
126 | self.assertMatches( |
---|
127 | '...2015-03-16 16:07:33.273297#,EUR,demo_creditcard,CON1,' |
---|
128 | ',K1000000,,%s,"[(u\'Any product option\', u\'88.8\')]",' |
---|
129 | '64,My Contract...' % self.payment.payment_id, |
---|
130 | result |
---|
131 | ) |
---|
132 | return |
---|