summaryrefslogtreecommitdiff
path: root/odoo/addons/test_new_api/tests
diff options
context:
space:
mode:
authorChristophe Simonis <chs@odoo.com>2017-12-05 16:46:14 +0100
committerChristophe Simonis <chs@odoo.com>2017-12-05 16:46:14 +0100
commitb8540eefe3b82fc4a54c8a808ce3bfa0bea8a29a (patch)
treef6c631c860d1decc712be66dcb0bfbcd186d8872 /odoo/addons/test_new_api/tests
parent519555054a3a0e2e0b8a139518da081c4d5e80aa (diff)
parent970be94f3763e40a75885f93840e2be40572fc54 (diff)
[MERGE] forward port branch saas-15 up to 970be94f376
Diffstat (limited to 'odoo/addons/test_new_api/tests')
-rw-r--r--odoo/addons/test_new_api/tests/test_new_fields.py72
1 files changed, 58 insertions, 14 deletions
diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py
index 405d5ed9ef9..ad2d1b97d3c 100644
--- a/odoo/addons/test_new_api/tests/test_new_fields.py
+++ b/odoo/addons/test_new_api/tests/test_new_fields.py
@@ -5,7 +5,7 @@ from datetime import date, datetime
from odoo.exceptions import AccessError, except_orm
from odoo.tests import common
-from odoo.tools import mute_logger
+from odoo.tools import mute_logger, float_repr
class TestFields(common.TransactionCase):
@@ -305,21 +305,65 @@ class TestFields(common.TransactionCase):
record.number = 2.4999999999999996
self.assertEqual(record.number, 2.50)
+ def check_monetary(self, record, amount, currency, msg=None):
+ # determine the possible roundings of amount
+ if currency:
+ ramount = currency.round(amount)
+ samount = float(float_repr(ramount, currency.decimal_places))
+ else:
+ ramount = samount = amount
+
+ # check the currency on record
+ self.assertEqual(record.currency_id, currency)
+
+ # check the value on the record
+ self.assertIn(record.amount, [ramount, samount], msg)
+
+ # check the value in the database
+ self.cr.execute('SELECT amount FROM test_new_api_mixed WHERE id=%s', [record.id])
+ value = self.cr.fetchone()[0]
+ self.assertEqual(value, samount, msg)
+
def test_20_monetary(self):
""" test monetary fields """
- record = self.env['test_new_api.mixed'].create({})
- self.assertTrue(record.currency_id)
- self.assertEqual(record.currency_id.rounding, 0.01)
-
- # the conversion to cache should round the value to 14.700000000000001
- record.amount = 14.7
- self.assertNotEqual(record.amount, 14.7)
- self.assertEqual(record.amount, 14.700000000000001)
-
- # however when stored to database, it should be serialized as 14.70
- self.cr.execute('SELECT amount FROM test_new_api_mixed WHERE id=%s', (record.id,))
- (amount,) = self.cr.fetchone()
- self.assertEqual(amount, 14.7)
+ model = self.env['test_new_api.mixed']
+ currency = self.env['res.currency'].with_context(active_test=False)
+ amount = 14.70126
+
+ for rounding in [0.01, 0.0001, 1.0, 0]:
+ # first retrieve a currency corresponding to rounding
+ if rounding:
+ currency = currency.search([('rounding', '=', rounding)], limit=1)
+ self.assertTrue(currency, "No currency found for rounding %s" % rounding)
+ else:
+ # rounding=0 corresponds to currency=False
+ currency = currency.browse()
+
+ # case 1: create with amount and currency
+ record = model.create({'amount': amount, 'currency_id': currency.id})
+ self.check_monetary(record, amount, currency, 'create(amount, currency)')
+
+ # case 2: assign amount
+ record.amount = 0
+ record.amount = amount
+ self.check_monetary(record, amount, currency, 'assign(amount)')
+
+ # case 3: write with amount and currency
+ record.write({'amount': 0, 'currency_id': False})
+ record.write({'amount': amount, 'currency_id': currency.id})
+ self.check_monetary(record, amount, currency, 'write(amount, currency)')
+
+ # case 4: write with amount only
+ record.write({'amount': 0})
+ record.write({'amount': amount})
+ self.check_monetary(record, amount, currency, 'write(amount)')
+
+ # case 5: write with amount on several records
+ records = record + model.create({'currency_id': currency.id})
+ records.write({'amount': 0})
+ records.write({'amount': amount})
+ for record in records:
+ self.check_monetary(record, amount, currency, 'multi write(amount)')
def test_21_date(self):
""" test date fields """