- Timestamp:
- 12 Mar 2015, 05:29:43 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.ikoba/trunk/src/waeup/ikoba/customers/tests/test_contract.py
r12259 r12741 19 19 Tests for contracts. 20 20 """ 21 import decimal 21 22 from zope.interface.verify import verifyClass, verifyObject 22 from zope.component import createObject 23 from zope.component import createObject, getUtility, getUtilitiesFor 24 from zope.component.hooks import setSite 23 25 from hurry.workflow.interfaces import ( 24 26 IWorkflowInfo, IWorkflowState, InvalidTransitionError) 25 27 from waeup.ikoba.customers.interfaces import ( 26 28 IContractsContainer, IContract) 27 from waeup.ikoba.interfaces import IObjectHistory28 29 from waeup.ikoba.customers.contracts import ( 29 ContractsContainer, SampleContract) 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 30 39 from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase) 40 31 41 32 42 class ContractsContainerTestCase(FunctionalTestCase): … … 89 99 self.assertTrue('Contract created by system' in messages) 90 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) 224 225 def test_different_currencies_forbiddedn(self): 226 # we do not accept different currencies in payment items 227 contract = SampleContract() 228 option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "EUR") 229 option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD") 230 contract.product_options = [option1, option2] 231 self.assertRaises(ValueError, PayableContract, contract)
Note: See TracChangeset for help on using the changeset viewer.