[FIX] fields.float: always round floats when persisting

Added test to try a full round-trip to
the database as well to make sure the
automatic rounding is working properly.

lp bug: https://launchpad.net/bugs/882036 fixed

bzr revid: odo@openerp.com-20111221011100-gybt9pjdfpg4wc86
This commit is contained in:
Olivier Dony
2011-12-21 02:11:00 +01:00
parent 68e03147ff
commit e410b81cc4
2 changed files with 31 additions and 6 deletions
+20
View File
@@ -267,6 +267,26 @@
try_round(457.5, '460.00', precision_rounding=5)
try_round(457.1, '456.00', precision_rounding=3)
-
"Float precision tests: check that proper rounding is performed for float persistence"
-
!python {model: res.currency}: |
currency = self.browse(cr, uid, ref('base.EUR'))
res_currency_rate = self.pool.get('res.currency.rate')
from tools import float_compare, float_is_zero, float_round, float_repr
def try_roundtrip(value, expected, self=self, cr=cr, currency=currency,
res_currency_rate=res_currency_rate):
rate_id = res_currency_rate.create(cr, 1, {'name':'2000-01-01',
'rate': value,
'currency_id': currency.id})
rate = res_currency_rate.read(cr, 1, rate_id, ['rate'])['rate']
assert rate == expected, 'Roundtrip error: got %s back from db, expected %s' % (rate, expected)
# res.currency.rate uses 6 digits of precision by default
try_roundtrip(2.6748955, 2.674896)
try_roundtrip(-2.6748955, -2.674896)
try_roundtrip(10000.999999, 10000.999999)
try_roundtrip(-10000.999999, -10000.999999)
-
"Float precision tests: verify that invalid parameters are forbidden"
-
+11 -6
View File
@@ -239,8 +239,10 @@ class float(_column):
def digits_change(self, cr):
if self.digits_compute:
self.digits = self.digits_compute(cr)
if self.digits:
precision, scale = self.digits
self._symbol_set = ('%s', lambda x: float_repr(float_round(__builtin__.float(x or 0.0), precision_digits=scale),
self._symbol_set = ('%s', lambda x: float_repr(float_round(__builtin__.float(x or 0.0),
precision_digits=scale),
precision_digits=scale))
class date(_column):
@@ -992,11 +994,14 @@ class function(_column):
self._symbol_set = integer._symbol_set
def digits_change(self, cr):
if self.digits_compute:
self.digits = self.digits_compute(cr)
precision, scale = self.digits
self._symbol_set = ('%s', lambda x: float_repr(float_round(__builtin__.float(x or 0.0), precision_digits=scale),
precision_digits=scale))
if self._type == 'float':
if self.digits_compute:
self.digits = self.digits_compute(cr)
if self.digits:
precision, scale = self.digits
self._symbol_set = ('%s', lambda x: float_repr(float_round(__builtin__.float(x or 0.0),
precision_digits=scale),
precision_digits=scale))
def search(self, cr, uid, obj, name, args, context=None):
if not self._fnct_search: