2020-04-21 14:47:13 +00:00
|
|
|
"""test_books_loader - Unit tests for books Loader class"""
|
|
|
|
# Copyright © 2020 Brett Smith
|
2021-01-08 21:57:43 +00:00
|
|
|
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
|
2020-04-21 14:47:13 +00:00
|
|
|
#
|
2021-01-08 21:57:43 +00:00
|
|
|
# Full copyright and licensing details can be found at toplevel file
|
|
|
|
# LICENSE.txt in the repository.
|
2020-04-21 14:47:13 +00:00
|
|
|
|
2020-05-25 14:37:21 +00:00
|
|
|
import collections
|
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
|
|
|
|
|
2020-05-25 14:37:21 +00:00
|
|
|
from beancount.core import data as bc_data
|
2020-04-21 14:47:13 +00:00
|
|
|
from conservancy_beancount import books
|
|
|
|
|
2020-06-04 13:03:10 +00:00
|
|
|
FY_START_MONTH = 3
|
|
|
|
|
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():
|
2020-06-04 13:03:10 +00:00
|
|
|
return books.Loader(books_path, books.FiscalYear(FY_START_MONTH))
|
2020-04-21 14:47:13 +00:00
|
|
|
|
2020-05-25 14:37:21 +00:00
|
|
|
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}"
|
|
|
|
|
2020-06-04 13:03:10 +00:00
|
|
|
def txn_dates(entries):
|
2020-05-25 15:16:17 +00:00
|
|
|
for entry in entries:
|
2020-06-04 13:03:10 +00:00
|
|
|
if isinstance(entry, bc_data.Transaction):
|
|
|
|
yield entry.date
|
|
|
|
|
|
|
|
def txn_years(entries):
|
|
|
|
return frozenset(date.year for date in txn_dates(entries))
|
2020-05-25 14:37:21 +00:00
|
|
|
|
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
|
2020-06-04 13:15:23 +00:00
|
|
|
actual_years = txn_years(entries)
|
|
|
|
assert actual_years.issuperset(expect_years)
|
|
|
|
assert min(actual_years) == expect_years.start
|
2020-05-16 14:27:06 +00:00
|
|
|
|
|
|
|
def test_load_fy_range_does_not_duplicate_openings(conservancy_loader):
|
|
|
|
entries, errors, options_map = conservancy_loader.load_fy_range(2010, 2030)
|
2020-05-25 14:37:21 +00:00
|
|
|
check_openings(entries)
|
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
|
2020-05-25 14:37:21 +00:00
|
|
|
|
2020-06-04 13:03:10 +00:00
|
|
|
@pytest.mark.parametrize('from_year', [None, *range(2018, 2021)])
|
|
|
|
def test_load_all(conservancy_loader, from_year):
|
|
|
|
entries, errors, options_map = conservancy_loader.load_all(from_year)
|
|
|
|
from_year = from_year or 2018
|
|
|
|
assert not errors
|
|
|
|
check_openings(entries)
|
2020-06-04 13:15:23 +00:00
|
|
|
actual_years = txn_years(entries)
|
|
|
|
assert actual_years.issuperset(range(from_year, 2021))
|
|
|
|
assert min(actual_years) == from_year
|
2020-06-04 13:03:10 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('from_date', [
|
|
|
|
date(2019, 2, 1),
|
|
|
|
date(2019, 9, 15),
|
|
|
|
date(2020, 1, 20),
|
|
|
|
date(2020, 5, 31),
|
|
|
|
])
|
|
|
|
def test_load_all_from_date(conservancy_loader, from_date):
|
|
|
|
from_year = from_date.year
|
|
|
|
if from_date.month < FY_START_MONTH:
|
|
|
|
from_year -= 1
|
|
|
|
entries, errors, options_map = conservancy_loader.load_all(from_date)
|
2020-05-25 14:37:21 +00:00
|
|
|
assert not errors
|
|
|
|
check_openings(entries)
|
2020-06-04 13:15:23 +00:00
|
|
|
actual_years = txn_years(entries)
|
|
|
|
assert actual_years.issuperset(range(from_year, 2021))
|
|
|
|
assert min(actual_years) == from_year
|
2020-06-07 13:04:53 +00:00
|
|
|
|
|
|
|
def test_load_none_full_args():
|
|
|
|
entries, errors, options_map = books.Loader.load_none('test.cfg', 42)
|
|
|
|
assert not entries
|
|
|
|
assert errors
|
|
|
|
assert all(err.source['filename'] == 'test.cfg' for err in errors)
|
|
|
|
assert all(err.source['lineno'] == 42 for err in errors)
|
|
|
|
|
|
|
|
def test_load_none_no_args():
|
|
|
|
entries, errors, options_map = books.Loader.load_none()
|
|
|
|
assert not entries
|
|
|
|
assert errors
|
|
|
|
assert all(isinstance(err.source['filename'], str) for err in errors)
|
|
|
|
assert all(isinstance(err.source['lineno'], int) for err in errors)
|