File diff 6801d1235969 → 9595d3334d5b
tests/test_books_loader.py
Show inline comments
...
 
@@ -11,29 +11,45 @@
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import collections
 
import re
 

	
 
from datetime import date
 
from pathlib import Path
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from beancount.core import data as bc_data
 
from conservancy_beancount import books
 

	
 
books_path = testutil.test_path('books')
 

	
 
@pytest.fixture(scope='module')
 
def conservancy_loader():
 
    return books.Loader(books_path, books.FiscalYear(3))
 

	
 
def check_openings(entries):
 
    openings = collections.defaultdict(int)
 
    for entry in entries:
 
        if isinstance(entry, bc_data.Open):
 
            openings[entry.account] += 1
 
    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)
 
    }
 

	
 
@pytest.mark.parametrize('from_fy,to_fy,expect_years', [
 
    (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)),
...
 
@@ -44,29 +60,29 @@ def conservancy_loader():
 
    (20, 2010, range(2018, 2021)),
 
    (-20, 2030, range(2018, 2021)),
 
])
 
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 = {getattr(entry, 'narration', None) for entry in entries}
 
    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)
 

	
 
def test_load_fy_range_does_not_duplicate_openings(conservancy_loader):
 
    entries, errors, options_map = conservancy_loader.load_fy_range(2010, 2030)
 
    openings = []
 
    open_accounts = set()
 
    for entry in entries:
 
        try:
 
            open_accounts.add(entry.account)
 
        except AttributeError:
 
            pass
 
        else:
 
            openings.append(entry)
 
    assert len(openings) == len(open_accounts)
 
    check_openings(entries)
 

	
 
def test_load_fy_range_empty(conservancy_loader):
 
    entries, errors, options_map = conservancy_loader.load_fy_range(2020, 2019)
 
    assert not errors
 
    assert not entries
 
    assert not options_map
 

	
 
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_openings(entries)