2009-06-26 16:17:33 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
#This file is part of Tryton. The COPYRIGHT file at the top level of
|
|
|
|
|
#this repository contains the full copyright notices and license terms.
|
2012-04-03 20:07:00 +02:00
|
|
|
import sys
|
|
|
|
|
import os
|
2009-06-26 16:17:33 +02:00
|
|
|
DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
|
|
|
|
|
'..', '..', '..', '..', '..', 'trytond')))
|
|
|
|
|
if os.path.isdir(DIR):
|
|
|
|
|
sys.path.insert(0, os.path.dirname(DIR))
|
|
|
|
|
|
|
|
|
|
import unittest
|
2009-09-03 12:05:10 +02:00
|
|
|
from decimal import Decimal
|
2010-08-10 11:58:58 -04:00
|
|
|
import trytond.tests.test_tryton
|
2011-08-04 16:57:28 +02:00
|
|
|
from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT, test_view,\
|
|
|
|
|
test_depends
|
2010-08-10 11:58:58 -04:00
|
|
|
from trytond.transaction import Transaction
|
2009-06-26 16:17:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class CurrencyTestCase(unittest.TestCase):
|
|
|
|
|
'''
|
|
|
|
|
Test Currency module.
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
|
trytond.tests.test_tryton.install_module('currency')
|
2010-03-04 14:09:16 +01:00
|
|
|
self.rate = POOL.get('currency.currency.rate')
|
|
|
|
|
self.currency = POOL.get('currency.currency')
|
|
|
|
|
self.date = POOL.get('ir.date')
|
|
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
def get_currency(self, code):
|
|
|
|
|
return self.currency.search([
|
|
|
|
|
('code', '=', code),
|
|
|
|
|
], limit=1)[0]
|
2009-06-26 16:17:33 +02:00
|
|
|
|
|
|
|
|
def test0005views(self):
|
|
|
|
|
'''
|
|
|
|
|
Test views.
|
|
|
|
|
'''
|
2010-08-27 12:18:09 +02:00
|
|
|
test_view('currency')
|
2009-06-26 16:17:33 +02:00
|
|
|
|
2011-08-04 16:57:28 +02:00
|
|
|
def test0006depends(self):
|
|
|
|
|
'''
|
|
|
|
|
Test depends.
|
|
|
|
|
'''
|
|
|
|
|
test_depends()
|
|
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0010currencies(self):
|
|
|
|
|
'''
|
|
|
|
|
Create currencies
|
|
|
|
|
'''
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2013-01-06 22:18:13 +01:00
|
|
|
cu1, cu2 = self.currency.create([{
|
|
|
|
|
'name': 'cu1',
|
|
|
|
|
'symbol': 'cu1',
|
|
|
|
|
'code': 'cu1'
|
|
|
|
|
}, {
|
|
|
|
|
'name': 'cu2',
|
|
|
|
|
'symbol': 'cu2',
|
|
|
|
|
'code': 'cu2'
|
|
|
|
|
}])
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assert_(cu1)
|
|
|
|
|
self.assert_(cu2)
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.commit()
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0020mon_grouping(self):
|
|
|
|
|
'''
|
|
|
|
|
Check grouping
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER, context=CONTEXT):
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assertRaises(Exception, self.currency.write, [cu1],
|
2010-08-10 11:58:58 -04:00
|
|
|
{'mon_grouping': ''})
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assertRaises(Exception, self.currency.write, [cu1],
|
2010-08-10 11:58:58 -04:00
|
|
|
{'mon_grouping': '[a]'})
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assertRaises(Exception, self.currency.write, [cu1],
|
2010-08-10 11:58:58 -04:00
|
|
|
{'mon_grouping': '[1,"a"]'})
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assertRaises(Exception, self.currency.write, [cu1],
|
2010-08-10 11:58:58 -04:00
|
|
|
{'mon_grouping': '[1,"1"]'})
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0030rate(self):
|
|
|
|
|
'''
|
|
|
|
|
Create rates.
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2013-01-06 22:18:13 +01:00
|
|
|
rate1, rate2 = self.rate.create([{
|
|
|
|
|
'rate': Decimal("1.3"),
|
|
|
|
|
'currency': cu1.id,
|
|
|
|
|
}, {
|
|
|
|
|
'rate': Decimal("1"),
|
|
|
|
|
'currency': cu2.id,
|
|
|
|
|
}])
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assert_(rate1)
|
|
|
|
|
self.assert_(rate2)
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
self.assertEqual(cu1.rate, Decimal("1.3"))
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.commit()
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0040rate_unicity(self):
|
|
|
|
|
'''
|
|
|
|
|
Rate unicity
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2010-08-10 11:58:58 -04:00
|
|
|
today = self.date.today()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2013-01-06 22:18:13 +01:00
|
|
|
cu, = self.currency.create([{
|
|
|
|
|
'name': 'cu',
|
|
|
|
|
'symbol': 'cu',
|
|
|
|
|
'code': 'cu'
|
|
|
|
|
}])
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2013-01-06 22:18:13 +01:00
|
|
|
self.rate.create([{
|
|
|
|
|
'rate': Decimal("1.3"),
|
|
|
|
|
'currency': cu.id,
|
|
|
|
|
'date': today,
|
|
|
|
|
}])
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
self.assertRaises(Exception, self.rate.create, {
|
|
|
|
|
'rate': Decimal("1.3"),
|
2012-09-24 22:17:53 +02:00
|
|
|
'currency': cu.id,
|
2010-08-10 11:58:58 -04:00
|
|
|
'date': today,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
transaction.cursor.rollback()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
|
|
|
|
def test0050compute_simple(self):
|
|
|
|
|
'''
|
|
|
|
|
Simple conversion
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
expected = Decimal("13")
|
|
|
|
|
converted_amount = self.currency.compute(
|
2012-09-24 22:17:53 +02:00
|
|
|
cu2, amount, cu1, True)
|
2010-08-10 11:58:58 -04:00
|
|
|
self.assertEqual(converted_amount, expected)
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.commit()
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0060compute_nonfinite(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion with rounding on non-finite decimal representation
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER, context=CONTEXT):
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
expected = Decimal("7.69")
|
|
|
|
|
converted_amount = self.currency.compute(
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, amount, cu2, True)
|
|
|
|
|
self.assertEqual(converted_amount, expected)
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0070compute_nonfinite_worounding(self):
|
|
|
|
|
'''
|
|
|
|
|
Same without rounding
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER, context=CONTEXT):
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
expected = Decimal('7.692307692307692307692307692')
|
|
|
|
|
converted_amount = self.currency.compute(
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, amount, cu2, False)
|
|
|
|
|
self.assertEqual(converted_amount, expected)
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0080compute_same(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion to the same currency
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER, context=CONTEXT):
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
converted_amount = self.currency.compute(
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, amount, cu1, True)
|
|
|
|
|
self.assertEqual(converted_amount, amount)
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0090compute_zeroamount(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion with zero amount
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER, context=CONTEXT):
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2011-11-02 16:09:39 +01:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
expected = Decimal("0")
|
|
|
|
|
converted_amount = self.currency.compute(
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, Decimal("0"), cu2, True)
|
|
|
|
|
self.assertEqual(converted_amount, expected)
|
2010-03-12 12:24:01 +01:00
|
|
|
|
2009-09-03 12:05:10 +02:00
|
|
|
def test0100compute_zerorate(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion with zero rate
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
|
|
|
|
cu2 = self.get_currency('cu2')
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
rates = self.rate.search([
|
|
|
|
|
('currency', '=', cu1.id),
|
2010-08-10 11:58:58 -04:00
|
|
|
], 0, 1, None)
|
2012-09-24 22:17:53 +02:00
|
|
|
self.rate.write(rates, {
|
2010-08-10 11:58:58 -04:00
|
|
|
'rate': Decimal("0"),
|
|
|
|
|
})
|
|
|
|
|
amount = Decimal("10")
|
|
|
|
|
self.assertRaises(Exception, self.currency.compute,
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, amount, cu2, True)
|
2010-08-10 11:58:58 -04:00
|
|
|
self.assertRaises(Exception, self.currency.compute,
|
2012-09-24 22:17:53 +02:00
|
|
|
cu2, amount, cu1, True)
|
2009-09-03 12:05:10 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.rollback()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
|
|
|
|
def test0110compute_missingrate(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion with missing rate
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1 = self.get_currency('cu1')
|
2013-01-06 22:18:13 +01:00
|
|
|
cu3, = self.currency.create([{
|
|
|
|
|
'name': 'cu3',
|
|
|
|
|
'symbol': 'cu3',
|
|
|
|
|
'code': 'cu3'
|
|
|
|
|
}])
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
self.assertRaises(Exception, self.currency.compute,
|
2012-09-24 22:17:53 +02:00
|
|
|
cu3, amount, cu1, True)
|
2011-10-28 10:50:24 +02:00
|
|
|
self.assertRaises(Exception, self.currency.compute,
|
2012-09-24 22:17:53 +02:00
|
|
|
cu1, amount, cu3, True)
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.rollback()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
|
|
|
|
def test0120compute_bothmissingrate(self):
|
|
|
|
|
'''
|
|
|
|
|
Conversion with both missing rate
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2013-01-06 22:18:13 +01:00
|
|
|
cu3, cu4 = self.currency.create([{
|
|
|
|
|
'name': 'cu3',
|
|
|
|
|
'symbol': 'cu3',
|
|
|
|
|
'code': 'cu3'
|
|
|
|
|
}, {
|
|
|
|
|
'name': 'cu4',
|
|
|
|
|
'symbol': 'cu4',
|
|
|
|
|
'code': 'cu4'
|
|
|
|
|
}])
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
amount = Decimal("10")
|
|
|
|
|
self.assertRaises(Exception, self.currency.compute,
|
2012-09-24 22:17:53 +02:00
|
|
|
cu3, amount, cu4, True)
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.rollback()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
|
|
|
|
def test0130delete_cascade(self):
|
|
|
|
|
'''
|
|
|
|
|
Test deletion of currency deletes rates
|
|
|
|
|
'''
|
2011-11-28 14:45:33 +01:00
|
|
|
with Transaction().start(DB_NAME, USER,
|
|
|
|
|
context=CONTEXT) as transaction:
|
2010-08-10 11:58:58 -04:00
|
|
|
codes = ['cu%s' % (i + 1) for i in range(2)]
|
2012-09-24 22:17:53 +02:00
|
|
|
currencies = [self.get_currency(i) for i in codes]
|
|
|
|
|
self.currency.delete(currencies)
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2012-09-24 22:17:53 +02:00
|
|
|
rates = self.rate.search([(
|
|
|
|
|
'currency', 'in', map(int, currencies),
|
|
|
|
|
)], 0, None, None)
|
|
|
|
|
self.assertFalse(rates)
|
2011-10-28 10:50:24 +02:00
|
|
|
|
2010-08-10 11:58:58 -04:00
|
|
|
transaction.cursor.rollback()
|
2009-09-03 12:05:10 +02:00
|
|
|
|
|
|
|
|
|
2009-06-26 16:17:33 +02:00
|
|
|
def suite():
|
2010-03-04 14:09:16 +01:00
|
|
|
suite = trytond.tests.test_tryton.suite()
|
2012-04-03 20:07:00 +02:00
|
|
|
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
|
|
|
|
|
CurrencyTestCase))
|
2010-03-04 14:09:16 +01:00
|
|
|
return suite
|
2009-06-26 16:17:33 +02:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2010-03-04 14:09:16 +01:00
|
|
|
unittest.TextTestRunner(verbosity=2).run(suite())
|