Changeset - 3780c31c5901
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-28 13:01:00
brettcsmith@brettcsmith.org
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.
2 files changed with 25 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
...
 
@@ -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()
tests/test_reports_balance.py
Show inline comments
...
 
@@ -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"),
0 comments (0 inline, 0 general)