diff --git a/tests/test_books_loader.py b/tests/test_books_loader.py index 058ad75be59e53cd52ca598759fe769c0b48ea31..02cb5ee94ecedc99481e2848a2a4d6a45e7fc7c0 100644 --- a/tests/test_books_loader.py +++ b/tests/test_books_loader.py @@ -15,6 +15,7 @@ # along with this program. If not, see . import hashlib +import re from datetime import date from pathlib import Path @@ -25,59 +26,54 @@ from . import testutil from conservancy_beancount import books -books_path = testutil.test_path('booksroot') +books_path = testutil.test_path('books') @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), - ]) +def include_patterns(years, subdir='..'): + for year in years: + path = Path(subdir, f'{year}.beancount') + yield rf'^include "{re.escape(str(path))}"$' @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)), + (10, 2019, [2019, 2020]), + (-10, 2019, [2018, 2019]), + (date(2019, 1, 1), date(2020, 6, 1), [2018, 2019, 2020]), (-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 + actual = conservancy_loader.fy_range_string(range_start, range_stop) + testutil.check_lines_match(actual.splitlines(), [ + rf'^option "title" "Books from {expect_years[0]}"$', + rf'^plugin "beancount\.plugins\.auto"$', + *include_patterns(expect_years), + ]) @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_with_offset(conservancy_loader, year_offset): + base_year = 2020 + start_year = max(2018, base_year + year_offset) + expect_years = range(start_year, base_year + 1) + actual = conservancy_loader.fy_range_string(year_offset, base_year) + testutil.check_lines_match(actual.splitlines(), include_patterns(expect_years)) def test_fy_range_string_empty_range(conservancy_loader): assert conservancy_loader.fy_range_string(2020, 2019) == '' +def test_load_fy_range(conservancy_loader): + entries, errors, options_map = conservancy_loader.load_fy_range(2018, 2019) + assert not errors + narrations = {getattr(entry, 'narration', None) for entry in entries} + assert '2018 donation' in narrations + assert '2019 donation' in narrations + assert '2020 donation' not in narrations + 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 options_map.get('input_hash') == hashlib.md5().hexdigest()