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

Last change on this file since 12688 was 12683, checked in by uli, 10 years ago

Add adapter to turn lists of product opitons into payment items.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1## $Id: test_contract.py 12683 2015-03-07 04:18:42Z 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    )
31from waeup.ikoba.payments.interfaces import IPaymentItem
32from waeup.ikoba.products.productoptions import ProductOption
33from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
34
35
36class ContractsContainerTestCase(FunctionalTestCase):
37
38    layer = FunctionalLayer
39
40    def test_interfaces(self):
41        # Make sure the correct interfaces are implemented.
42        self.assertTrue(
43            verifyClass(
44                IContractsContainer, ContractsContainer)
45            )
46        self.assertTrue(
47            verifyObject(
48                IContractsContainer, ContractsContainer())
49            )
50        self.assertTrue(
51            verifyClass(
52                IContract, SampleContract)
53            )
54        self.assertTrue(
55            verifyObject(
56                IContract, SampleContract())
57            )
58        return
59
60    def test_addContract(self):
61        container = ContractsContainer()
62        contract = createObject(u'waeup.SampleContract')
63        id = contract.contract_id
64        container.addContract(contract)
65        self.assertEqual(container[id], contract)
66        self.assertRaises(TypeError, container.addContract, object())
67        self.assertEqual(contract.class_name, 'SampleContract')
68        return
69
70    def test_contract_workflow(self):
71        contract = createObject(u'waeup.SampleContract')
72        IWorkflowInfo(contract).fireTransition('create')
73        self.assertEqual(IWorkflowState(contract).getState(), 'created')
74        IWorkflowInfo(contract).fireTransition('submit')
75        self.assertEqual(IWorkflowState(contract).getState(), 'submitted')
76        IWorkflowInfo(contract).fireTransition('approve')
77        self.assertEqual(IWorkflowState(contract).getState(), 'approved')
78        IWorkflowInfo(contract).fireTransition('expire')
79        self.assertEqual(IWorkflowState(contract).getState(), 'expired')
80        IWorkflowInfo(contract).fireTransition('reset4')
81        self.assertEqual(IWorkflowState(contract).getState(), 'created')
82        self.assertRaises(InvalidTransitionError,
83            IWorkflowInfo(contract).fireTransition, 'approve')
84        IWorkflowInfo(contract).fireTransition('submit')
85        IWorkflowInfo(contract).fireTransition('reset3')
86        self.assertEqual(IWorkflowState(contract).getState(), 'created')
87        return
88
89    def test_contract_history(self):
90        doc = createObject(u'waeup.SampleContract')
91        IWorkflowInfo(doc).fireTransition('create')
92        messages = ' '.join(doc.history.messages)
93        self.assertTrue('Contract created by system' in messages)
94        return
95
96
97class TestContractHelpers(FunctionalTestCase):
98
99    layer = FunctionalLayer
100
101    def test_payment_items_from_contract(self):
102        # we can turn contracts into lists of payment items
103        contract = SampleContract()
104        option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD")
105        option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD")
106        contract.product_options = [option1, option2]
107        payment_items = payment_items_from_contract(contract)
108        assert len(payment_items) == 2
109        item1, item2 = payment_items
110        assert IPaymentItem.providedBy(item1)
111        assert IPaymentItem.providedBy(item2)
112        assert item1.item_id == "0"
113        assert item2.item_id == "1"
114        assert item1.title == u'Fee 1'
115        assert item2.title == u'Fee 2'
116        assert item1.amount == decimal.Decimal("31.10")
117        assert item2.amount == decimal.Decimal("12.12")
118
119    def test_payment_item_from_contract_no_options(self):
120        # we cope with empty contracts
121        contract = SampleContract()
122        payment_items = payment_items_from_contract(contract)
123        assert payment_items == []
Note: See TracBrowser for help on using the repository browser.