Files
@ 68c2c1e6f87c
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_reports_balance.py
68c2c1e6f87c
4.7 KiB
text/x-python
reports.accrual: Sort imports.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | """test_reports_balance - Unit tests for reports.core.Balance"""
# Copyright © 2020 Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# 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 itertools
from decimal import Decimal
import pytest
from . import testutil
from conservancy_beancount.reports import core
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()
balance = core.Balance(testutil.balance_map((key, 0) for key in keys))
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):
amounts = testutil.balance_map(zip(currencies.split(), itertools.count(110, 100)))
balance = core.Balance(amounts.items())
assert balance
assert len(balance) == len(amounts)
assert not balance.is_zero()
assert all(balance[key] == amt for key, amt in amounts.items())
def test_mixed_balance():
amounts = testutil.balance_map(USD=0, EUR=120)
balance = core.Balance(amounts.items())
assert balance
assert len(balance) == 2
assert not balance.is_zero()
assert all(balance[key] == amt for key, amt in amounts.items())
@pytest.mark.parametrize('balance_map_kwargs,expected', [
({}, 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),
])
def test_eq_zero(balance_map_kwargs, expected):
amounts = testutil.balance_map(**balance_map_kwargs)
balance = core.Balance(amounts.items())
assert balance.eq_zero() == expected
assert balance.is_zero() == expected
@pytest.mark.parametrize('balance_map_kwargs,expected', [
({}, 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),
])
def test_ge_zero(balance_map_kwargs, expected):
amounts = testutil.balance_map(**balance_map_kwargs)
balance = core.Balance(amounts.items())
assert balance.ge_zero() == expected
@pytest.mark.parametrize('balance_map_kwargs,expected', [
({}, 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),
])
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_neg(balance_map_kwargs):
amounts = testutil.balance_map(**balance_map_kwargs)
actual = -core.Balance(amounts.items())
assert set(actual) == set(balance_map_kwargs)
for key in balance_map_kwargs:
assert actual[key] == -amounts[key]
@pytest.mark.parametrize('balance_map_kwargs,expected', [
({}, "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': '-55.00', 'BRL': '-85.00'}, "-85.00 BRL, -55.00 JPY"),
])
def test_str(balance_map_kwargs, expected):
amounts = testutil.balance_map(**balance_map_kwargs)
assert str(core.Balance(amounts.items())) == expected
|