From 3721226d17fe7ec214ffc9bcd7ad9eb79f56b52a Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Fri, 19 Feb 2021 11:52:40 -0500 Subject: [PATCH] books: Add Loader.dispatch() classmethod. --- conservancy_beancount/books.py | 13 +++++++++ tests/test_books_loader.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/conservancy_beancount/books.py b/conservancy_beancount/books.py index 09a8799..42bf434 100644 --- a/conservancy_beancount/books.py +++ b/conservancy_beancount/books.py @@ -254,3 +254,16 @@ class Loader: 'lineno': lineno, } return LoadResult.empty(Error(source, "no books to load in configuration", None)) + + @classmethod + def dispatch(cls, + loader: Optional['Loader'], + from_fy: Optional[Year]=None, + to_fy: Optional[Year]=None, + ) -> LoadResult: + if loader is None: + return cls.load_none() + elif to_fy is None: + return loader.load_all(from_fy) + else: + return loader.load_fy_range(from_fy or 0, to_fy) diff --git a/tests/test_books_loader.py b/tests/test_books_loader.py index 9efed3c..e5819bd 100644 --- a/tests/test_books_loader.py +++ b/tests/test_books_loader.py @@ -225,3 +225,52 @@ def test_load_none_no_args(): assert errors assert all(isinstance(err.source['filename'], str) for err in errors) assert all(isinstance(err.source['lineno'], int) for err in errors) + +def test_dispatch_empty(): + result = books.Loader.dispatch(None) + assert not result.entries + assert result.errors + +@pytest.mark.parametrize('from_arg', [ + None, + *range(2018, 2021), + date(2019, 2, 1), + date(2019, 9, 15), + date(2020, 1, 20), + date(2020, 5, 31), +]) +def test_dispatch_load_all_from_year(conservancy_loader, from_arg): + try: + from_year = from_arg.year + except AttributeError: + from_year = from_arg or 2018 + else: + if from_arg.month < FY_START_MONTH: + from_year -= 1 + result = books.Loader.dispatch(conservancy_loader, from_arg) + check_openings(result.entries) + actual_years = txn_years(result.entries) + assert actual_years.issuperset(range(from_year, 2021)) + assert min(actual_years) == from_year + assert not result.errors + +@pytest.mark.parametrize('from_arg,to_arg,expected', [ + (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_dispatch_load_all_fy_range(conservancy_loader, from_arg, to_arg, expected): + result = books.Loader.dispatch(conservancy_loader, from_arg, to_arg) + check_openings(result.entries) + actual_years = txn_years(result.entries) + assert actual_years.issuperset(iter(expected)) + assert min(actual_years) == expected.start + assert not result.errors