From 00bc7c52e2fcc97ee4440bc659012678923ba922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 13 Sep 2021 23:07:13 +0200 Subject: [PATCH] Support integer as price to round issue10720 review375381002 (grafted from 9780dfb370fae591f5a85342e26b4278773267cf) --HG-- branch : 6.0 --- product.py | 2 ++ tests/test_product.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/product.py b/product.py index 8f9b174290..40be6b3a98 100644 --- a/product.py +++ b/product.py @@ -44,6 +44,8 @@ price_digits = (16, price_decimal) def round_price(value, rounding=None): "Round price using the price digits" + if isinstance(value, int): + return Decimal(value) return value.quantize( Decimal(1) / 10 ** price_digits[1], rounding=rounding) diff --git a/tests/test_product.py b/tests/test_product.py index a897faa835..1a163ba109 100644 --- a/tests/test_product.py +++ b/tests/test_product.py @@ -12,6 +12,8 @@ from trytond.pool import Pool from trytond.modules.company.tests import CompanyTestMixin +from ..product import round_price + class ProductTestCase(CompanyTestMixin, ModuleTestCase): 'Test Product module' @@ -446,6 +448,15 @@ class ProductTestCase(CompanyTestMixin, ModuleTestCase): [('name', 'like', '%')], order=[('type', 'DESC')]), [product2, product1]) + def test_round_price(self): + for value, result in [ + (Decimal('1'), Decimal('1.0000')), + (Decimal('1.12345'), Decimal('1.1234')), + (1, Decimal('1')), + ]: + with self.subTest(value=value): + self.assertEqual(round_price(value), result) + def suite(): suite = trytond.tests.test_tryton.suite()