books.Loader: Ensure load_all properly sorts in chronological order.

The test changes make them order-sensitive, which they should be.
It's important that our loader methods return date-sorted entries
just like Beancount itself would.
This commit is contained in:
Brett Smith 2020-05-25 11:16:17 -04:00
parent 9595d3334d
commit fea306b278
2 changed files with 15 additions and 14 deletions

View file

@ -144,7 +144,7 @@ class Loader:
""" """
path = Path(self.books_root, 'books') path = Path(self.books_root, 'books')
fy_paths = list(path.glob('[1-9][0-9][0-9][0-9].beancount')) fy_paths = list(path.glob('[1-9][0-9][0-9][0-9].beancount'))
fy_paths.sort() fy_paths.sort(key=lambda path: int(path.stem))
return self._load_paths(iter(fy_paths)) return self._load_paths(iter(fy_paths))
def load_fy_range(self, def load_fy_range(self,

View file

@ -41,11 +41,18 @@ def check_openings(entries):
for account, count in openings.items(): for account, count in openings.items():
assert count == 1, f"found {count} open directives for {account}" assert count == 1, f"found {count} open directives for {account}"
def get_narrations(entries): def check_narrations(entries, expected):
return { expected = iter(expected)
entry.narration for entry in entries expected_next = next(expected)
if isinstance(entry, bc_data.Transaction) for entry in entries:
} if (isinstance(entry, bc_data.Transaction)
and entry.narration == expected_next):
try:
expected_next = next(expected)
except StopIteration:
break
else:
assert None, f"{expected_next} not found in entry narrations"
@pytest.mark.parametrize('from_fy,to_fy,expect_years', [ @pytest.mark.parametrize('from_fy,to_fy,expect_years', [
(2019, 2019, range(2019, 2020)), (2019, 2019, range(2019, 2020)),
@ -63,10 +70,7 @@ def get_narrations(entries):
def test_load_fy_range(conservancy_loader, from_fy, to_fy, expect_years): def test_load_fy_range(conservancy_loader, from_fy, to_fy, expect_years):
entries, errors, options_map = conservancy_loader.load_fy_range(from_fy, to_fy) entries, errors, options_map = conservancy_loader.load_fy_range(from_fy, to_fy)
assert not errors assert not errors
narrations = get_narrations(entries) check_narrations(entries, [f'{year} donation' for year in expect_years])
assert ('2018 donation' in narrations) == (2018 in expect_years)
assert ('2019 donation' in narrations) == (2019 in expect_years)
assert ('2020 donation' in narrations) == (2020 in expect_years)
def test_load_fy_range_does_not_duplicate_openings(conservancy_loader): def test_load_fy_range_does_not_duplicate_openings(conservancy_loader):
entries, errors, options_map = conservancy_loader.load_fy_range(2010, 2030) entries, errors, options_map = conservancy_loader.load_fy_range(2010, 2030)
@ -81,8 +85,5 @@ def test_load_fy_range_empty(conservancy_loader):
def test_load_all(conservancy_loader): def test_load_all(conservancy_loader):
entries, errors, options_map = conservancy_loader.load_all() entries, errors, options_map = conservancy_loader.load_all()
assert not errors assert not errors
narrations = get_narrations(entries) check_narrations(entries, [f'{year} donation' for year in range(2018, 2021)])
assert '2018 donation' in narrations
assert '2019 donation' in narrations
assert '2020 donation' in narrations
check_openings(entries) check_openings(entries)