Changeset - cc0656dde9b1
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-06-03 22:52:44
brettcsmith@brettcsmith.org
reports: Add Balance.__abs__() method.
2 files changed with 26 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
...
 
@@ -12,24 +12,26 @@
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import collections
 
import operator
 

	
 
from decimal import Decimal
 

	
 
import babel.numbers  # type:ignore[import]
 

	
 
from beancount.core import amount as bc_amount
 

	
 
from .. import data
 

	
 
from typing import (
 
    overload,
 
    Any,
 
    Callable,
 
    DefaultDict,
 
    Dict,
 
    Iterable,
 
    Iterator,
 
    List,
 
    Mapping,
...
 
@@ -64,24 +66,29 @@ class Balance(Mapping[str, data.Amount]):
 
        if isinstance(source, Mapping):
 
            source = source.items()
 
        self._currency_map = {
 
            currency: amount.number for currency, amount in source
 
        }
 

	
 
    def __repr__(self) -> str:
 
        return f"{type(self).__name__}({self._currency_map!r})"
 

	
 
    def __str__(self) -> str:
 
        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:
 
        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
...
 
@@ -118,24 +118,43 @@ def test_ge_zero(balance_map_kwargs, expected):
 
    ({'EUR': -10}, True),
 
    ({'USD': 10, 'EUR': -20}, False),
 
    ({'USD': -10, 'EUR': -20}, True),
 
    ({'JPY': 10}, False),
 
    ({'JPY': 10, 'BRL': 0}, False),
 
    ({'JPY': 10, 'BRL': 20}, False),
 
])
 
def test_le_zero(balance_map_kwargs, expected):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    balance = core.Balance(amounts.items())
 
    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', [
 
    {},
 
    {'USD': 0},
 
    {'EUR': 10},
 
    {'JPY': 20, 'BRL': 30},
 
    {'EUR': -15},
 
    {'JPY': -25, 'BRL': -35},
 
    {'JPY': 40, 'USD': 0, 'EUR': -50},
 
])
 
def test_neg(balance_map_kwargs):
 
    amounts = testutil.balance_map(**balance_map_kwargs)
 
    actual = -core.Balance(amounts.items())
0 comments (0 inline, 0 general)