Changeset - 3721226d17fe
[Not reviewed]
0 2 0
Brett Smith - 3 years ago 2021-02-24 18:15:33
brettcsmith@brettcsmith.org
books: Add Loader.dispatch() classmethod.
2 files changed with 62 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/books.py
Show inline comments
...
 
@@ -245,12 +245,25 @@ class Loader:
 
        general Beancount errors. If a configuration problem prevents them from
 
        loading the books, they can call this method in place of a regular
 
        loading method, and then continue on their normal code path.
 

	
 
        The path and line number given in the arguments will be named as the
 
        source of the error.
 
        """
 
        source = {
 
            'filename': str(config_path or 'conservancy_beancount.ini'),
 
            '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)
tests/test_books_loader.py
Show inline comments
...
 
@@ -216,12 +216,61 @@ 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)
 

	
 
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
0 comments (0 inline, 0 general)