testERP5CurrencyExchangeLine.py 25.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Aurelien Calonne <aurel@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import unittest
Jérome Perrin's avatar
Jérome Perrin committed
29

30
import transaction
31 32 33 34 35
from DateTime import DateTime
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5.tests.testAccounting import AccountingTestCase
from AccessControl.SecurityManagement import newSecurityManager

36
class CurrencyExchangeTestCase(AccountingTestCase, ERP5TypeTestCase):
37 38

  username = 'username'
39

40
  def beforeTearDown(self):
41
    transaction.abort()
42 43 44
    # clear modules if necessary
    currency_list = ('euro', 'yen', 'usd')
    module = self.portal.currency_module
Jérome Perrin's avatar
Jérome Perrin committed
45
    module.manage_delObjects([x for x in module.objectIds()
46 47 48 49 50 51
                        if x not in currency_list])
    for currency_id in currency_list:
      currency = self.currency_module._getOb(currency_id, None)
      if currency is not None:
        currency.manage_delObjects([x.getId() for x in
                currency.objectValues(
52 53 54 55 56 57 58
                 portal_type='Currency Exchange Line')])

    currency_exchange_type = \
      self.portal.portal_categories.currency_exchange_type
    currency_exchange_type.manage_delObjects(
                list(currency_exchange_type.objectIds()))

59
    transaction.commit()
Jérome Perrin's avatar
Jérome Perrin committed
60
    self.tic()
61
 
Jérome Perrin's avatar
Jérome Perrin committed
62
  def login(self, name=username):
63
    uf = self.getPortal().acl_users
64 65
    uf._doAddUser(self.username, '', ['Assignee', 'Assignor',
       'Author','Manager'], [])
66 67 68 69 70 71 72 73 74 75 76
    user = uf.getUserById(self.username).__of__(uf)
    newSecurityManager(None, user)
   
  def getBusinessTemplateList(self):
    """
      Return the list of business templates we need to run the test.
      This method is called during the initialization of the unit test by
      the unit test framework in order to know which business templates
      need to be installed to run the test on.
    """
    return ('erp5_base',
Jérome Perrin's avatar
Jérome Perrin committed
77
            'erp5_pdm',
78
            'erp5_accounting',
Jérome Perrin's avatar
Jérome Perrin committed
79
            'erp5_accounting_ui_test'
80 81
            )

82 83 84 85 86

class TestCurrencyExchangeLine(CurrencyExchangeTestCase):
  """
  Test Currency exchange lines.
  """
Jérome Perrin's avatar
Jérome Perrin committed
87
  def test_CreateCurrencies(self):
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    """
      Create currencies to be used for transactions
    """
    module = self.portal.currency_module
    currency1 = module.newContent(portal_type='Currency')
    currency1.setTitle('Euro')
    currency1.setReference('EUR')
    currency1.setBaseUnitQuantity(0.01)
    self.assertEquals(currency1.getTitle(), 'Euro')
    self.assertEquals(currency1.getReference(), 'EUR')
    self.assertEquals(currency1.getBaseUnitQuantity(), 0.01)
    currency1.validate()
    self.assertEquals(0, len(currency1.checkConsistency()))
    self.assertEquals(currency1.getValidationState(),'validated')
    currency2 = module.newContent(portal_type = 'Currency')
    currency2.setTitle('Francs CFA')
    currency2.setReference('XOF')
    currency2.setBaseUnitQuantity(1.00)
    self.assertEquals(currency2.getTitle(), 'Francs CFA')
    self.assertEquals(currency2.getReference(), 'XOF')
    self.assertEquals(currency2.getBaseUnitQuantity(), 1.00)
    currency2.validate()
    self.assertEquals(currency2.getValidationState(),'validated')
    self.assertEquals(0, len(currency2.checkConsistency()))
Jérome Perrin's avatar
Jérome Perrin committed
112 113


Jérome Perrin's avatar
Jérome Perrin committed
114
  def test_UseCurrencyExchangeLineForDestination(self):
