reports: Add Balance.__neg__() method.
This commit is contained in:
parent
68acb86e7e
commit
1b81375294
2 changed files with 21 additions and 0 deletions
|
@ -67,6 +67,11 @@ class Balance(Mapping[str, data.Amount]):
|
||||||
amounts.sort(key=lambda amt: abs(amt.number), reverse=True)
|
amounts.sort(key=lambda amt: abs(amt.number), reverse=True)
|
||||||
return ', '.join(str(amount) for amount in amounts)
|
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:
|
def __getitem__(self, key: str) -> data.Amount:
|
||||||
return data.Amount(self._currency_map[key], key)
|
return data.Amount(self._currency_map[key], key)
|
||||||
|
|
||||||
|
|
|
@ -67,6 +67,22 @@ def test_mixed_balance():
|
||||||
assert not balance.is_zero()
|
assert not balance.is_zero()
|
||||||
assert all(balance[key] == amt for key, amt in amounts.items())
|
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', [
|
@pytest.mark.parametrize('balance_map_kwargs,expected', [
|
||||||
({}, "Zero balance"),
|
({}, "Zero balance"),
|
||||||
({'JPY': 0, 'BRL': 0}, "Zero balance"),
|
({'JPY': 0, 'BRL': 0}, "Zero balance"),
|
||||||
|
|
Loading…
Reference in a new issue