diff --git a/tests/test_plugin_txn_date.py b/tests/test_plugin_txn_date.py new file mode 100644 index 0000000000000000000000000000000000000000..8c1c056457cf77d675e5f775942db1d64e6b47f5 --- /dev/null +++ b/tests/test_plugin_txn_date.py @@ -0,0 +1,79 @@ +"""test_txn_date.py - Unit tests for transaction date validation""" +# 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 . + +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)