reports: Add Balance.__eq__() method.
It turns out the provided implementation gets us most of the way there, we just needed to add handling for the special case of zero balances. Now it's confirmed with tests.
This commit is contained in:
parent
81d216f282
commit
3780c31c59
2 changed files with 25 additions and 0 deletions
|
@ -71,6 +71,14 @@ 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 __eq__(self, other: Any) -> bool:
|
||||
if (self.is_zero()
|
||||
and isinstance(other, Balance)
|
||||
and other.is_zero()):
|
||||
return True
|
||||
else:
|
||||
return super().__eq__(other)
|
||||
|
||||
def __neg__(self) -> 'Balance':
|
||||
return type(self)(
|
||||
(key, -amt) for key, amt in self.items()
|
||||
|
|
|
@ -133,6 +133,23 @@ def test_neg(balance_map_kwargs):
|
|||
for key in balance_map_kwargs:
|
||||
assert actual[key] == -amounts[key]
|
||||
|
||||
@pytest.mark.parametrize('kwargs1,kwargs2,expected', [
|
||||
({}, {}, True),
|
||||
({}, {'USD': 0}, True),
|
||||
({}, {'EUR': 1}, False),
|
||||
({'USD': 1}, {'EUR': 1}, False),
|
||||
({'USD': 1}, {'USD': '1.0'}, True),
|
||||
({'USD': 1}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
||||
({'USD': 1, 'BRL': '2.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
||||
({'USD': 1, 'EUR': 2, 'BRL': '3.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
||||
({'USD': 1, 'EUR': 2}, {'USD': '1.0', 'EUR': '2.0'}, True),
|
||||
])
|
||||
def test_eq(kwargs1, kwargs2, expected):
|
||||
bal1 = core.Balance(testutil.balance_map(**kwargs1))
|
||||
bal2 = core.Balance(testutil.balance_map(**kwargs2))
|
||||
actual = bal1 == bal2
|
||||
assert actual == expected
|
||||
|
||||
@pytest.mark.parametrize('balance_map_kwargs,expected', [
|
||||
({}, "Zero balance"),
|
||||
({'JPY': 0, 'BRL': 0}, "Zero balance"),
|
||||
|
|
Loading…
Reference in a new issue