2020-04-21 14:47:13 +00:00
|
|
|
"""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/>.
|
|
|
|
|
2020-04-21 15:58:28 +00:00
|
|
|
import hashlib
|
2020-05-05 18:31:08 +00:00
|
|
|
import re
|
2020-04-21 15:58:28 +00:00
|
|
|
|
2020-04-21 14:47:13 +00:00
|
|
|
from datetime import date
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount import books
|
|
|
|
|
2020-05-05 18:31:08 +00:00
|
|
|
books_path = testutil.test_path('books')
|
2020-04-21 14:47:13 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def conservancy_loader():
|
|
|
|
return books.Loader(books_path, books.FiscalYear(3))
|
|
|
|
|
2020-05-16 14:27:06 +00:00
|
|
|
@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)),
|
|
|
|
(2019, 2020, range(2019, 2021)),
|
|
|
|
(1, 2019, range(2019, 2021)),
|
|
|
|
(-1, 2020, range(2019, 2021)),
|
|
|
|
(2010, 2030, range(2018, 2021)),
|
|
|
|
(20, 2010, range(2018, 2021)),
|
|
|
|
(-20, 2030, range(2018, 2021)),
|
2020-04-21 14:47:13 +00:00
|
|
|
])
|
2020-05-16 14:27:06 +00:00
|
|
|
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)
|
2020-05-05 18:31:08 +00:00
|
|
|
assert not errors
|
|
|
|
narrations = {getattr(entry, 'narration', None) for entry in entries}
|
2020-05-16 14:27:06 +00:00
|
|
|
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)
|
2020-05-05 18:31:08 +00:00
|
|
|
|
2020-04-21 15:58:28 +00:00
|
|
|
def test_load_fy_range_empty(conservancy_loader):
|
|
|
|
entries, errors, options_map = conservancy_loader.load_fy_range(2020, 2019)
|
2020-05-05 18:31:08 +00:00
|
|
|
assert not errors
|
2020-04-21 15:58:28 +00:00
|
|
|
assert not entries
|
2020-05-16 14:27:06 +00:00
|
|
|
assert not options_map
|