diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index c692cf3..a879299 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -67,6 +67,11 @@ class Balance(Mapping[str, data.Amount]): amounts.sort(key=lambda amt: abs(amt.number), reverse=True) return ', '.join(str(amount) for amount in amounts) + def __neg__(self) -> 'Balance': + return type(self)( + (key, -amt) for key, amt in self.items() + ) + def __getitem__(self, key: str) -> data.Amount: return data.Amount(self._currency_map[key], key) diff --git a/tests/test_reports_balance.py b/tests/test_reports_balance.py index f3af572..bdba8ab 100644 --- a/tests/test_reports_balance.py +++ b/tests/test_reports_balance.py @@ -67,6 +67,22 @@ def test_mixed_balance(): assert not balance.is_zero() assert all(balance[key] == amt for key, amt in amounts.items()) +@pytest.mark.parametrize('balance_map_kwargs', [ + {}, + {'USD': 0}, + {'EUR': 10}, + {'JPY': 20, 'BRL': 30}, + {'EUR': -15}, + {'JPY': -25, 'BRL': -35}, + {'JPY': 40, 'USD': 0, 'EUR': -50}, +]) +def test_neg(balance_map_kwargs): + amounts = testutil.balance_map(**balance_map_kwargs) + actual = -core.Balance(amounts.items()) + assert set(actual) == set(balance_map_kwargs) + for key in balance_map_kwargs: + assert actual[key] == -amounts[key] + @pytest.mark.parametrize('balance_map_kwargs,expected', [ ({}, "Zero balance"), ({'JPY': 0, 'BRL': 0}, "Zero balance"),