source: main/waeup.ikoba/trunk/src/waeup/ikoba/payments/tests/test_export.py @ 13807

Last change on this file since 13807 was 12819, checked in by uli, 10 years ago

pep8, pyflakes.

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