From fea306b2783f5d469c37931f7bfe8fd75744433c Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Mon, 25 May 2020 11:16:17 -0400 Subject: [PATCH] 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. --- conservancy_beancount/books.py | 2 +- tests/test_books_loader.py | 27 ++++++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/conservancy_beancount/books.py b/conservancy_beancount/books.py index e3fac9a..9f3a2b1 100644 --- a/conservancy_beancount/books.py +++ b/conservancy_beancount/books.py @@ -144,7 +144,7 @@ class Loader: """ path = Path(self.books_root, 'books') 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)) def load_fy_range(self, diff --git a/tests/test_books_loader.py b/tests/test_books_loader.py index ce6ae87..7b3ce6d 100644 --- a/tests/test_books_loader.py +++ b/tests/test_books_loader.py @@ -41,11 +41,18 @@ def check_openings(entries): for account, count in openings.items(): assert count == 1, f"found {count} open directives for {account}" -def get_narrations(entries): - return { - entry.narration for entry in entries - if isinstance(entry, bc_data.Transaction) - } +def check_narrations(entries, expected): + expected = iter(expected) + expected_next = next(expected) + 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', [ (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): entries, errors, options_map = conservancy_loader.load_fy_range(from_fy, to_fy) assert not errors - narrations = get_narrations(entries) - 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) + check_narrations(entries, [f'{year} donation' for year in expect_years]) def test_load_fy_range_does_not_duplicate_openings(conservancy_loader): 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): entries, errors, options_map = conservancy_loader.load_all() assert not errors - narrations = get_narrations(entries) - assert '2018 donation' in narrations - assert '2019 donation' in narrations - assert '2020 donation' in narrations + check_narrations(entries, [f'{year} donation' for year in range(2018, 2021)]) check_openings(entries)