1 | ## $Id: test_contract.py 12769 2015-03-15 13:14:08Z henrik $ |
---|
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 | Tests for contracts. |
---|
20 | """ |
---|
21 | import decimal |
---|
22 | from zope.interface.verify import verifyClass, verifyObject |
---|
23 | from zope.component import createObject, getUtility, getUtilitiesFor |
---|
24 | from zope.component.hooks import setSite |
---|
25 | from hurry.workflow.interfaces import ( |
---|
26 | IWorkflowInfo, IWorkflowState, InvalidTransitionError) |
---|
27 | from waeup.ikoba.customers.interfaces import ( |
---|
28 | IContractsContainer, IContract) |
---|
29 | from waeup.ikoba.customers.contracts import ( |
---|
30 | ContractsContainer, SampleContract, ContractPayer, ContractFinder, |
---|
31 | PayableContract, |
---|
32 | ) |
---|
33 | from waeup.ikoba.app import Company |
---|
34 | from waeup.ikoba.customers.customer import Customer |
---|
35 | from waeup.ikoba.payments.interfaces import ( |
---|
36 | IPaymentItem, IPayer, IPayableFinder, IPayable, |
---|
37 | ) |
---|
38 | from waeup.ikoba.products.productoptions import ProductOption |
---|
39 | from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) |
---|
40 | |
---|
41 | |
---|
42 | class ContractsContainerTestCase(FunctionalTestCase): |
---|
43 | |
---|
44 | layer = FunctionalLayer |
---|
45 | |
---|
46 | def test_interfaces(self): |
---|
47 | # Make sure the correct interfaces are implemented. |
---|
48 | self.assertTrue( |
---|
49 | verifyClass( |
---|
50 | IContractsContainer, ContractsContainer) |
---|
51 | ) |
---|
52 | self.assertTrue( |
---|
53 | verifyObject( |
---|
54 | IContractsContainer, ContractsContainer()) |
---|
55 | ) |
---|
56 | self.assertTrue( |
---|
57 | verifyClass( |
---|
58 | IContract, SampleContract) |
---|
59 | ) |
---|
60 | self.assertTrue( |
---|
61 | verifyObject( |
---|
62 | IContract, SampleContract()) |
---|
63 | ) |
---|
64 | return |
---|
65 | |
---|
66 | def test_addContract(self): |
---|
67 | container = ContractsContainer() |
---|
68 | contract = createObject(u'waeup.SampleContract') |
---|
69 | id = contract.contract_id |
---|
70 | container.addContract(contract) |
---|
71 | self.assertEqual(container[id], contract) |
---|
72 | self.assertRaises(TypeError, container.addContract, object()) |
---|
73 | self.assertEqual(contract.class_name, 'SampleContract') |
---|
74 | return |
---|
75 | |
---|
76 | def test_contract_workflow(self): |
---|
77 | contract = createObject(u'waeup.SampleContract') |
---|
78 | IWorkflowInfo(contract).fireTransition('create') |
---|
79 | self.assertEqual(IWorkflowState(contract).getState(), 'created') |
---|
80 | IWorkflowInfo(contract).fireTransition('submit') |
---|
81 | self.assertEqual(IWorkflowState(contract).getState(), 'submitted') |
---|
82 | IWorkflowInfo(contract).fireTransition('approve') |
---|
83 | self.assertEqual(IWorkflowState(contract).getState(), 'approved') |
---|
84 | IWorkflowInfo(contract).fireTransition('expire') |
---|
85 | self.assertEqual(IWorkflowState(contract).getState(), 'expired') |
---|
86 | IWorkflowInfo(contract).fireTransition('reset4') |
---|
87 | self.assertEqual(IWorkflowState(contract).getState(), 'created') |
---|
88 | self.assertRaises(InvalidTransitionError, |
---|
89 | IWorkflowInfo(contract).fireTransition, 'approve') |
---|
90 | IWorkflowInfo(contract).fireTransition('submit') |
---|
91 | IWorkflowInfo(contract).fireTransition('reset3') |
---|
92 | self.assertEqual(IWorkflowState(contract).getState(), 'created') |
---|
93 | return |
---|
94 | |
---|
95 | def test_contract_history(self): |
---|
96 | doc = createObject(u'waeup.SampleContract') |
---|
97 | IWorkflowInfo(doc).fireTransition('create') |
---|
98 | messages = ' '.join(doc.history.messages) |
---|
99 | self.assertTrue('Contract created by system' in messages) |
---|
100 | return |
---|
101 | |
---|
102 | |
---|
103 | class TestContractHelpers(FunctionalTestCase): |
---|
104 | |
---|
105 | layer = FunctionalLayer |
---|
106 | |
---|
107 | def test_payer_adapter(self): |
---|
108 | # we can adapt IContract to IPayer (i.e. create a payer) |
---|
109 | customer = Customer() |
---|
110 | customer.firstname, customer.lastname = u'Anna', u'Tester' |
---|
111 | contract = createObject(u'waeup.SampleContract') |
---|
112 | customer['contracts'] = ContractsContainer() |
---|
113 | customer['contracts'].addContract(contract) |
---|
114 | result = IPayer(contract) |
---|
115 | self.assertTrue(isinstance(result, ContractPayer)) |
---|
116 | verifyObject(IPayer, result) |
---|
117 | self.assertEqual(result.first_name, u'Anna') |
---|
118 | self.assertEqual(result.last_name, u'Tester') |
---|
119 | self.assertEqual(result.payer_id, customer.customer_id) |
---|
120 | |
---|
121 | def test_contract_finder_iface(self): |
---|
122 | # we have a contract finder that returns IPayableFinder data. |
---|
123 | verifyClass(IPayableFinder, ContractFinder) |
---|
124 | |
---|
125 | def test_contract_finder_registered(self): |
---|
126 | # the contract finder is a utility registered on startup |
---|
127 | util = getUtility(IPayableFinder, name='contracts_finder') |
---|
128 | self.assertTrue(isinstance(util, ContractFinder)) |
---|
129 | utils = [util for name, util in getUtilitiesFor(IPayableFinder) |
---|
130 | if isinstance(util, ContractFinder)] |
---|
131 | self.assertEqual(len(utils), 1) |
---|
132 | |
---|
133 | def create_contract_and_site(self): |
---|
134 | contract = SampleContract() |
---|
135 | option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD") |
---|
136 | option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD") |
---|
137 | contract.product_options = [option1, option2] |
---|
138 | contract.contract_id = u'CON1234' |
---|
139 | self.getRootFolder()['app'] = Company() |
---|
140 | app = self.getRootFolder()['app'] |
---|
141 | setSite(app) |
---|
142 | return contract, app |
---|
143 | |
---|
144 | def test_contract_finder(self): |
---|
145 | # the contract finder can really find contracts |
---|
146 | contract, app = self.create_contract_and_site() |
---|
147 | app['mycontract'] = contract # trigger cataloging |
---|
148 | finder = ContractFinder() |
---|
149 | result = finder.get_payable_by_id('CON1234') |
---|
150 | self.assertTrue(result is contract) |
---|
151 | |
---|
152 | def test_contract_finder_not_stored(self): |
---|
153 | # we get none if an id is not stored |
---|
154 | contract, app = self.create_contract_and_site() |
---|
155 | app['mycontract'] = contract # trigger cataloging |
---|
156 | finder = ContractFinder() |
---|
157 | result = finder.get_payable_by_id('Not-a-valid-id') |
---|
158 | self.assertTrue(result is None) |
---|
159 | |
---|
160 | def test_contract_finder_no_catalog(self): |
---|
161 | # contract finder does not complain about missing catalog |
---|
162 | finder = ContractFinder() |
---|
163 | result = finder.get_payable_by_id('CON1234') |
---|
164 | self.assertTrue(result is None) |
---|
165 | |
---|
166 | |
---|
167 | class TestContractAsPayable(FunctionalTestCase): |
---|
168 | |
---|
169 | layer = FunctionalLayer |
---|
170 | |
---|
171 | def test_adaptable(self): |
---|
172 | # we can turn contracts into payables. |
---|
173 | contract = SampleContract() |
---|
174 | payable = IPayable(contract) |
---|
175 | self.assertTrue(payable is not None) |
---|
176 | self.assertTrue(isinstance(payable, PayableContract)) |
---|
177 | |
---|
178 | def test_payable_iface(self): |
---|
179 | # PayableContracts really provide IPayable |
---|
180 | contract = SampleContract() |
---|
181 | option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD") |
---|
182 | option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD") |
---|
183 | contract.product_options = [option1, option2] |
---|
184 | payable = PayableContract(contract) |
---|
185 | verifyObject(IPayable, payable) |
---|
186 | verifyClass(IPayable, PayableContract) |
---|
187 | |
---|
188 | def test_payable_simple_attributes(self): |
---|
189 | # the simple attribs are set correctly, according to context contract |
---|
190 | contract = SampleContract() |
---|
191 | contract.title = u'the title' |
---|
192 | option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "EUR") |
---|
193 | option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "EUR") |
---|
194 | contract.product_options = [option1, option2] |
---|
195 | payable = PayableContract(contract) |
---|
196 | self.assertTrue(contract.contract_id, payable.payable_id) |
---|
197 | self.assertEqual(payable.title, contract.title) |
---|
198 | self.assertEqual(payable.currency, 'EUR') |
---|
199 | |
---|
200 | def test_payable_items(self): |
---|
201 | # we can get payment items from payable |
---|
202 | contract = SampleContract() |
---|
203 | option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "EUR") |
---|
204 | option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "EUR") |
---|
205 | contract.product_options = [option1, option2] |
---|
206 | payable = PayableContract(contract) |
---|
207 | items = payable.payment_items |
---|
208 | self.assertTrue(isinstance(items, tuple)) |
---|
209 | self.assertEqual(len(items), 2) |
---|
210 | verifyObject(IPaymentItem, items[0]) |
---|
211 | verifyObject(IPaymentItem, items[1]) |
---|
212 | self.assertEqual(items[0].item_id, '0') |
---|
213 | self.assertEqual(items[0].title, u'Fee 1') |
---|
214 | self.assertEqual(items[0].amount, decimal.Decimal("31.10")) |
---|
215 | |
---|
216 | def test_payable_no_items(self): |
---|
217 | # payables work also with no options set on contract |
---|
218 | contract = SampleContract() |
---|
219 | payable = PayableContract(contract) |
---|
220 | items = payable.payment_items |
---|
221 | self.assertTrue(isinstance(items, tuple)) |
---|
222 | self.assertEqual(len(items), 0) |
---|
223 | self.assertEqual(payable.currency, None) |
---|