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

        issue10180
        review342071007
        (grafted from 5037451db47e33765e668c3496dfb54dc4b648e3)
diffstat:

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

diffs (112 lines):

diff -r 83f1c5c3ec48 -r 8d5819d64900 currency.py
--- a/currency.py       Fri Jan 01 17:02:40 2021 +0100
+++ b/currency.py       Fri Apr 16 18:59:08 2021 +0200
@@ -145,6 +145,8 @@
 
     @classmethod
     def _round(cls, amount, factor, rounding):
+        if not factor:
+            return amount
         with localcontext() as ctx:
             ctx.prec = max(ctx.prec, (amount / factor).adjusted() + 1)
             # Divide and multiple by factor for case factor is not 10En
@@ -154,7 +156,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 83f1c5c3ec48 -r 8d5819d64900 tests/test_currency.py
--- a/tests/test_currency.py    Fri Jan 01 17:02:40 2021 +0100
+++ b/tests/test_currency.py    Fri Apr 16 18:59:08 2021 +0200
@@ -120,6 +120,85 @@
         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