Changeset - d8df34ebaf6f
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-28 13:03:23
brettcsmith@brettcsmith.org
reports: Balance.format() accepts None as a format.
2 files changed with 12 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
...
 
@@ -104,33 +104,36 @@ class Balance(Mapping[str, data.Amount]):
 

	
 
    is_zero = eq_zero
 

	
 
    def ge_zero(self) -> bool:
 
        """Returns true if all amounts in the balance >= 0."""
 
        return self._all_amounts(operator.ge, 0)
 

	
 
    def le_zero(self) -> bool:
 
        """Returns true if all amounts in the balance <= 0."""
 
        return self._all_amounts(operator.le, 0)
 

	
 
    def format(self,
 
               fmt: str='#,#00.00 ¤¤',
 
               fmt: Optional[str]='#,#00.00 ¤¤',
 
               sep: str=', ',
 
               empty: str="Zero balance",
 
    ) -> 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 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]
 
        if not amounts:
 
            return empty
 
        amounts.sort(key=lambda amt: abs(amt.number), reverse=True)
 
        return sep.join(
 
            babel.numbers.format_currency(amt.number, amt.currency, fmt)
 
            for amt in amounts
 
        )
 

	
 

	
 
class MutableBalance(Balance):
tests/test_reports_balance.py
Show inline comments
...
 
@@ -13,24 +13,26 @@
 
#
 
# 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 itertools
 

	
 
from decimal import Decimal
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
import babel.numbers
 

	
 
from conservancy_beancount.reports import core
 

	
 
DEFAULT_STRINGS = [
 
    ({}, "Zero balance"),
 
    ({'JPY': 0, 'BRL': 0}, "Zero balance"),
 
    ({'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"),
 
]
 

	
 
def test_empty_balance():
 
    balance = core.Balance()
...
 
@@ -182,20 +184,26 @@ def test_format_fmt(fmt, expected):
 
@pytest.mark.parametrize('sep', [
 
    '; ',
 
    '—',
 
    '\0',
 
])
 
def test_format_sep(sep):
 
    bal_kwargs, expected = DEFAULT_STRINGS[-1]
 
    expected = expected.replace(', ', sep)
 
    amounts = testutil.balance_map(**bal_kwargs)
 
    balance = core.Balance(amounts)
 
    assert balance.format(sep=sep) == expected
 

	
 
def test_format_none():
 
    amounts = testutil.balance_map(BRL=65000)
 
    balance = core.Balance(amounts)
 
    expected = babel.numbers.format_currency(65000, 'BRL')
 
    assert balance.format(None) == expected
 

	
 
@pytest.mark.parametrize('empty', [
 
    "N/A",
 
    "Zero",
 
    "ø",
 
])
 
def test_format_empty(empty):
 
    balance = core.Balance()
 
    assert balance.format(empty=empty) == empty
0 comments (0 inline, 0 general)