[IMP] tools: float_utils, precision rounding must be positive

Adding a new rule to verify that precision_rounding is positive.

Before this commit:
when a precision_round was 0 or smaller than 0 the float_utils
functions gave as results:
* float_is_zero(0.0, precision_rounding=0.0) -> False
* float_round(1.25, precision_rounding=0.0) -> 0.0
* float_compare(1.0, 1.0, precision_rounding=0.0) -> 1

These results where at least not correct at worst not logic.

Now, the function raises an error when the precision_rounding
is not positive
This commit is contained in:
Jorge Pinna Puissant
2019-02-13 07:05:13 +00:00
parent 73fa0748da
commit e270e9e0cc
3 changed files with 25 additions and 6 deletions
+3 -5
View File
@@ -134,11 +134,7 @@ class Currency(models.Model):
:param float amount: the amount to round
:return: rounded float
"""
# TODO: Need to check why it calls round() from sale.py, _amount_all() with *No* ID after below commits,
# https://github.com/odoo/odoo/commit/36ee1ad813204dcb91e9f5f20d746dff6f080ac2
# https://github.com/odoo/odoo/commit/0b6058c585d7d9a57bd7581b8211f20fca3ec3f7
# Removing self.ensure_one() will make few test cases to break of modules event_sale, sale_mrp and stock_dropshipping.
#self.ensure_one()
self.ensure_one()
return tools.float_round(amount, precision_rounding=self.rounding)
@api.multi
@@ -162,6 +158,7 @@ class Currency(models.Model):
With the new API, call it like: ``currency.compare_amounts(amount1, amount2)``.
"""
self.ensure_one()
return tools.float_compare(amount1, amount2, precision_rounding=self.rounding)
@api.multi
@@ -177,6 +174,7 @@ class Currency(models.Model):
With the new API, call it like: ``currency.is_zero(amount)``.
"""
self.ensure_one()
return tools.float_is_zero(amount, precision_rounding=self.rounding)
@api.model
+18
View File
@@ -201,12 +201,30 @@ class TestFloatPrecision(TransactionCase):
with self.assertRaises(AssertionError):
float_is_zero(0.01, precision_digits=3, precision_rounding=0.01)
with self.assertRaises(AssertionError):
float_is_zero(0.0, precision_rounding=0.0)
with self.assertRaises(AssertionError):
float_is_zero(0.0, precision_rounding=-0.1)
with self.assertRaises(AssertionError):
float_compare(0.01, 0.02, precision_digits=3, precision_rounding=0.01)
with self.assertRaises(AssertionError):
float_compare(1.0, 1.0, precision_rounding=0.0)
with self.assertRaises(AssertionError):
float_compare(1.0, 1.0, precision_rounding=-0.1)
with self.assertRaises(AssertionError):
float_round(0.01, precision_digits=3, precision_rounding=0.01)
with self.assertRaises(AssertionError):
float_round(1.25, precision_rounding=0.0)
with self.assertRaises(AssertionError):
float_round(1.25, precision_rounding=-0.1)
def test_amount_to_text_10(self):
""" verify that amount_to_text works as expected """
currency = self.env.ref('base.EUR')
+4 -1
View File
@@ -26,6 +26,8 @@ def _float_check_precision(precision_digits=None, precision_rounding=None):
assert (precision_digits is not None or precision_rounding is not None) and \
not (precision_digits and precision_rounding),\
"exactly one of precision_digits and precision_rounding must be specified"
assert precision_rounding is None or precision_rounding > 0,\
"precision_rounding must be positive, got %s" % precision_rounding
if precision_digits is not None:
return 10 ** -precision_digits
return precision_rounding
@@ -51,7 +53,8 @@ def float_round(value, precision_digits=None, precision_rounding=None, rounding_
"""
rounding_factor = _float_check_precision(precision_digits=precision_digits,
precision_rounding=precision_rounding)
if rounding_factor == 0 or value == 0: return 0.0
if rounding_factor == 0 or value == 0:
return 0.0
# NORMALIZE - ROUND - DENORMALIZE
# In order to easily support rounding to arbitrary 'steps' (e.g. coin values),