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:
		
							parent
							
								
									9595d3334d
								
							
						
					
					
						commit
						fea306b278
					
				
					 2 changed files with 15 additions and 14 deletions
				
			
		| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue