conservancy_beancount/tests/test_reports_core.py
Brett Smith 7a9bc2da50 reports: Add sort_and_filter_accounts() function.
Extracted from the ledger report.
2020-06-20 09:11:01 -04:00

127 lines
3.9 KiB
Python

"""test_reports_core - Unit tests for basic reports functions"""
# 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 pytest
from decimal import Decimal
from . import testutil
from conservancy_beancount.reports import core
from conservancy_beancount.data import Account
AMOUNTS = [
2,
Decimal('4.40'),
testutil.Amount('6.60', 'CHF'),
core.Balance([testutil.Amount('8.80')]),
]
@pytest.mark.parametrize('acct_name', [
'Assets:Checking',
'Assets:Receivable:Accounts',
'Expenses:Other',
'Expenses:FilingFees',
])
def test_normalize_amount_func_pos(acct_name):
actual = core.normalize_amount_func(acct_name)
for amount in AMOUNTS:
assert actual(amount) == amount
@pytest.mark.parametrize('acct_name', [
'Equity:Funds:Restricted',
'Equity:Realized:CurrencyConversion',
'Income:Donations',
'Income:Other',
'Liabilities:CreditCard',
'Liabilities:Payable:Accounts',
])
def test_normalize_amount_func_neg(acct_name):
actual = core.normalize_amount_func(acct_name)
for amount in AMOUNTS:
assert actual(amount) == -amount
@pytest.mark.parametrize('acct_name', [
'',
'Assets',
'Equity',
'Expenses',
'Income',
'Liabilities',
])
def test_normalize_amount_func_bad_acct_name(acct_name):
with pytest.raises(ValueError):
core.normalize_amount_func(acct_name)
def test_sort_and_filter_accounts():
accounts = (Account(s) for s in [
'Expenses:Services',
'Assets:Receivable',
'Income:Other',
'Liabilities:Payable',
'Equity:Funds:Unrestricted',
'Income:Donations',
'Expenses:Other',
])
actual = core.sort_and_filter_accounts(accounts, ['Equity', 'Income', 'Expenses'])
assert list(actual) == [
(0, 'Equity:Funds:Unrestricted'),
(1, 'Income:Donations'),
(1, 'Income:Other'),
(2, 'Expenses:Other'),
(2, 'Expenses:Services'),
]
def test_sort_and_filter_accounts_unused_name():
accounts = (Account(s) for s in [
'Liabilities:CreditCard',
'Assets:Cash',
'Assets:Receivable:Accounts',
])
actual = core.sort_and_filter_accounts(
accounts, ['Assets:Receivable', 'Liabilities:Payable', 'Assets', 'Liabilities'],
)
assert list(actual) == [
(0, 'Assets:Receivable:Accounts'),
(2, 'Assets:Cash'),
(3, 'Liabilities:CreditCard'),
]
def test_sort_and_filter_accounts_with_subaccounts():
accounts = (Account(s) for s in [
'Assets:Checking',
'Assets:Receivable:Fraud',
'Assets:Cash',
'Assets:Receivable:Accounts',
])
actual = core.sort_and_filter_accounts(accounts, ['Assets:Receivable', 'Assets'])
assert list(actual) == [
(0, 'Assets:Receivable:Accounts'),
(0, 'Assets:Receivable:Fraud'),
(1, 'Assets:Cash'),
(1, 'Assets:Checking'),
]
@pytest.mark.parametrize('empty_arg', ['accounts', 'order'])
def test_sort_and_filter_accounts_empty_accounts(empty_arg):
accounts = [Account(s) for s in ['Expenses:Other', 'Income:Other']]
if empty_arg == 'accounts':
args = ([], accounts)
else:
args = (accounts, [])
actual = core.sort_and_filter_accounts(*args)
assert next(actual, None) is None