115
    """
116 117
      Create a currency exchange line for a currency and then
      convert destination price using that currency exchange line
118 119 120
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
121
    new_currency = self.portal.currency_module.newContent(
122
                          portal_type='Currency')
123 124 125
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
126
    transaction.commit()
127
    self.tic()
128 129
    self.organisation1.edit(
                    price_currency=new_currency.getRelativeUrl())
130 131
    euro = self.portal.currency_module.euro
    x_curr_ex_line = euro.newContent(
Jérome Perrin's avatar
Jérome Perrin committed
132 133
                        portal_type='Currency Exchange Line',
                        price_currency=new_currency.getRelativeUrl())
134 135 136
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,9,8))
137 138 139
    x_curr_ex_line.setStopDate(DateTime(2008,9,10))
    self.assertEquals(x_curr_ex_line.getPriceCurrencyTitle(),
                          'Francs CFA')
140 141
    self.assertEquals(x_curr_ex_line.getBasePrice(),655.957)
    x_curr_ex_line.validate()
142 143
    self.assertEquals(x_curr_ex_line.getValidationState(),
                           'validated')
144
    accounting_module = self.portal.accounting_module
145
    invoice = self._makeOne(
146 147
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
148
            source_section_value=self.organisation_module.supplier,
Jérome Perrin's avatar
Jérome Perrin committed
149 150
               lines=(dict(
               destination_value=self.account_module.goods_purchase,
151
                           destination_debit=500),
152
              dict(destination_value=self.account_module.receivable,
153
                           destination_credit=500)))
154 155
    invoice.AccountingTransaction_convertDestinationPrice(form_id='view')
    line_list = invoice.contentValues(
156
           portal_type=self.portal.getPortalAccountingMovementTypeList())
157
    for line in line_list:
Jérome Perrin's avatar
Jérome Perrin committed
158
      self.assertEquals(line.getDestinationTotalAssetPrice(),
159 160
             round(655.957*line.getQuantity()))
                                        
Jérome Perrin's avatar
Jérome Perrin committed
161

Jérome Perrin's avatar
Jérome Perrin committed
162
  def test_CreateEmptyCurrencyExchangeLineForDestination(self):
163 164 165 166 167 168 169
    """
      Create empty currency exchange lines for currencies,
      and verify that only the one that matches the criteria will
      be selected for the conversion
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
170
    new_currency = self.portal.currency_module.newContent(
171 172 173 174
                   portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
175
    transaction.commit()
176
    self.tic()
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    self.organisation1.edit(
                   price_currency=new_currency.getRelativeUrl())
    euro = self.portal.currency_module.euro
    x_curr_ex_line = euro.newContent(
                      portal_type='Currency Exchange Line',
                     price_currency=new_currency.getRelativeUrl())
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,9,8))
    x_curr_ex_line.setStopDate(DateTime(2008,9,10))
    self.assertEquals(x_curr_ex_line.getPriceCurrencyTitle(),
                                         'Francs CFA')
    self.assertEquals(x_curr_ex_line.getBasePrice(),655.957)
    x_curr_ex_line.validate()
    self.assertEquals(x_curr_ex_line.getValidationState(),
                            'validated')
    yen = self.portal.currency_module.yen
    yen_line1 = yen.newContent(
                          portal_type='Currency Exchange Line')
    yen_line2 = yen.newContent(
                          portal_type='Currency Exchange Line')
                          
    usd = self.portal.currency_module.usd
    usd_line1 = usd.newContent(
                          portal_type='Currency Exchange Line')
    usd_line2 = usd.newContent(
                          portal_type='Currency Exchange Line')
    
    euro_line = euro.newContent(
                           portal_type='Currency Exchange Line')
    euro_line.validate()
    accounting_module = self.portal.accounting_module
209
    invoice = self._makeOne(
210 211 212 213 214 215 216 217
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
           source_section_value=self.organisation_module.supplier,
               lines=(dict(
              destination_value=self.account_module.goods_purchase,
                           destination_debit=500),
              dict(destination_value=self.account_module.receivable,
                           destination_credit=500)))
218
    invoice.AccountingTransaction_convertDestinationPrice(
219
                         form_id='view')
220
    line_list = invoice.contentValues(
221
           portal_type=self.portal.getPortalAccountingMovementTypeList())
