reports: Balance.format() respects tolerance.
This commit is contained in:
parent
cd1766adcf
commit
581046f988
2 changed files with 25 additions and 4 deletions
|
@ -198,17 +198,18 @@ class Balance(Mapping[str, data.Amount]):
|
||||||
fmt: Optional[str]='#,#00.00 ¤¤',
|
fmt: Optional[str]='#,#00.00 ¤¤',
|
||||||
sep: str=', ',
|
sep: str=', ',
|
||||||
empty: str="Zero balance",
|
empty: str="Zero balance",
|
||||||
|
tolerance: Optional[Decimal]=None,
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Formats the balance as a string with the given parameters
|
"""Formats the balance as a string with the given parameters
|
||||||
|
|
||||||
If the balance is zero, returns ``empty``. Otherwise, returns a string
|
If the balance is zero (within tolerance), returns ``empty``.
|
||||||
with each amount in the balance formatted as ``fmt``, separated by
|
Otherwise, returns a string with each amount in the balance formatted
|
||||||
``sep``.
|
as ``fmt``, separated by ``sep``.
|
||||||
|
|
||||||
If you set ``fmt`` to None, amounts will be formatted according to the
|
If you set ``fmt`` to None, amounts will be formatted according to the
|
||||||
user's locale. The default format is Beancount's input format.
|
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:
|
if not amounts:
|
||||||
return empty
|
return empty
|
||||||
amounts.sort(key=lambda amt: abs(amt.number), reverse=True)
|
amounts.sort(key=lambda amt: abs(amt.number), reverse=True)
|
||||||
|
|
|
@ -32,6 +32,8 @@ DEFAULT_STRINGS = [
|
||||||
({'USD': '20.00'}, "20.00 USD"),
|
({'USD': '20.00'}, "20.00 USD"),
|
||||||
({'EUR': '50.00', 'GBP': '80.00'}, "80.00 GBP, 50.00 EUR"),
|
({'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"),
|
({'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]]
|
TOLERANCES = [Decimal(n) for n in ['.1', '.01', '.001', 0]]
|
||||||
|
@ -379,3 +381,21 @@ def test_format_none():
|
||||||
def test_format_empty(empty):
|
def test_format_empty(empty):
|
||||||
balance = core.Balance()
|
balance = core.Balance()
|
||||||
assert balance.format(empty=empty) == empty
|
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"
|
||||||
|
|
Loading…
Reference in a new issue