conservancy_beancount/tests/test_books_loader.py

84 lines
3 KiB
Python
Raw Normal View History

2020-04-21 14:47:13 +00:00
"""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 hashlib
2020-04-21 14:47:13 +00:00
from datetime import date
from pathlib import Path
import pytest
from . import testutil
from conservancy_beancount import books
books_path = testutil.test_path('booksroot')
@pytest.fixture(scope='module')
def conservancy_loader():
return books.Loader(books_path, books.FiscalYear(3))
def format_include(year, subdir=''):
path = Path(books_path, subdir, f'{year}.beancount')
return f'include "{path}"'
def format_plugin(name, optstring=None):
if optstring is None:
return f'plugin "{name}"'
else:
return f'plugin "{name}" "{optstring}"'
def expect_string(years, plugins={'conservancy_beancount.plugin': None}):
years = iter(years)
year1_s = format_include(next(years), 'books')
return '\n'.join([
*(format_plugin(name, opts) for name, opts in plugins.items()),
year1_s,
*(format_include(year) for year in years),
])
@pytest.mark.parametrize('range_start,range_stop,expect_years', [
(2019, 2020, [2019, 2020]),
(-1, 2020, [2019, 2020]),
(date(2019, 1, 1), date(2020, 6, 1), range(2018, 2021)),
(-1, date(2020, 2, 1), [2018, 2019]),
])
def test_fy_range_string(conservancy_loader, range_start, range_stop, expect_years):
expected = expect_string(expect_years)
assert conservancy_loader.fy_range_string(range_start, range_stop) == expected
@pytest.mark.parametrize('year_offset', range(-3, 1))
def test_fy_range_string_one_offset(conservancy_loader, year_offset):
this_year = date.today().year
expected = expect_string(range(this_year + year_offset, this_year + 1))
assert conservancy_loader.fy_range_string(year_offset) == expected
@pytest.mark.parametrize('plugins', [
{},
{'conservancy_beancount.plugin': '-all'},
])
def test_fy_range_string_plugins_override(conservancy_loader, plugins):
expected = expect_string([2019, 2020], plugins)
assert conservancy_loader.fy_range_string(2019, 2020, plugins) == expected
def test_fy_range_string_empty_range(conservancy_loader):
assert conservancy_loader.fy_range_string(2020, 2019) == ''
def test_load_fy_range_empty(conservancy_loader):
entries, errors, options_map = conservancy_loader.load_fy_range(2020, 2019)
assert not entries
assert options_map.get('input_hash') == hashlib.md5().hexdigest()