222
    for line in line_list:
Jérome Perrin's avatar
Jérome Perrin committed
223 224 225
      self.assertEquals(line.getDestinationTotalAssetPrice(),
                   round(655.957*line.getQuantity()))

Jérome Perrin's avatar
Jérome Perrin committed
226
  def test_UseCurrencyExchangeLineForSource(self):
227
    """
228 229
      Create a currency exchange line for a currency and then
      convert
230 231 232 233
      source price using that currency exchange line
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
234
    new_currency = self.portal.currency_module.newContent(portal_type='Currency')
235 236 237
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
238
    transaction.commit()
239
    self.tic()
240 241
    self.organisation1.edit(
              price_currency=new_currency.getRelativeUrl())
242 243
    euro = self.portal.currency_module.euro
    x_curr_ex_line = euro.newContent(
Jérome Perrin's avatar
Jérome Perrin committed
244 245
                          portal_type='Currency Exchange Line',
                          price_currency=new_currency.getRelativeUrl())
246 247 248
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,9,8))
249 250 251
    x_curr_ex_line.setStopDate(DateTime(2008,9,10))
    self.assertEquals(x_curr_ex_line.getPriceCurrencyTitle(),
                            'Francs CFA')
252 253
    self.assertEquals(x_curr_ex_line.getBasePrice(),655.957)
    x_curr_ex_line.validate()
254 255
    self.assertEquals(x_curr_ex_line.getValidationState(),
                         'validated')
256
    accounting_module = self.portal.accounting_module
257
    invoice = self._makeOne(
258 259
               portal_type='Sale Invoice Transaction',
               start_date=DateTime('2008/09/08'),
260 261
        destination_section_value=self.organisation_module.supplier,
        lines=(dict(source_value=self.account_module.goods_purchase,
262
                           source_debit=500),
263
                   dict(source_value=self.account_module.receivable,
264
                           source_credit=500)))
265
    invoice.AccountingTransaction_convertSourcePrice(
266
                   form_id='view')
267
    line_list = invoice.contentValues(
268
           portal_type=self.portal.getPortalAccountingMovementTypeList())
269
    for line in line_list:
270
      if line.getSourceValue() == self.account_module.goods_purchase:
271 272
        self.assertEquals(line.getSourceInventoriatedTotalAssetDebit(),
                           327979)
273
      elif line.getSourceValue() == self.account_module.receivable:
274 275
        self.assertEquals(line.getSourceInventoriatedTotalAssetCredit(),
                           327979)
276 277
      else:
        self.fail('line not found')
Jérome Perrin's avatar
Jérome Perrin committed
278

Jérome Perrin's avatar
Jérome Perrin committed
279
  def test_NoCurrencyExchangeLineForResourceCurrency(self):
280 281 282 283 284 285
    """
      Test that the conversion is not done when there is no currency 
      exchange line defined for the date of the transaction
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
286
    new_currency = self.portal.currency_module.newContent(
287
                portal_type='Currency')
288 289 290
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
291
    transaction.commit()
292
    self.tic()
293 294
    self.organisation1.edit(
                price_currency=new_currency.getRelativeUrl())
295 296
    euro = self.portal.currency_module.euro
    accounting_module = self.portal.accounting_module
297
    invoice = self._makeOne(
298 299
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
300
            source_section_value=self.organisation_module.supplier,
Jérome Perrin's avatar
Jérome Perrin committed
301 302
               lines=(dict(
                 destination_value=self.account_module.goods_purchase,
303
                           destination_debit=500),
304
              dict(destination_value=self.account_module.receivable,
305
                           destination_credit=500)))
306
    invoice.AccountingTransaction_convertDestinationPrice(
307
                        form_id='view')
308
    line_list = invoice.contentValues(
309
           portal_type=self.portal.getPortalAccountingMovementTypeList())
310
    for line in line_list:
Jérome Perrin's avatar
Jérome Perrin committed
311 312 313
      self.assertEquals(line.getDestinationTotalAssetPrice(),None)


Jérome Perrin's avatar
Jérome Perrin committed
314
  def test_DateOfCurrencyExchangeLineNotDateofTransaction(self):
