diff options
-rw-r--r-- | portfolio.py | 8 | ||||
-rw-r--r-- | test.py | 9 |
2 files changed, 17 insertions, 0 deletions
diff --git a/portfolio.py b/portfolio.py index 17a17ba..21a9834 100644 --- a/portfolio.py +++ b/portfolio.py | |||
@@ -124,6 +124,8 @@ class Amount: | |||
124 | return Amount(self.currency, abs(self.value)) | 124 | return Amount(self.currency, abs(self.value)) |
125 | 125 | ||
126 | def __add__(self, other): | 126 | def __add__(self, other): |
127 | if other == 0: | ||
128 | return self | ||
127 | if other.currency != self.currency and other.value * self.value != 0: | 129 | if other.currency != self.currency and other.value * self.value != 0: |
128 | raise Exception("Summing amounts must be done with same currencies") | 130 | raise Exception("Summing amounts must be done with same currencies") |
129 | return Amount(self.currency, self.value + other.value) | 131 | return Amount(self.currency, self.value + other.value) |
@@ -141,6 +143,12 @@ class Amount: | |||
141 | raise Exception("Summing amounts must be done with same currencies") | 143 | raise Exception("Summing amounts must be done with same currencies") |
142 | return Amount(self.currency, self.value - other.value) | 144 | return Amount(self.currency, self.value - other.value) |
143 | 145 | ||
146 | def __rsub__(self, other): | ||
147 | if other == 0: | ||
148 | return -self | ||
149 | else: | ||
150 | return -self.__sub__(other) | ||
151 | |||
144 | def __mul__(self, value): | 152 | def __mul__(self, value): |
145 | if not isinstance(value, (int, float, D)): | 153 | if not isinstance(value, (int, float, D)): |
146 | raise TypeError("Amount may only be multiplied by numbers") | 154 | raise TypeError("Amount may only be multiplied by numbers") |
@@ -220,6 +220,8 @@ class AmountTest(WebMockTestCase): | |||
220 | amount4 = portfolio.Amount("ETH", 0.0) | 220 | amount4 = portfolio.Amount("ETH", 0.0) |
221 | self.assertEqual(amount1, amount1 + amount4) | 221 | self.assertEqual(amount1, amount1 + amount4) |
222 | 222 | ||
223 | self.assertEqual(amount1, amount1 + 0) | ||
224 | |||
223 | def test__radd(self): | 225 | def test__radd(self): |
224 | amount = portfolio.Amount("XVG", "12.9") | 226 | amount = portfolio.Amount("XVG", "12.9") |
225 | 227 | ||
@@ -241,6 +243,13 @@ class AmountTest(WebMockTestCase): | |||
241 | amount4 = portfolio.Amount("ETH", 0.0) | 243 | amount4 = portfolio.Amount("ETH", 0.0) |
242 | self.assertEqual(amount1, amount1 - amount4) | 244 | self.assertEqual(amount1, amount1 - amount4) |
243 | 245 | ||
246 | def test__rsub(self): | ||
247 | amount = portfolio.Amount("ETH", "1.6") | ||
248 | with self.assertRaises(Exception): | ||
249 | 3 - amount | ||
250 | |||
251 | self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount) | ||
252 | |||
244 | def test__mul(self): | 253 | def test__mul(self): |
245 | amount = portfolio.Amount("XEM", 11) | 254 | amount = portfolio.Amount("XEM", 11) |
246 | 255 | ||