From 581046f988a38f58c0d15382630f44404a01a1d3 2020-06-09 13:04:27 From: Brett Smith Date: 2020-06-09 13:04:27 Subject: [PATCH] reports: Balance.format() respects tolerance. --- diff --git a/conservancy_beancount/reports/core.py b/conservancy_beancount/reports/core.py index 6b2845c70f7b3fcf29aef1af8c603fee4ab62ba0..6b759d816cf956a6c8158923048875640c1c439d 100644 --- a/conservancy_beancount/reports/core.py +++ b/conservancy_beancount/reports/core.py @@ -198,17 +198,18 @@ class Balance(Mapping[str, data.Amount]): fmt: Optional[str]='#,#00.00 ¤¤', sep: str=', ', empty: str="Zero balance", + tolerance: Optional[Decimal]=None, ) -> str: """Formats the balance as a string with the given parameters - If the balance is zero, returns ``empty``. Otherwise, returns a string - with each amount in the balance formatted as ``fmt``, separated by - ``sep``. + If the balance is zero (within tolerance), returns ``empty``. + Otherwise, returns a string with each amount in the balance formatted + as ``fmt``, separated by ``sep``. If you set ``fmt`` to None, amounts will be formatted according to the user's locale. The default format is Beancount's input format. """ - amounts = [amount for amount in self.values() if amount.number] + amounts = list(self.clean_copy(tolerance).values()) if not amounts: return empty amounts.sort(key=lambda amt: abs(amt.number), reverse=True) diff --git a/tests/test_reports_balance.py b/tests/test_reports_balance.py index 706531e63689f3e5196803cb7f9aaa949e98f3c1..be5b265c218638653775caed09a1677a909b7db0 100644 --- a/tests/test_reports_balance.py +++ b/tests/test_reports_balance.py @@ -32,6 +32,8 @@ DEFAULT_STRINGS = [ ({'USD': '20.00'}, "20.00 USD"), ({'EUR': '50.00', 'GBP': '80.00'}, "80.00 GBP, 50.00 EUR"), ({'JPY': '-5500.00', 'BRL': '-8500.00'}, "-8,500.00 BRL, -5,500 JPY"), + ({'USD': 10, 'EUR': '.00015'}, "10.00 USD"), + ({'JPY': '-.00015'}, "Zero balance"), ] TOLERANCES = [Decimal(n) for n in ['.1', '.01', '.001', 0]] @@ -379,3 +381,21 @@ def test_format_none(): def test_format_empty(empty): balance = core.Balance() assert balance.format(empty=empty) == empty + +@pytest.mark.parametrize('tolerance', TOLERANCES) +def test_str_tolerance(tolerance): + chf = testutil.Amount('.005', 'CHF') + actual = str(core.Balance([chf], tolerance)) + if tolerance > chf.number: + assert actual == "Zero balance" + else: + assert actual == "00.00 CHF" + +@pytest.mark.parametrize('tolerance', TOLERANCES) +def test_format_tolerance(tolerance): + chf = testutil.Amount('.005', 'CHF') + actual = core.Balance([chf]).format(tolerance=tolerance) + if tolerance > chf.number: + assert actual == "Zero balance" + else: + assert actual == "00.00 CHF"