Files
@ 7baa78fe3473
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_books_loader.py
7baa78fe3473
4.2 KiB
text/x-python
ledger: Add Equity back to default project report sheets.
While many projects often won't have anything here, this is necessary to
show project funds released from restrictions.
While many projects often won't have anything here, this is necessary to
show project funds released from restrictions.
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 | """test_books_loader - Unit tests for books Loader class"""
# 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 collections
import re
from datetime import date
from pathlib import Path
import pytest
from . import testutil
from beancount.core import data as bc_data
from conservancy_beancount import books
FY_START_MONTH = 3
books_path = testutil.test_path('books')
@pytest.fixture(scope='module')
def conservancy_loader():
return books.Loader(books_path, books.FiscalYear(FY_START_MONTH))
def check_openings(entries):
openings = collections.defaultdict(int)
for entry in entries:
if isinstance(entry, bc_data.Open):
openings[entry.account] += 1
for account, count in openings.items():
assert count == 1, f"found {count} open directives for {account}"
def txn_dates(entries):
for entry in entries:
if isinstance(entry, bc_data.Transaction):
yield entry.date
def txn_years(entries):
return frozenset(date.year for date in txn_dates(entries))
@pytest.mark.parametrize('from_fy,to_fy,expect_years', [
(2019, 2019, range(2019, 2020)),
(0, 2019, range(2019, 2020)),
(2018, 2019, range(2018, 2020)),
(1, 2018, range(2018, 2020)),
(-1, 2019, range(2018, 2020)),
(2019, 2020, range(2019, 2021)),
(1, 2019, range(2019, 2021)),
(-1, 2020, range(2019, 2021)),
(2010, 2030, range(2018, 2021)),
(20, 2010, range(2018, 2021)),
(-20, 2030, range(2018, 2021)),
])
def test_load_fy_range(conservancy_loader, from_fy, to_fy, expect_years):
entries, errors, options_map = conservancy_loader.load_fy_range(from_fy, to_fy)
assert not errors
actual_years = txn_years(entries)
assert actual_years.issuperset(expect_years)
assert min(actual_years) == expect_years.start
def test_load_fy_range_does_not_duplicate_openings(conservancy_loader):
entries, errors, options_map = conservancy_loader.load_fy_range(2010, 2030)
check_openings(entries)
def test_load_fy_range_empty(conservancy_loader):
entries, errors, options_map = conservancy_loader.load_fy_range(2020, 2019)
assert not errors
assert not entries
assert not options_map
@pytest.mark.parametrize('from_year', [None, *range(2018, 2021)])
def test_load_all(conservancy_loader, from_year):
entries, errors, options_map = conservancy_loader.load_all(from_year)
from_year = from_year or 2018
assert not errors
check_openings(entries)
actual_years = txn_years(entries)
assert actual_years.issuperset(range(from_year, 2021))
assert min(actual_years) == from_year
@pytest.mark.parametrize('from_date', [
date(2019, 2, 1),
date(2019, 9, 15),
date(2020, 1, 20),
date(2020, 5, 31),
])
def test_load_all_from_date(conservancy_loader, from_date):
from_year = from_date.year
if from_date.month < FY_START_MONTH:
from_year -= 1
entries, errors, options_map = conservancy_loader.load_all(from_date)
assert not errors
check_openings(entries)
actual_years = txn_years(entries)
assert actual_years.issuperset(range(from_year, 2021))
assert min(actual_years) == from_year
def test_load_none_full_args():
entries, errors, options_map = books.Loader.load_none('test.cfg', 42)
assert not entries
assert errors
assert all(err.source['filename'] == 'test.cfg' for err in errors)
assert all(err.source['lineno'] == 42 for err in errors)
def test_load_none_no_args():
entries, errors, options_map = books.Loader.load_none()
assert not entries
assert errors
assert all(isinstance(err.source['filename'], str) for err in errors)
assert all(isinstance(err.source['lineno'], int) for err in errors)
|