2020-04-12 15:00:41 +00:00
|
|
|
"""test_reports_balance - Unit tests for reports.core.Balance"""
|
|
|
|
# Copyright © 2020 Brett Smith
|
2021-01-08 21:57:43 +00:00
|
|
|
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
|
2020-04-12 15:00:41 +00:00
|
|
|
#
|
2021-01-08 21:57:43 +00:00
|
|
|
# Full copyright and licensing details can be found at toplevel file
|
|
|
|
# LICENSE.txt in the repository.
|
2020-04-12 15:00:41 +00:00
|
|
|
|
|
|
|
import itertools
|
|
|
|
|
|
|
|
from decimal import Decimal
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
2020-05-28 12:57:43 +00:00
|
|
|
import babel.numbers
|
|
|
|
|
2020-04-12 15:00:41 +00:00
|
|
|
from conservancy_beancount.reports import core
|
|
|
|
|
2020-05-28 12:39:50 +00:00
|
|
|
DEFAULT_STRINGS = [
|
|
|
|
({}, "Zero balance"),
|
2020-06-18 18:07:44 +00:00
|
|
|
({'JPY': 0, 'BRL': 0}, "0.00 BRL, 0 JPY"),
|
2020-05-28 12:39:50 +00:00
|
|
|
({'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"),
|
2020-06-07 15:59:31 +00:00
|
|
|
({'USD': 10, 'EUR': '.00015'}, "10.00 USD"),
|
2020-06-18 18:07:44 +00:00
|
|
|
({'JPY': '-.00015'}, "-0 JPY"),
|
2020-05-28 12:39:50 +00:00
|
|
|
]
|
|
|
|
|
2020-06-07 15:46:19 +00:00
|
|
|
TOLERANCES = [Decimal(n) for n in ['.1', '.01', '.001', 0]]
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
def amounts_from_map(currency_map):
|
|
|
|
for code, number in currency_map.items():
|
|
|
|
yield testutil.Amount(number, code)
|
|
|
|
|
2020-04-12 15:00:41 +00:00
|
|
|
def test_empty_balance():
|
|
|
|
balance = core.Balance()
|
|
|
|
assert not balance
|
|
|
|
assert len(balance) == 0
|
|
|
|
assert balance.is_zero()
|
|
|
|
with pytest.raises(KeyError):
|
|
|
|
balance['USD']
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('currencies', [
|
|
|
|
'USD',
|
|
|
|
'EUR GBP',
|
|
|
|
'JPY INR BRL',
|
|
|
|
])
|
|
|
|
def test_zero_balance(currencies):
|
|
|
|
keys = currencies.split()
|
2020-06-03 22:54:12 +00:00
|
|
|
balance = core.Balance(testutil.Amount(0, key) for key in keys)
|
2020-04-12 15:00:41 +00:00
|
|
|
assert balance
|
|
|
|
assert len(balance) == len(keys)
|
|
|
|
assert balance.is_zero()
|
|
|
|
assert all(balance[key].number == 0 for key in keys)
|
|
|
|
assert all(balance[key].currency == key for key in keys)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('currencies', [
|
|
|
|
'USD',
|
|
|
|
'EUR GBP',
|
|
|
|
'JPY INR BRL',
|
|
|
|
])
|
|
|
|
def test_nonzero_balance(currencies):
|
2020-06-03 22:54:12 +00:00
|
|
|
amounts = dict(zip(currencies.split(), itertools.count(110, 100)))
|
|
|
|
balance = core.Balance(amounts_from_map(amounts))
|
2020-04-12 15:00:41 +00:00
|
|
|
assert balance
|
|
|
|
assert len(balance) == len(amounts)
|
|
|
|
assert not balance.is_zero()
|
2020-06-03 22:54:12 +00:00
|
|
|
assert all(balance[key] == testutil.Amount(amt, key) for key, amt in amounts.items())
|
2020-04-12 15:00:41 +00:00
|
|
|
|
|
|
|
def test_mixed_balance():
|
2020-06-03 22:54:12 +00:00
|
|
|
amounts = {'USD': 0, 'EUR': 120}
|
|
|
|
balance = core.Balance(amounts_from_map(amounts))
|
2020-04-12 15:00:41 +00:00
|
|
|
assert balance
|
|
|
|
assert len(balance) == 2
|
|
|
|
assert not balance.is_zero()
|
2020-06-03 22:54:12 +00:00
|
|
|
assert all(balance[key] == testutil.Amount(amt, key) for key, amt in amounts.items())
|
2020-04-29 15:37:38 +00:00
|
|
|
|
2020-06-07 15:17:24 +00:00
|
|
|
def test_init_recurring_currency():
|
|
|
|
balance = core.Balance([
|
|
|
|
testutil.Amount(20),
|
|
|
|
testutil.Amount(40),
|
|
|
|
testutil.Amount(60, 'EUR'),
|
|
|
|
testutil.Amount(-80),
|
|
|
|
])
|
|
|
|
assert balance
|
|
|
|
assert balance['EUR'] == testutil.Amount(60, 'EUR')
|
|
|
|
assert balance['USD'] == testutil.Amount(-20)
|
|
|
|
|
|
|
|
def test_init_zeroed_out():
|
|
|
|
balance = core.Balance([
|
|
|
|
testutil.Amount(25),
|
|
|
|
testutil.Amount(40, 'EUR'),
|
|
|
|
testutil.Amount(-25),
|
|
|
|
testutil.Amount(-40, 'EUR'),
|
|
|
|
])
|
|
|
|
assert balance.is_zero()
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping,expected', [
|
2020-05-22 03:08:57 +00:00
|
|
|
({}, True),
|
|
|
|
({'USD': 0}, True),
|
|
|
|
({'USD': 0, 'EUR': 0}, True),
|
|
|
|
({'USD': -10, 'EUR': 0}, False),
|
|
|
|
({'EUR': -10}, False),
|
|
|
|
({'USD': -10, 'EUR': -20}, False),
|
|
|
|
({'USD': 10, 'EUR': -20}, False),
|
|
|
|
({'JPY': 10}, False),
|
|
|
|
({'JPY': 10, 'BRL': 0}, False),
|
|
|
|
({'JPY': 10, 'BRL': 20}, False),
|
2020-06-04 02:01:07 +00:00
|
|
|
({'USD': '0.00015'}, True),
|
|
|
|
({'EUR': '-0.00052'}, True),
|
2020-05-22 03:08:57 +00:00
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_eq_zero(mapping, expected):
|
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
2020-05-22 03:08:57 +00:00
|
|
|
assert balance.eq_zero() == expected
|
|
|
|
assert balance.is_zero() == expected
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping,expected', [
|
2020-05-22 03:08:57 +00:00
|
|
|
({}, True),
|
|
|
|
({'USD': 0}, True),
|
|
|
|
({'USD': 0, 'EUR': 0}, True),
|
|
|
|
({'EUR': -10}, False),
|
|
|
|
({'USD': 10, 'EUR': -20}, False),
|
|
|
|
({'USD': -10, 'EUR': -20}, False),
|
|
|
|
({'JPY': 10}, True),
|
|
|
|
({'JPY': 10, 'BRL': 0}, True),
|
|
|
|
({'JPY': 10, 'BRL': 20}, True),
|
2020-06-04 02:01:07 +00:00
|
|
|
({'USD': '0.00015'}, True),
|
|
|
|
({'EUR': '-0.00052'}, True),
|
2020-06-04 14:49:55 +00:00
|
|
|
({'RUB': core.Balance.TOLERANCE}, True),
|
|
|
|
({'RUB': -core.Balance.TOLERANCE}, False),
|
2020-05-22 03:08:57 +00:00
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_ge_zero(mapping, expected):
|
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
2020-05-22 03:08:57 +00:00
|
|
|
assert balance.ge_zero() == expected
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping,expected', [
|
2020-05-22 03:08:57 +00:00
|
|
|
({}, True),
|
|
|
|
({'USD': 0}, True),
|
|
|
|
({'USD': 0, 'EUR': 0}, True),
|
|
|
|
({'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),
|
2020-06-04 02:01:07 +00:00
|
|
|
({'USD': '0.00015'}, True),
|
|
|
|
({'EUR': '-0.00052'}, True),
|
2020-06-04 14:49:55 +00:00
|
|
|
({'RUB': core.Balance.TOLERANCE}, False),
|
|
|
|
({'RUB': -core.Balance.TOLERANCE}, True),
|
2020-05-22 03:08:57 +00:00
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_le_zero(mapping, expected):
|
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
2020-05-22 03:08:57 +00:00
|
|
|
assert balance.le_zero() == expected
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping', [
|
2020-06-03 22:52:44 +00:00
|
|
|
{},
|
|
|
|
{'USD': 0},
|
|
|
|
{'EUR': 10},
|
|
|
|
{'JPY': 20, 'BRL': 30},
|
|
|
|
{'EUR': -15},
|
|
|
|
{'JPY': -25, 'BRL': -35},
|
|
|
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_abs(mapping):
|
|
|
|
actual = abs(core.Balance(amounts_from_map(mapping)))
|
|
|
|
assert set(actual) == set(mapping)
|
|
|
|
for key, number in mapping.items():
|
|
|
|
assert actual[key] == testutil.Amount(abs(number), key)
|
2020-06-03 22:52:44 +00:00
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping', [
|
2020-04-29 18:35:20 +00:00
|
|
|
{},
|
|
|
|
{'USD': 0},
|
|
|
|
{'EUR': 10},
|
|
|
|
{'JPY': 20, 'BRL': 30},
|
|
|
|
{'EUR': -15},
|
|
|
|
{'JPY': -25, 'BRL': -35},
|
|
|
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_neg(mapping):
|
|
|
|
actual = -core.Balance(amounts_from_map(mapping))
|
|
|
|
assert set(actual) == set(mapping)
|
|
|
|
for key, number in mapping.items():
|
|
|
|
assert actual[key] == testutil.Amount(-number, key)
|
2020-04-29 18:35:20 +00:00
|
|
|
|
2020-06-06 15:31:04 +00:00
|
|
|
@pytest.mark.parametrize('mapping', [
|
|
|
|
{},
|
|
|
|
{'USD': 0},
|
|
|
|
{'EUR': 10},
|
|
|
|
{'JPY': 20, 'BRL': 30},
|
|
|
|
{'EUR': -15},
|
|
|
|
{'JPY': -25, 'BRL': -35},
|
|
|
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
|
|
|
])
|
|
|
|
def test_pos(mapping):
|
|
|
|
amounts = frozenset(amounts_from_map(mapping))
|
|
|
|
actual = +core.Balance(amounts)
|
|
|
|
assert set(actual.values()) == amounts
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('map1,map2,expected', [
|
2020-05-28 12:11:02 +00:00
|
|
|
({}, {}, True),
|
|
|
|
({}, {'USD': 0}, True),
|
|
|
|
({}, {'EUR': 1}, False),
|
|
|
|
({'USD': 1}, {'EUR': 1}, False),
|
|
|
|
({'USD': 1}, {'USD': '1.0'}, True),
|
|
|
|
({'USD': 1}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
|
|
|
({'USD': 1, 'BRL': '2.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
|
|
|
({'USD': 1, 'EUR': 2, 'BRL': '3.0'}, {'USD': '1.0', 'EUR': '2.0'}, False),
|
|
|
|
({'USD': 1, 'EUR': 2}, {'USD': '1.0', 'EUR': '2.0'}, True),
|
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_eq(map1, map2, expected):
|
|
|
|
bal1 = core.Balance(amounts_from_map(map1))
|
|
|
|
bal2 = core.Balance(amounts_from_map(map2))
|
2020-05-28 12:11:02 +00:00
|
|
|
actual = bal1 == bal2
|
|
|
|
assert actual == expected
|
|
|
|
|
2020-06-07 15:46:19 +00:00
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
|
|
|
def test_eq_considers_tolerance(tolerance):
|
|
|
|
tolerance = Decimal(tolerance)
|
|
|
|
mapping = {'EUR': 100, 'USD': '.002'}
|
|
|
|
bal1 = core.Balance(amounts_from_map(mapping))
|
|
|
|
mapping['USD'] = '.004'
|
|
|
|
bal2 = core.Balance(amounts_from_map(mapping), tolerance)
|
|
|
|
assert (bal1 == bal2) == (tolerance > Decimal('.002'))
|
|
|
|
|
2020-06-03 22:53:17 +00:00
|
|
|
@pytest.mark.parametrize('number,currency', {
|
|
|
|
(50, 'USD'),
|
|
|
|
(-50, 'USD'),
|
|
|
|
(50000, 'BRL'),
|
|
|
|
(-4000, 'BRL'),
|
|
|
|
})
|
|
|
|
def test_add_amount(number, currency):
|
2020-06-03 22:54:12 +00:00
|
|
|
start_amount = testutil.Amount(500, 'USD')
|
|
|
|
start_bal = core.Balance([start_amount])
|
2020-06-03 22:53:17 +00:00
|
|
|
add_amount = testutil.Amount(number, currency)
|
|
|
|
actual = start_bal + add_amount
|
|
|
|
if currency == 'USD':
|
|
|
|
assert len(actual) == 1
|
|
|
|
assert actual['USD'] == testutil.Amount(500 + number)
|
|
|
|
else:
|
|
|
|
assert len(actual) == 2
|
2020-06-03 22:54:12 +00:00
|
|
|
assert actual['USD'] == start_amount
|
2020-06-03 22:53:17 +00:00
|
|
|
assert actual[currency] == add_amount
|
2020-06-03 22:54:12 +00:00
|
|
|
assert start_bal == {'USD': start_amount}
|
2020-06-03 22:53:17 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('number,currency', {
|
|
|
|
(50, 'USD'),
|
|
|
|
(-50, 'USD'),
|
|
|
|
(50000, 'BRL'),
|
|
|
|
(-4000, 'BRL'),
|
|
|
|
})
|
|
|
|
def test_iadd_amount(number, currency):
|
2020-06-03 22:54:12 +00:00
|
|
|
balance = core.MutableBalance([testutil.Amount(500, 'USD')])
|
2020-06-03 22:53:17 +00:00
|
|
|
add_amount = testutil.Amount(number, currency)
|
|
|
|
balance += add_amount
|
|
|
|
if currency == 'USD':
|
|
|
|
assert len(balance) == 1
|
|
|
|
assert balance['USD'] == testutil.Amount(500 + number)
|
|
|
|
else:
|
|
|
|
assert len(balance) == 2
|
|
|
|
assert balance['USD'] == testutil.Amount(500)
|
|
|
|
assert balance[currency] == add_amount
|
|
|
|
|
2020-06-15 14:15:19 +00:00
|
|
|
@pytest.mark.parametrize('number,currency', {
|
|
|
|
(50, 'USD'),
|
|
|
|
(-50, 'USD'),
|
|
|
|
(50000, 'BRL'),
|
|
|
|
(-4000, 'BRL'),
|
|
|
|
})
|
|
|
|
def test_sub_amount(number, currency):
|
|
|
|
start_amount = testutil.Amount(500, 'USD')
|
|
|
|
start_bal = core.Balance([start_amount])
|
|
|
|
sub_amount = testutil.Amount(number, currency)
|
|
|
|
actual = start_bal - sub_amount
|
|
|
|
if currency == 'USD':
|
|
|
|
assert len(actual) == 1
|
|
|
|
assert actual['USD'] == testutil.Amount(500 - number)
|
|
|
|
else:
|
|
|
|
assert len(actual) == 2
|
|
|
|
assert actual['USD'] == start_amount
|
|
|
|
assert actual[currency] == -sub_amount
|
|
|
|
assert start_bal == {'USD': start_amount}
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('number,currency', {
|
|
|
|
(50, 'USD'),
|
|
|
|
(-50, 'USD'),
|
|
|
|
(50000, 'BRL'),
|
|
|
|
(-4000, 'BRL'),
|
|
|
|
})
|
|
|
|
def test_isub_amount(number, currency):
|
|
|
|
balance = core.MutableBalance([testutil.Amount(500, 'USD')])
|
|
|
|
sub_amount = testutil.Amount(number, currency)
|
|
|
|
balance -= sub_amount
|
|
|
|
if currency == 'USD':
|
|
|
|
assert len(balance) == 1
|
|
|
|
assert balance['USD'] == testutil.Amount(500 - number)
|
|
|
|
else:
|
|
|
|
assert len(balance) == 2
|
|
|
|
assert balance['USD'] == testutil.Amount(500)
|
|
|
|
assert balance[currency] == -sub_amount
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping', [
|
2020-06-03 22:53:17 +00:00
|
|
|
{},
|
|
|
|
{'USD': 0},
|
|
|
|
{'EUR': 10},
|
|
|
|
{'JPY': 20, 'BRL': 30},
|
|
|
|
{'EUR': -15},
|
|
|
|
{'JPY': -25, 'BRL': -35},
|
|
|
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_add_balance(mapping):
|
|
|
|
expect_numbers = {'USD': 500, 'BRL': 40000}
|
|
|
|
start_bal = core.Balance(amounts_from_map(expect_numbers))
|
|
|
|
for code, number in mapping.items():
|
2020-06-03 22:53:17 +00:00
|
|
|
expect_numbers[code] = expect_numbers.get(code, 0) + number
|
2020-06-03 22:54:12 +00:00
|
|
|
add_bal = core.Balance(amounts_from_map(mapping))
|
2020-06-03 22:53:17 +00:00
|
|
|
actual = start_bal + add_bal
|
2020-06-03 22:54:12 +00:00
|
|
|
expected = core.Balance(amounts_from_map(expect_numbers))
|
2020-06-03 22:53:17 +00:00
|
|
|
assert actual == expected
|
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping', [
|
2020-06-03 22:53:17 +00:00
|
|
|
{},
|
|
|
|
{'USD': 0},
|
|
|
|
{'EUR': 10},
|
|
|
|
{'JPY': 20, 'BRL': 30},
|
|
|
|
{'EUR': -15},
|
|
|
|
{'JPY': -25, 'BRL': -35},
|
|
|
|
{'JPY': 40, 'USD': 0, 'EUR': -50},
|
|
|
|
])
|
2020-06-03 22:54:12 +00:00
|
|
|
def test_iadd_balance(mapping):
|
|
|
|
expect_numbers = {'USD': 500, 'BRL': 40000}
|
|
|
|
balance = core.MutableBalance(amounts_from_map(expect_numbers))
|
|
|
|
for code, number in mapping.items():
|
2020-06-03 22:53:17 +00:00
|
|
|
expect_numbers[code] = expect_numbers.get(code, 0) + number
|
2020-06-03 22:54:12 +00:00
|
|
|
balance += core.Balance(amounts_from_map(mapping))
|
|
|
|
expected = core.Balance(amounts_from_map(expect_numbers))
|
2020-06-03 22:53:17 +00:00
|
|
|
assert balance == expected
|
|
|
|
|
2020-06-20 13:08:35 +00:00
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
|
|
|
def test_copy(tolerance):
|
|
|
|
eur = testutil.Amount('.003', 'EUR')
|
|
|
|
source = core.Balance([eur], tolerance)
|
|
|
|
new = source.copy()
|
|
|
|
assert source is not new
|
|
|
|
assert dict(source) == dict(new)
|
|
|
|
assert new.tolerance == tolerance
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
|
|
|
def test_copy_tolerance_arg(tolerance):
|
|
|
|
eur = testutil.Amount('.003', 'EUR')
|
|
|
|
source = core.Balance([eur])
|
|
|
|
new = source.copy(tolerance)
|
|
|
|
assert source is not new
|
|
|
|
assert dict(source) == dict(new)
|
|
|
|
assert new.tolerance == tolerance
|
2020-06-07 15:46:19 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
|
|
|
def test_clean_copy(tolerance):
|
|
|
|
usd = testutil.Amount(10)
|
|
|
|
eur = testutil.Amount('.002', 'EUR')
|
|
|
|
actual = core.Balance([usd, eur], tolerance).clean_copy()
|
|
|
|
if tolerance < eur.number:
|
|
|
|
expected = {usd, eur}
|
|
|
|
else:
|
|
|
|
expected = {usd}
|
|
|
|
assert frozenset(actual.values()) == expected
|
2020-06-20 13:08:35 +00:00
|
|
|
assert actual.tolerance == tolerance
|
2020-06-07 15:46:19 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
|
|
|
def test_clean_copy_arg(tolerance):
|
|
|
|
usd = testutil.Amount(10)
|
|
|
|
eur = testutil.Amount('.002', 'EUR')
|
|
|
|
actual = core.Balance([usd, eur], 0).clean_copy(tolerance)
|
|
|
|
if tolerance < eur.number:
|
|
|
|
expected = {usd, eur}
|
|
|
|
else:
|
|
|
|
expected = {usd}
|
|
|
|
assert frozenset(actual.values()) == expected
|
2020-06-20 13:08:35 +00:00
|
|
|
assert actual.tolerance == tolerance
|
2020-06-07 15:46:19 +00:00
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping,expected', DEFAULT_STRINGS)
|
|
|
|
def test_str(mapping, expected):
|
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
|
|
|
assert str(balance) == expected
|
2020-05-28 12:39:50 +00:00
|
|
|
|
2020-06-03 22:54:12 +00:00
|
|
|
@pytest.mark.parametrize('mapping,expected', DEFAULT_STRINGS)
|
|
|
|
def test_format_defaults(mapping, expected):
|
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
|
|
|
assert balance.format() == expected
|
2020-05-28 12:39:50 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('fmt,expected', [
|
|
|
|
('¤##0.0', '¥5000, -€1500.00'),
|
2020-06-18 18:07:44 +00:00
|
|
|
('#,##0.0¤¤', '5,000JPY, -1,500.00EUR'),
|
2020-05-28 12:39:50 +00:00
|
|
|
('¤+##0.0;¤-##0.0', '¥+5000, €-1500.00'),
|
2020-06-18 18:07:44 +00:00
|
|
|
('#,##0.0 ¤¤;(#,##0.0 ¤¤)', '5,000 JPY, (1,500.00 EUR)'),
|
2020-05-28 12:39:50 +00:00
|
|
|
])
|
|
|
|
def test_format_fmt(fmt, expected):
|
2020-06-03 22:54:12 +00:00
|
|
|
amounts = [testutil.Amount(5000, 'JPY'), testutil.Amount(-1500, 'EUR')]
|
2020-05-28 12:39:50 +00:00
|
|
|
balance = core.Balance(amounts)
|
|
|
|
assert balance.format(fmt) == expected
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('sep', [
|
|
|
|
'; ',
|
|
|
|
'—',
|
|
|
|
'\0',
|
|
|
|
])
|
|
|
|
def test_format_sep(sep):
|
2020-06-03 22:54:12 +00:00
|
|
|
mapping, expected = DEFAULT_STRINGS[-1]
|
2020-05-28 12:39:50 +00:00
|
|
|
expected = expected.replace(', ', sep)
|
2020-06-03 22:54:12 +00:00
|
|
|
balance = core.Balance(amounts_from_map(mapping))
|
2020-05-28 12:39:50 +00:00
|
|
|
assert balance.format(sep=sep) == expected
|
|
|
|
|
2020-06-21 15:39:31 +00:00
|
|
|
@pytest.mark.parametrize('number', [65000, -77000])
|
|
|
|
def test_format_none(number):
|
|
|
|
args = (number, 'BRL')
|
2020-06-03 22:54:12 +00:00
|
|
|
balance = core.Balance([testutil.Amount(*args)])
|
2020-06-21 15:39:31 +00:00
|
|
|
expected = babel.numbers.format_currency(*args, format_type='accounting')
|
2020-05-28 12:57:43 +00:00
|
|
|
assert balance.format(None) == expected
|
|
|
|
|
2020-05-28 12:39:50 +00:00
|
|
|
@pytest.mark.parametrize('empty', [
|
|
|
|
"N/A",
|
|
|
|
"Zero",
|
|
|
|
"ø",
|
|
|
|
])
|
|
|
|
def test_format_empty(empty):
|
|
|
|
balance = core.Balance()
|
|
|
|
assert balance.format(empty=empty) == empty
|
2020-06-07 15:59:31 +00:00
|
|
|
|
2020-06-18 18:07:44 +00:00
|
|
|
@pytest.mark.parametrize('currency,fmt', itertools.product(
|
|
|
|
['USD', 'JPY', 'BRL'],
|
|
|
|
[None, '¤#,##0.00', '###0.00 ¤¤'],
|
|
|
|
))
|
|
|
|
def test_format_zero_balance_fmt(currency, fmt):
|
|
|
|
zero_amt = testutil.Amount(0, currency)
|
|
|
|
nonzero_amt = testutil.Amount(9, currency)
|
|
|
|
zero_bal = core.Balance([zero_amt])
|
|
|
|
nonzero_bal = core.Balance([nonzero_amt])
|
|
|
|
expected = nonzero_bal.format(fmt).replace('9', '0')
|
|
|
|
assert zero_bal.format(fmt) == expected
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('currency,fmt', testutil.combine_values(
|
|
|
|
['USD', 'JPY', 'BRL'],
|
|
|
|
["N/A", "Zero", "ø"],
|
|
|
|
))
|
|
|
|
def test_format_zero_balance_zero_str(currency, fmt):
|
|
|
|
zero_amt = testutil.Amount(0, currency)
|
|
|
|
zero_bal = core.Balance([zero_amt])
|
|
|
|
assert zero_bal.format(zero=fmt) == fmt
|
2020-06-07 15:59:31 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('tolerance', TOLERANCES)
|
2020-06-18 18:07:44 +00:00
|
|
|
def test_format_zero_balance_with_tolerance(tolerance):
|
2020-06-07 15:59:31 +00:00
|
|
|
chf = testutil.Amount('.005', 'CHF')
|
2020-06-18 18:07:44 +00:00
|
|
|
actual = core.Balance([chf]).format(zero="ø", tolerance=tolerance)
|
2020-06-07 15:59:31 +00:00
|
|
|
if tolerance > chf.number:
|
2020-06-18 18:07:44 +00:00
|
|
|
assert actual == "ø"
|
2020-06-07 15:59:31 +00:00
|
|
|
else:
|
2020-06-18 18:07:44 +00:00
|
|
|
assert actual == "0.00 CHF"
|