From 3780c31c5901e3da087f8d6b38fbe5790f24dc96 2020-05-28 13:01:00 From: Brett Smith Date: 2020-05-28 13:01:00 Subject: [PATCH] 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. --- diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 5ded97fbd1b84a474817f64c0fa1469ea21543a1..e2f474ed1332c948d985b6dd3be77cc010b6da57 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -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() diff --git a/tests/test_reports_balance.py b/tests/test_reports_balance.py index 249a288d9890209e8265085d419725db5e8f130c..07e238662870c712fc76a39a84f210ac8aa691c1 100644 --- a/tests/test_reports_balance.py +++ b/tests/test_reports_balance.py @@ -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"),