]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Add missing amount operations
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Feb 2018 01:13:17 +0000 (02:13 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Mon, 12 Feb 2018 08:02:13 +0000 (09:02 +0100)
portfolio.py
test.py

index 17a17ba51e3c1672d4d174c1ba2f7872439bca4a..21a98342f15fa03ba7753be2a69579bda16cd7c9 100644 (file)
@@ -124,6 +124,8 @@ class Amount:
         return Amount(self.currency, abs(self.value))
 
     def __add__(self, other):
+        if other == 0:
+            return self
         if other.currency != self.currency and other.value * self.value != 0:
             raise Exception("Summing amounts must be done with same currencies")
         return Amount(self.currency, self.value + other.value)
@@ -141,6 +143,12 @@ class Amount:
             raise Exception("Summing amounts must be done with same currencies")
         return Amount(self.currency, self.value - other.value)
 
+    def __rsub__(self, other):
+        if other == 0:
+            return -self
+        else:
+            return -self.__sub__(other)
+
     def __mul__(self, value):
         if not isinstance(value, (int, float, D)):
             raise TypeError("Amount may only be multiplied by numbers")
diff --git a/test.py b/test.py
index 19655213cb8a71e54dd3925ba1c13090f37904ad..c496e11a916adb2aaeb0a371fb4695ae8b4fb5a7 100644 (file)
--- a/test.py
+++ b/test.py
@@ -220,6 +220,8 @@ class AmountTest(WebMockTestCase):
         amount4 = portfolio.Amount("ETH", 0.0)
         self.assertEqual(amount1, amount1 + amount4)
 
+        self.assertEqual(amount1, amount1 + 0)
+
     def test__radd(self):
         amount = portfolio.Amount("XVG", "12.9")
 
@@ -241,6 +243,13 @@ class AmountTest(WebMockTestCase):
         amount4 = portfolio.Amount("ETH", 0.0)
         self.assertEqual(amount1, amount1 - amount4)
 
+    def test__rsub(self):
+        amount = portfolio.Amount("ETH", "1.6")
+        with self.assertRaises(Exception):
+            3 - amount
+
+        self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount)
+
     def test__mul(self):
         amount = portfolio.Amount("XEM", 11)