reports: Balance.__init__ better handles multiple amounts of same currency.
This is something that should've happened with 3d704e286
but I didn't think of it at the time.
This commit is contained in:
parent
a23d075add
commit
110e5038e1
2 changed files with 23 additions and 1 deletions
|
@ -90,8 +90,10 @@ class Balance(Mapping[str, data.Amount]):
|
||||||
) -> None:
|
) -> None:
|
||||||
if tolerance is None:
|
if tolerance is None:
|
||||||
tolerance = self.TOLERANCE
|
tolerance = self.TOLERANCE
|
||||||
self._currency_map = {amount.currency: amount for amount in source}
|
|
||||||
self.tolerance = tolerance
|
self.tolerance = tolerance
|
||||||
|
self._currency_map: Dict[str, data.Amount] = {}
|
||||||
|
for amount in source:
|
||||||
|
self._add_amount(self._currency_map, amount)
|
||||||
|
|
||||||
def _add_amount(self,
|
def _add_amount(self,
|
||||||
currency_map: MutableMapping[str, data.Amount],
|
currency_map: MutableMapping[str, data.Amount],
|
||||||
|
|
|
@ -81,6 +81,26 @@ def test_mixed_balance():
|
||||||
assert not balance.is_zero()
|
assert not balance.is_zero()
|
||||||
assert all(balance[key] == testutil.Amount(amt, key) for key, amt in amounts.items())
|
assert all(balance[key] == testutil.Amount(amt, key) for key, amt in amounts.items())
|
||||||
|
|
||||||
|
def test_init_recurring_currency():
|
||||||
|
balance = core.Balance([
|
||||||
|
testutil.Amount(20),
|
||||||
|
testutil.Amount(40),
|
||||||
|
testutil.Amount(60, 'EUR'),
|
||||||
|
testutil.Amount(-80),
|
||||||
|
])
|
||||||
|
assert balance
|
||||||
|
assert balance['EUR'] == testutil.Amount(60, 'EUR')
|
||||||
|
assert balance['USD'] == testutil.Amount(-20)
|
||||||
|
|
||||||
|
def test_init_zeroed_out():
|
||||||
|
balance = core.Balance([
|
||||||
|
testutil.Amount(25),
|
||||||
|
testutil.Amount(40, 'EUR'),
|
||||||
|
testutil.Amount(-25),
|
||||||
|
testutil.Amount(-40, 'EUR'),
|
||||||
|
])
|
||||||
|
assert balance.is_zero()
|
||||||
|
|
||||||
@pytest.mark.parametrize('mapping,expected', [
|
@pytest.mark.parametrize('mapping,expected', [
|
||||||
({}, True),
|
({}, True),
|
||||||
({'USD': 0}, True),
|
({'USD': 0}, True),
|
||||||
|
|
Loading…
Reference in a new issue