books: Add Loader.dispatch() classmethod.

This commit is contained in:
Brett Smith 2021-02-19 11:52:40 -05:00
parent f3c3ebcf59
commit 3721226d17
2 changed files with 62 additions and 0 deletions

View file

@ -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)

View file

@ -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