diff options
| author | Jorge Pinna Puissant <jpp@odoo.com> | 2019-01-30 09:12:18 +0000 |
|---|---|---|
| committer | Jorge Pinna Puissant <jpp@odoo.com> | 2019-02-13 07:05:13 +0000 |
| commit | e270e9e0cc6574d2c6bdd29983d3b66d12d2ced7 (patch) | |
| tree | 82e1303f90932a05086a600c1b4d38cbb2fea23c | |
| parent | 73fa0748da28ac475520aa390459ace55fe076c1 (diff) | |
[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
| -rw-r--r-- | odoo/addons/base/models/res_currency.py | 8 | ||||
| -rw-r--r-- | odoo/addons/base/tests/test_float.py | 18 | ||||
| -rw-r--r-- | odoo/tools/float_utils.py | 5 |
3 files changed, 25 insertions, 6 deletions
diff --git a/odoo/addons/base/models/res_currency.py b/odoo/addons/base/models/res_currency.py index e3624000a0e..c75ddfe0021 100644 --- a/odoo/addons/base/models/res_currency.py +++ b/odoo/addons/base/models/res_currency.py @@ -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 diff --git a/odoo/addons/base/tests/test_float.py b/odoo/addons/base/tests/test_float.py index 4447b9de1c0..4dbf1f5f139 100644 --- a/odoo/addons/base/tests/test_float.py +++ b/odoo/addons/base/tests/test_float.py @@ -202,11 +202,29 @@ class TestFloatPrecision(TransactionCase): 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') diff --git a/odoo/tools/float_utils.py b/odoo/tools/float_utils.py index 12881503f32..47b5d72c069 100644 --- a/odoo/tools/float_utils.py +++ b/odoo/tools/float_utils.py @@ -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), |
