Files @ 1e09339b32f6
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_books_loader.py

Brett Smith
config: Make payment threshold configurable.
"""test_books_loader - Unit tests for books Loader class"""
# Copyright © 2020  Brett Smith
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# 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 hashlib
import re

from datetime import date
from pathlib import Path

import pytest

from . import testutil

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 include_patterns(years, subdir='..'):
    for year in years:
        path = Path(subdir, f'{year}.beancount')
        yield rf'^include "{re.escape(str(path))}"$'

@pytest.mark.parametrize('range_start,range_stop,expect_years', [
    (2019, 2020, [2019, 2020]),
    (-1, 2020, [2019, 2020]),
    (10, 2019, [2019, 2020]),
    (-10, 2019, [2018, 2019]),
    (date(2019, 1, 1), date(2020, 6, 1), [2018, 2019, 2020]),
    (-1, date(2020, 2, 1), [2018, 2019]),
])
def test_fy_range_string(conservancy_loader, range_start, range_stop, expect_years):
    actual = conservancy_loader.fy_range_string(range_start, range_stop)
    testutil.check_lines_match(actual.splitlines(), [
        rf'^option "title" "Books from {expect_years[0]}"$',
        rf'^plugin "beancount\.plugins\.auto"$',
        *include_patterns(expect_years),
    ])

@pytest.mark.parametrize('year_offset', range(-3, 1))
def test_fy_range_string_with_offset(conservancy_loader, year_offset):
    base_year = 2020
    start_year = max(2018, base_year + year_offset)
    expect_years = range(start_year, base_year + 1)
    actual = conservancy_loader.fy_range_string(year_offset, base_year)
    testutil.check_lines_match(actual.splitlines(), include_patterns(expect_years))

def test_fy_range_string_empty_range(conservancy_loader):
    assert conservancy_loader.fy_range_string(2020, 2019) == ''

def test_load_fy_range(conservancy_loader):
    entries, errors, options_map = conservancy_loader.load_fy_range(2018, 2019)
    assert not errors
    narrations = {getattr(entry, 'narration', None) for entry in entries}
    assert '2018 donation' in narrations
    assert '2019 donation' in narrations
    assert '2020 donation' not in narrations

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 options_map.get('input_hash') == hashlib.md5().hexdigest()