315 316 317
    """
      Test that the conversion is not done when there is the start date
      and the end date of a currency exchange line don't correspond to
318 319 320
      the date of the transaction, but when the date of the transaction
      falls into the validity period of the currency exchange line,the
      conversion is done
321 322 323
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
324
    new_currency = self.portal.currency_module.newContent(
325
    portal_type='Currency')
326 327 328
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
329
    transaction.commit()
330
    self.tic()
331 332
    self.organisation1.edit(
               price_currency=new_currency.getRelativeUrl())
333
    euro = self.portal.currency_module.euro
334
    x_curr_ex_line = euro.newContent(
Jérome Perrin's avatar
Jérome Perrin committed
335 336
                      portal_type='Currency Exchange Line',
                      price_currency=new_currency.getRelativeUrl())
337 338 339 340
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,9,6))
    x_curr_ex_line.setStopDate(DateTime(2008,9,7))
341 342
    self.assertEquals(x_curr_ex_line.getPriceCurrencyTitle(),
               'Francs CFA')
343 344
    self.assertEquals(x_curr_ex_line.getBasePrice(),655.957)
    x_curr_ex_line.validate()
345 346
    self.assertEquals(x_curr_ex_line.getValidationState(),
                               'validated')
347
    accounting_module = self.portal.accounting_module
348
    transaction1 = self._makeOne(
349 350
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
351
            source_section_value=self.organisation_module.supplier,
Jérome Perrin's avatar
Jérome Perrin committed
352 353
            lines=(dict(
               destination_value=self.account_module.goods_purchase,
354
                           destination_debit=500),
355
              dict(destination_value=self.account_module.receivable,
356
                           destination_credit=500)))
357 358
    transaction1.AccountingTransaction_convertDestinationPrice(
                              form_id='view')
359
    line_list = transaction1.contentValues(
360
           portal_type=self.portal.getPortalAccountingMovementTypeList())
361
    for line in line_list:
Jérome Perrin's avatar
Jérome Perrin committed
362
      self.assertEquals(line.getDestinationTotalAssetPrice(),None)
363 364 365
    transaction2 = self._makeOne(
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/06'),
366
             source_section_value=self.organisation_module.supplier,
Jérome Perrin's avatar
Jérome Perrin committed
367 368
               lines=(dict(
               destination_value=self.account_module.goods_purchase,
369
                           destination_debit=500),
370
              dict(destination_value=self.account_module.receivable,
371
                           destination_credit=500)))
372 373
    transaction2.AccountingTransaction_convertDestinationPrice(
                  form_id='view')
374
    line_list = transaction2.contentValues(
375
        portal_type=self.portal.getPortalAccountingMovementTypeList())
376
    for line in line_list:
377
      if line.getDestinationValue() == self.account_module.goods_purchase:
378 379
        self.assertEquals(line.getDestinationInventoriatedTotalAssetDebit(),
                           327979)
380
      elif line.getDestinationValue() == self.account_module.receivable:
381 382
        self.assertEquals(line.getDestinationInventoriatedTotalAssetCredit(),
                           327979)
383 384 385 386
      else:
        self.fail('line not found')


Jérome Perrin's avatar
Jérome Perrin committed
387
  def test_CreateCELWithNoReferenceCurrency(self):
388 389 390 391 392 393
    """
      Create a currency exchange line with no reference currency
      and verify that the CEL won't apply for the currency
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
394
    new_currency = self.portal.currency_module.newContent(
395 396 397 398
               portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
399
    transaction.commit()
400
    self.tic()
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
    self.organisation1.edit(
            price_currency=new_currency.getRelativeUrl())
    euro = self.portal.currency_module.euro
    x_curr_ex_line = euro.newContent(
                              portal_type='Currency Exchange Line')
    x_curr_ex_line.setTitle('Euro to Francs CFA')
    x_curr_ex_line.setBasePrice(655.957)
    x_curr_ex_line.setStartDate(DateTime(2008,9,8))
    x_curr_ex_line.setStopDate(DateTime(2008,9,10))
    self.assertEquals(x_curr_ex_line.getPriceCurrency(),None)
    self.assertEquals(x_curr_ex_line.getBasePrice(),655.957)
    x_curr_ex_line.validate()
    self.assertEquals(x_curr_ex_line.getValidationState(),
                          'validated')
    
    accounting_module = self.portal.accounting_module
417
    invoice = self._makeOne(
418 419 420 421 422 423 424 425 426
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
             source_section_value=self.organisation_module.supplier,
               lines=(dict(
               destination_value=self.account_module.goods_purchase,
                           destination_debit=500),
              dict(destination_value=self.account_module.receivable,
                           destination_credit=500)))
    
427
    invoice.AccountingTransaction_convertDestinationPrice(
428
                           form_id='view')
429
    line_list = invoice.contentValues(
430
           portal_type=self.portal.getPortalAccountingMovementTypeList())
431 432 433 434 435
    for line in line_list:
        self.assertEquals(line.getDestinationTotalAssetPrice(),
                 None)
  
   
Jérome Perrin's avatar
Jérome Perrin committed
436
  def test_CreateCELWithNoBasePrice(self):
437 438 439 440 441 442
    """
      Create two currency exchange lines with no base and 
      verify that only one of the CEL will apply for the currency
    """
    self.organisation_module = self.portal.organisation_module
    self.organisation1 = self.organisation_module.my_organisation
443
    new_currency = self.portal.currency_module.newContent(
444 445 446 447
                portal_type='Currency')
    new_currency.setReference('XOF')
    new_currency.setTitle('Francs CFA')
    new_currency.setBaseUnitQuantity(1.00)
448
    transaction.commit()
449
    self.tic()
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
    self.organisation1.edit(
               price_currency=new_currency.getRelativeUrl())
    euro = self.portal.currency_module.euro
    
    euro_line1 = euro.newContent(
                              portal_type='Currency Exchange Line',
                       price_currency=new_currency.getRelativeUrl())
    euro_line1.setTitle('Euro to Francs CFA')
    euro_line1.setBasePrice(655.957)
    euro_line1.setStartDate(DateTime(2008,9,8))
    euro_line1.setStopDate(DateTime(2008,9,10))
    self.assertEquals(euro_line1.getPriceCurrencyTitle(),
                            'Francs CFA')
    self.assertEquals(euro_line1.getBasePrice(),655.957)
    euro_line1.validate()
    self.assertEquals(euro_line1.getValidationState(),
                                 'validated')
    euro_line2 = euro.newContent(
                              portal_type='Currency Exchange Line',
                       price_currency=new_currency.getRelativeUrl())
    euro_line2.setTitle('Euro to Francs CFA')
    euro_line2.setStartDate(DateTime(2008,9,8))
    euro_line2.setStopDate(DateTime(2008,9,10))
    self.assertEquals(euro_line2.getPriceCurrencyTitle(),
                            'Francs CFA')
    self.assertEquals(euro_line2.getBasePrice(),None)
    euro_line2.validate()
    
    self.assertEquals(euro_line2.getValidationState(),
                                 'validated')
    accounting_module = self.portal.accounting_module
481
    invoice = self._makeOne(
482 483 484 485 486 487 488 489
               portal_type='Purchase Invoice Transaction',
               stop_date=DateTime('2008/09/08'),
            source_section_value=self.organisation_module.supplier,
               lines=(dict(
               destination_value=self.account_module.goods_purchase,
                           destination_debit=500),
              dict(destination_value=self.account_module.receivable,
                           destination_credit=500)))
490
    invoice.AccountingTransaction_convertDestinationPrice(
491
                           form_id='view')
492
    line_list = invoice.contentValues(
493
        portal_type=self.portal.getPortalAccountingMovementTypeList())
494

495
    for line in line_list:
496
      if line.getDestinationValue() == self.account_module.goods_purchase:
497 498
        self.assertEquals(line.getDestinationInventoriatedTotalAssetDebit(),
                           327979)
499
      elif line.getDestinationValue() == self.account_module.receivable:
500 501
        self.assertEquals(line.getDestinationInventoriatedTotalAssetCredit(),
                           327979)
502 503 504
      else:
        self.fail('line not found')
  
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
class TestCurrencyExchangeCell(CurrencyExchangeTestCase):
  def afterSetUp(self):
    currency_exchange_type = \
      self.portal.portal_categories.currency_exchange_type
    currency_exchange_type.newContent(
          portal_type='Category',
          id='type_a',
          title='Type A',
          int_index=1)
    currency_exchange_type.newContent(
          portal_type='Category',
          id='type_b',
          title='Type B',
          int_index=2)
    
  def test_CreateCurrencyExchangeCell(self):
    euro = self.currency_module.euro
    usd = self.currency_module.usd
    euro_to_usd = euro.newContent(portal_type='Currency Exchange Line')
    self.assertEquals(0, len(euro_to_usd.contentValues()))
    # when we set the target currency, currency exchange cells will be added
    euro_to_usd.setPriceCurrencyValue(usd)
    self.assertEquals(2, len(euro_to_usd.contentValues()))

    # cell range is like this:
    self.assertEquals([
      ['currency_exchange_type/type_a', 'currency_exchange_type/type_b'],
      ['resource/%s' % euro.getRelativeUrl()],
      ['price_currency/%s' % usd.getRelativeUrl()],
      ], euro_to_usd.getCellRange(base_id='path'))
    
    type_a_cell = euro_to_usd.getCell(
      'currency_exchange_type/type_a',
      'resource/%s' % euro.getRelativeUrl(),
      'price_currency/%s' % usd.getRelativeUrl(),
      base_id='path')
    self.assertNotEquals(None, type_a_cell)
    self.assertEquals('Currency Exchange Cell', type_a_cell.getPortalTypeName())

    # int index have been copied, so that listbox in CurrencyExchangeLine_view
    # displays currency exchange line in same order than int indexes on
    # currency_exchange_type categories.
    self.assertEquals(1, type_a_cell.getIntIndex())
 
    self.assertTrue('currency_exchange_type/type_a' in
        type_a_cell.getCategoryList())
    
    type_a_cell_predicate = type_a_cell.asPredicate()
    self.assertEquals(sorted(('price_currency',
                              'resource',
                              'currency_exchange_type')),
      sorted(type_a_cell_predicate.getMembershipCriterionBaseCategoryList()))

    self.assertEquals(sorted(('price_currency/currency_module/usd',
                              'resource/currency_module/euro',
                              'currency_exchange_type/type_a')),
          sorted(type_a_cell_predicate.getMembershipCriterionCategoryList()))


  def test_ConvertUsingCurrencyExchangeCell(self):
    euro = self.currency_module.euro
    usd = self.currency_module.usd
    euro_to_usd = euro.newContent(portal_type='Currency Exchange Line')
    euro_to_usd.setPriceCurrencyValue(usd)
    euro_to_usd.validate()
  
    type_a_cell = euro_to_usd.getCell(
      'currency_exchange_type/type_a',
      'resource/%s' % euro.getRelativeUrl(),
      'price_currency/%s' % usd.getRelativeUrl(),
      base_id='path')
    type_a_cell.setBasePrice(0.98)

    type_b_cell = euro_to_usd.getCell(
      'currency_exchange_type/type_b',
      'resource/%s' % euro.getRelativeUrl(),
      'price_currency/%s' % usd.getRelativeUrl(),
      base_id='path')
    type_b_cell.setBasePrice(1.24)

    transaction.commit()
    self.tic()

    # we need a base for asContext, we use the currency, but in real code you
    # might want to use a more meaningful context.
    context = euro.asContext(
                    categories=['resource/%s' % euro.getRelativeUrl(),
                                'price_currency/%s' % usd.getRelativeUrl(),
                                'currency_exchange_type/type_a'])

    exchange_ratio = euro.getPrice(context=context,
                                   portal_type='Currency Exchange Cell')
    self.assertEquals(0.98, exchange_ratio)

599 600 601

def test_suite():
  suite = unittest.TestSuite()
Jérome Perrin's avatar
Jérome Perrin committed
602
  suite.addTest(unittest.makeSuite(TestCurrencyExchangeLine))
603
  suite.addTest(unittest.makeSuite(TestCurrencyExchangeCell))
Jérome Perrin's avatar
Jérome Perrin committed
604
  return suite