reports: Add Balance.__abs__() method.
This commit is contained in:
parent
58b02b6f33
commit
cc0656dde9
2 changed files with 26 additions and 0 deletions
|
@ -21,6 +21,8 @@ from decimal import Decimal
|
||||||
|
|
||||||
import babel.numbers # type:ignore[import]
|
import babel.numbers # type:ignore[import]
|
||||||
|
|
||||||
|
from beancount.core import amount as bc_amount
|
||||||
|
|
||||||
from .. import data
|
from .. import data
|
||||||
|
|
||||||
from typing import (
|
from typing import (
|
||||||
|
@ -73,6 +75,11 @@ class Balance(Mapping[str, data.Amount]):
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.format()
|
return self.format()
|
||||||
|
|
||||||
|
def __abs__(self) -> 'Balance':
|
||||||
|
return type(self)(
|
||||||
|
(key, bc_amount.abs(amt)) for key, amt in self.items()
|
||||||
|
)
|
||||||
|
|
||||||
def __eq__(self, other: Any) -> bool:
|
def __eq__(self, other: Any) -> bool:
|
||||||
if (self.is_zero()
|
if (self.is_zero()
|
||||||
and isinstance(other, Balance)
|
and isinstance(other, Balance)
|
||||||
|
|
|
@ -127,6 +127,25 @@ def test_le_zero(balance_map_kwargs, expected):
|
||||||
balance = core.Balance(amounts.items())
|
balance = core.Balance(amounts.items())
|
||||||
assert balance.le_zero() == expected
|
assert balance.le_zero() == expected
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('balance_map_kwargs', [
|
||||||
|
{},
|
||||||
|
{'USD': 0},
|
||||||
|
{'EUR': 10},
|
||||||
|
{'JPY': 20, 'BRL': 30},
|
||||||
|
{'EUR': -15},
|
||||||
|
{'JPY': -25, 'BRL': -35},
|
||||||
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
||||||
|
])
|
||||||
|
def test_abs(balance_map_kwargs):
|
||||||
|
amounts = testutil.balance_map(**balance_map_kwargs)
|
||||||
|
actual = abs(core.Balance(amounts.items()))
|
||||||
|
assert set(actual) == set(balance_map_kwargs)
|
||||||
|
abs_amounts = testutil.balance_map(**{
|
||||||
|
key: abs(number) for key, number in balance_map_kwargs.items()
|
||||||
|
})
|
||||||
|
for key in balance_map_kwargs:
|
||||||
|
assert actual[key] == abs_amounts[key]
|
||||||
|
|
||||||
@pytest.mark.parametrize('balance_map_kwargs', [
|
@pytest.mark.parametrize('balance_map_kwargs', [
|
||||||
{},
|
{},
|
||||||
{'USD': 0},
|
{'USD': 0},
|
||||||
|
|
Loading…
Reference in a new issue