changeset be4f875f1d2e in modules/currency:5.0
details: https://hg.tryton.org/modules/currency?cmd=changeset&node=be4f875f1d2e
description:
        Manage negative and zero rounding

        issue10180
        review342071007
        (grafted from 5037451db47e33765e668c3496dfb54dc4b648e3)
diffstat:

 currency.py            |    6 ++-
 tests/test_currency.py |  112 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 117 insertions(+), 1 deletions(-)

diffs (145 lines):

diff -r 0895ec9a64b5 -r be4f875f1d2e currency.py
--- a/currency.py       Sat Apr 04 17:52:57 2020 +0200
+++ b/currency.py       Fri Apr 16 18:59:08 2021 +0200
@@ -136,6 +136,8 @@
 
     def round(self, amount, rounding=ROUND_HALF_EVEN):
         'Round the amount depending of the currency'
+        if not self.rounding:
+            return amount
         with localcontext() as ctx:
             ctx.prec = max(ctx.prec, (amount / self.rounding).adjusted() + 1)
             # Divide and multiple by rounding for case rounding is not 10En
@@ -145,7 +147,9 @@
 
     def is_zero(self, amount):
         'Return True if the amount can be considered as zero for the currency'
-        return abs(self.round(amount)) < self.rounding
+        if not self.rounding:
+            return not amount
+        return abs(self.round(amount)) < abs(self.rounding)
 
     @classmethod
     def compute(cls, from_currency, amount, to_currency, round=True):
diff -r 0895ec9a64b5 -r be4f875f1d2e tests/test_currency.py
--- a/tests/test_currency.py    Sat Apr 04 17:52:57 2020 +0200
+++ b/tests/test_currency.py    Fri Apr 16 18:59:08 2021 +0200
@@ -87,6 +87,118 @@
                 })
 
     @with_transaction()
+    def test_round(self):
+        "Test simple round"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('0.001')
+        cu.digits = 3
+        cu.save()
+
+        rounded = cu.round(Decimal('1.2345678'))
+
+        self.assertEqual(rounded, Decimal('1.235'))
+
+    @with_transaction()
+    def test_round_non_unity(self):
+        "Test round with non unity"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('0.02')
+        cu.digits = 2
+        cu.save()
+
+        rounded = cu.round(Decimal('1.2345'))
+
+        self.assertEqual(rounded, Decimal('1.24'))
+
+    @with_transaction()
+    def test_round_big_number(self):
+        "Test rounding big number"
+        cu = create_currency('cu')
+
+        rounded = cu.round(Decimal('1E50'))
+
+        self.assertEqual(rounded, Decimal('1E50'))
+
+    @with_transaction()
+    def test_round_negative(self):
+        "Test rounding with negative rounding"
+        cu = create_currency('cu')
+        cu.rounding = -Decimal('0.1')
+        cu.digits = 1
+        cu.save()
+
+        rounded = cu.round(Decimal('1.23'))
+
+        self.assertEqual(rounded, Decimal('1.2'))
+
+    @with_transaction()
+    def test_round_zero(self):
+        "Test rounding with 0 as rounding"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('0')
+        cu.save()
+
+        rounded = cu.round(Decimal('1.2345'))
+
+        self.assertEqual(rounded, Decimal('1.2345'))
+
+    @with_transaction()
+    def test_is_zero(self):
+        "Test is zero"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('0.001')
+        cu.digits = 3
+        cu.save()
+
+        for value, result in [
+                (Decimal('0'), True),
+                (Decimal('0.0002'), True),
+                (Decimal('0.0009'), False),
+                (Decimal('0.002'), False),
+                ]:
+            with self.subTest(value=value):
+                self.assertEqual(cu.is_zero(value), result)
+            with self.subTest(value=-value):
+                self.assertEqual(cu.is_zero(-value), result)
+
+    @with_transaction()
+    def test_is_zero_negative(self):
+        "Test is zero with negative rounding"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('-0.001')
+        cu.digits = 3
+        cu.save()
+
+        for value, result in [
+                (Decimal('0'), True),
+                (Decimal('0.0002'), True),
+                (Decimal('0.0009'), False),
+                (Decimal('0.002'), False),
+                ]:
+            with self.subTest(value=value):
+                self.assertEqual(cu.is_zero(value), result)
+            with self.subTest(value=-value):
+                self.assertEqual(cu.is_zero(-value), result)
+
+    @with_transaction()
+    def test_is_zero_zero(self):
+        "Test is zero with 0 as rounding"
+        cu = create_currency('cu')
+        cu.rounding = Decimal('0')
+        cu.save()
+
+        for value, result in [
+                (Decimal('0'), True),
+                (Decimal('0.0002'), False),
+                (Decimal('0.0009'), False),
+                (Decimal('0.002'), False),
+                ]:
+            with self.subTest(value=value):
+                self.assertEqual(cu.is_zero(value), result)
+            with self.subTest(value=-value):
+                self.assertEqual(cu.is_zero(-value), result)
+
+    @with_transaction()
     def test_compute_simple(self):
         'Simple conversion'
         pool = Pool()

Reply via email to