source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/tests/test_contract.py @ 12711

Last change on this file since 12711 was 12697, checked in by uli, 10 years ago

Add adapter to turn IContract into IPayer

  • Property svn:keywords set to Id
File size: 5.6 KB
Line 
1## $Id: test_contract.py 12697 2015-03-09 01:51:12Z 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"""
19Tests for contracts.
20"""
21import decimal
22from zope.interface.verify import verifyClass, verifyObject
23from zope.component import createObject
24from hurry.workflow.interfaces import (
25    IWorkflowInfo, IWorkflowState, InvalidTransitionError)
26from waeup.ikoba.customers.interfaces import (
27    IContractsContainer, IContract)
28from waeup.ikoba.customers.contracts import (
29    ContractsContainer, SampleContract, payment_items_from_contract,
30    ContractPayer,
31    )
32from waeup.ikoba.customers.customer import Customer
33from waeup.ikoba.payments.interfaces import IPaymentItem, IPayer
34from waeup.ikoba.products.productoptions import ProductOption
35from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
36
37
38class ContractsContainerTestCase(FunctionalTestCase):
39
40    layer = FunctionalLayer
41
42    def test_interfaces(self):
43        # Make sure the correct interfaces are implemented.
44        self.assertTrue(
45            verifyClass(
46                IContractsContainer, ContractsContainer)
47            )
48        self.assertTrue(
49            verifyObject(
50                IContractsContainer, ContractsContainer())
51            )
52        self.assertTrue(
53            verifyClass(
54                IContract, SampleContract)
55            )
56        self.assertTrue(
57            verifyObject(
58                IContract, SampleContract())
59            )
60        return
61
62    def test_addContract(self):
63        container = ContractsContainer()
64        contract = createObject(u'waeup.SampleContract')
65        id = contract.contract_id
66        container.addContract(contract)
67        self.assertEqual(container[id], contract)
68        self.assertRaises(TypeError, container.addContract, object())
69        self.assertEqual(contract.class_name, 'SampleContract')
70        return
71
72    def test_contract_workflow(self):
73        contract = createObject(u'waeup.SampleContract')
74        IWorkflowInfo(contract).fireTransition('create')
75        self.assertEqual(IWorkflowState(contract).getState(), 'created')
76        IWorkflowInfo(contract).fireTransition('submit')
77        self.assertEqual(IWorkflowState(contract).getState(), 'submitted')
78        IWorkflowInfo(contract).fireTransition('approve')
79        self.assertEqual(IWorkflowState(contract).getState(), 'approved')
80        IWorkflowInfo(contract).fireTransition('expire')
81        self.assertEqual(IWorkflowState(contract).getState(), 'expired')
82        IWorkflowInfo(contract).fireTransition('reset4')
83        self.assertEqual(IWorkflowState(contract).getState(), 'created')
84        self.assertRaises(InvalidTransitionError,
85            IWorkflowInfo(contract).fireTransition, 'approve')
86        IWorkflowInfo(contract).fireTransition('submit')
87        IWorkflowInfo(contract).fireTransition('reset3')
88        self.assertEqual(IWorkflowState(contract).getState(), 'created')
89        return
90
91    def test_contract_history(self):
92        doc = createObject(u'waeup.SampleContract')
93        IWorkflowInfo(doc).fireTransition('create')
94        messages = ' '.join(doc.history.messages)
95        self.assertTrue('Contract created by system' in messages)
96        return
97
98
99class TestContractHelpers(FunctionalTestCase):
100
101    layer = FunctionalLayer
102
103    def test_payment_items_from_contract(self):
104        # we can turn contracts into lists of payment items
105        contract = SampleContract()
106        option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD")
107        option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD")
108        contract.product_options = [option1, option2]
109        payment_items = payment_items_from_contract(contract)
110        assert len(payment_items) == 2
111        item1, item2 = payment_items
112        assert IPaymentItem.providedBy(item1)
113        assert IPaymentItem.providedBy(item2)
114        assert item1.item_id == "0"
115        assert item2.item_id == "1"
116        assert item1.title == u'Fee 1'
117        assert item2.title == u'Fee 2'
118        assert item1.amount == decimal.Decimal("31.10")
119        assert item2.amount == decimal.Decimal("12.12")
120
121    def test_payment_item_from_contract_no_options(self):
122        # we cope with empty contracts
123        contract = SampleContract()
124        payment_items = payment_items_from_contract(contract)
125        assert payment_items == []
126
127    def test_payer_adapter(self):
128        # we can adapt IContract to IPayer (i.e. create a payer)
129        customer = Customer()
130        customer.firstname, customer.lastname = u'Anna', u'Tester'
131        contract = createObject(u'waeup.SampleContract')
132        customer['contracts'] = ContractsContainer()
133        customer['contracts'].addContract(contract)
134        result = IPayer(contract)
135        self.assertTrue(isinstance(result, ContractPayer))
136        verifyObject(IPayer, result)
137        self.assertEqual(result.first_name, u'Anna')
138        self.assertEqual(result.last_name, u'Tester')
139        self.assertEqual(result.payer_id, customer.customer_id)
Note: See TracBrowser for help on using the repository browser.