conservancy_beancount/tests/test_plugin_txn_date.py

71 lines
2.2 KiB
Python
Raw Normal View History

"""test_txn_date.py - Unit tests for transaction date validation"""
# Copyright © 2020 Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.
from datetime import date
import pytest
from . import testutil
from conservancy_beancount import config as configmod
from conservancy_beancount import errors as errormod
from conservancy_beancount.plugin import txn_date as hookmod
BOOKS_PATH = testutil.test_path('books')
CONFIG = testutil.TestConfig(books_path=BOOKS_PATH)
HOOK = hookmod.TransactionDate(CONFIG)
@pytest.mark.parametrize('txn_date,fyear', [
(date(2016, 1, 1), 2015),
(date(2016, 2, 29), 2015),
(date(2016, 3, 1), 2016),
(date(2016, 12, 31), 2016),
(date(2017, 2, 28), 2016),
(date(2017, 3, 1), 2017),
])
def test_good_txn(txn_date, fyear):
filename = str(BOOKS_PATH / f'{fyear}.beancount')
txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
('Assets:Cash', 5),
('Income:Donations', -5),
])
assert not list(HOOK.run(txn))
@pytest.mark.parametrize('txn_date,fyear', [
(date(2018, 1, 1), 2017),
(date(2018, 12, 31), 2018),
(date(2019, 3, 1), 2019),
])
def test_bad_txn(txn_date, fyear):
filename = str(BOOKS_PATH / '2020.beancount')
txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
('Assets:Cash', 5),
('Income:Donations', -5),
])
errors = list(HOOK.run(txn))
assert len(errors) == 1
assert errors[0].message == f"transaction dated in FY{fyear} entered in FY2020 books"
@pytest.mark.parametrize('path_s', [
'books/2020.beancount',
'historical/2020.beancount',
'definitions.beancount',
])
def test_outer_transactions_not_checked(path_s):
txn_date = date(1900, 6, 15)
filename = str(BOOKS_PATH / path_s)
txn = testutil.Transaction(date=txn_date, filename=filename, postings=[
('Assets:Cash', 5),
('Income:Donations', -5),
])
assert not list(HOOK.run(txn))
def test_error_without_books_path():
config = configmod.Config()
with pytest.raises(errormod.ConfigurationError):
hookmod.TransactionDate(config)