diff --git a/conservancy_beancount/plugin/core.py b/conservancy_beancount/plugin/core.py index e94b4771d24f89f49086fa66c82b683599047713..241d7f92cf893a48e6f5fff13756ba518e7b2aae 100644 --- a/conservancy_beancount/plugin/core.py +++ b/conservancy_beancount/plugin/core.py @@ -252,12 +252,18 @@ class _RequireLinksPostingMetadataHook(_PostingHook): super().__init_subclass__() cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(['metadata', cls.METADATA_KEY]) - def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: + def _check_links(self, txn: Transaction, post: data.Posting, key: MetaKey) -> None: try: - problem = not post.meta.get_links(self.METADATA_KEY) + problem = not post.meta.get_links(key) value = None except TypeError: problem = True - value = post.meta[self.METADATA_KEY] + value = post.meta[key] if problem: - yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, value, post) + raise errormod.InvalidMetadataError(txn, key, value, post) + + def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: + try: + self._check_links(txn, post, self.METADATA_KEY) + except errormod.Error as error: + yield error diff --git a/conservancy_beancount/plugin/meta_receipt.py b/conservancy_beancount/plugin/meta_receipt.py new file mode 100644 index 0000000000000000000000000000000000000000..0c4655993e94811c567ff2c026953b99090cb614 --- /dev/null +++ b/conservancy_beancount/plugin/meta_receipt.py @@ -0,0 +1,56 @@ +"""meta_receipt - Validate receipt metadata""" +# 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 . import core +from .. import data +from .. import errors as errormod +from ..beancount_types import ( + MetaValueEnum, + Transaction, +) + +class MetaReceipt(core._RequireLinksPostingMetadataHook): + DEFAULT_STOP_AMOUNT = 0 + METADATA_KEY = 'receipt' + + def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: + return bool( + (post.account.is_real_asset() or post.account.is_under('Liabilities')) + and post.units.number + and post.units.number < self.DEFAULT_STOP_AMOUNT + ) + + def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter: + try: + self._check_links(txn, post, 'receipt') + except errormod.Error as receipt_error: + # Outgoing check may omit a receipt if they have a check link + # instead. + if ':Check' not in post.account: + yield receipt_error + else: + try: + self._check_links(txn, post, 'check') + except errormod.Error as check_error: + if (receipt_error.message.endswith(" missing receipt") + and check_error.message.endswith(" missing check")): + check_error.message = check_error.message.replace( + " missing check", " missing receipt or check", + ) + yield check_error + else: + yield receipt_error + yield check_error diff --git a/tests/test_meta_invoice.py b/tests/test_meta_invoice.py index 5586e75b4f78d521fb2d715ab5fdb2300664a75c..fcaa6ce8845844e2c4051f5dff0fb8f0ca5f0e0b 100644 --- a/tests/test_meta_invoice.py +++ b/tests/test_meta_invoice.py @@ -20,18 +20,6 @@ from . import testutil from conservancy_beancount.plugin import meta_invoice -VALID_VALUES = { - 'Invoices/304321.pdf', - 'rt:123/456', - 'rt://ticket/234', -} - -INVALID_VALUES = { - '', - ' ', - ' ', -} - REQUIRED_ACCOUNTS = { 'Accrued:AccountsPayable', 'Accrued:AccountsReceivable', @@ -55,7 +43,7 @@ def hook(): @pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( REQUIRED_ACCOUNTS, NON_REQUIRED_ACCOUNTS, - VALID_VALUES, + testutil.LINK_METADATA_STRINGS, )) def test_valid_values_on_postings(hook, acct1, acct2, value): txn = testutil.Transaction(postings=[ @@ -67,7 +55,7 @@ def test_valid_values_on_postings(hook, acct1, acct2, value): @pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( REQUIRED_ACCOUNTS, NON_REQUIRED_ACCOUNTS, - INVALID_VALUES, + testutil.NON_LINK_METADATA_STRINGS, )) def test_invalid_values_on_postings(hook, acct1, acct2, value): txn = testutil.Transaction(postings=[ @@ -98,7 +86,7 @@ def test_bad_type_values_on_postings(hook, acct1, acct2, value): @pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( REQUIRED_ACCOUNTS, NON_REQUIRED_ACCOUNTS, - VALID_VALUES, + testutil.LINK_METADATA_STRINGS, )) def test_valid_values_on_transaction(hook, acct1, acct2, value): txn = testutil.Transaction(**{TEST_KEY: value}, postings=[ @@ -110,7 +98,7 @@ def test_valid_values_on_transaction(hook, acct1, acct2, value): @pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( REQUIRED_ACCOUNTS, NON_REQUIRED_ACCOUNTS, - INVALID_VALUES, + testutil.NON_LINK_METADATA_STRINGS, )) def test_invalid_values_on_transaction(hook, acct1, acct2, value): txn = testutil.Transaction(**{TEST_KEY: value}, postings=[ diff --git a/tests/test_meta_receipt.py b/tests/test_meta_receipt.py new file mode 100644 index 0000000000000000000000000000000000000000..aaf63be7a5f53fa3a484bdb83b5d3985b5e2dc9b --- /dev/null +++ b/tests/test_meta_receipt.py @@ -0,0 +1,248 @@ +"""Test validation of receipt metadata""" +# 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 . + +import pytest + +from . import testutil + +from conservancy_beancount.plugin import meta_receipt + +REQUIRED_ACCOUNTS = { + 'Assets:Cash', + 'Assets:Savings', + 'Liabilities:CreditCard', +} + +CHECKING_ACCOUNTS = { + 'Assets:Checking', + 'Assets:CheckCard', +} + +TESTABLE_ACCOUNTS = REQUIRED_ACCOUNTS | CHECKING_ACCOUNTS + +NON_REQUIRED_ACCOUNTS = { + 'Accrued:AccountsPayable', + 'Assets:PrepaidExpenses', + 'Assets:PrepaidVacation', + 'Expenses:Other', + 'Income:Other', + 'UnearnedIncome:Donations', +} + +TEST_KEY = 'receipt' + +@pytest.fixture(scope='module') +def hook(): + config = testutil.TestConfig() + return meta_receipt.MetaReceipt(config) + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + TESTABLE_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_valid_values_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {TEST_KEY: value}), + ]) + assert not list(hook.run(txn)) + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + REQUIRED_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_LINK_METADATA_STRINGS, +)) +def test_invalid_values_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {TEST_KEY: value}), + ]) + actual = {error.message for error in hook.run(txn)} + assert actual == {"{} missing {}".format(acct1, TEST_KEY)} + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + TESTABLE_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_STRING_METADATA_VALUES, +)) +def test_bad_type_values_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {TEST_KEY: value}), + ]) + expected_msg = "{} has wrong type of {}: expected str but is a {}".format( + acct1, + TEST_KEY, + type(value).__name__, + ) + actual = {error.message for error in hook.run(txn)} + assert expected_msg in actual + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + TESTABLE_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_valid_values_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(**{TEST_KEY: value}, postings=[ + (acct2, 25), + (acct1, -25), + ]) + assert not list(hook.run(txn)) + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + REQUIRED_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_LINK_METADATA_STRINGS, +)) +def test_invalid_values_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(**{TEST_KEY: value}, postings=[ + (acct2, 25), + (acct1, -25), + ]) + actual = {error.message for error in hook.run(txn)} + assert actual == {"{} missing {}".format(acct1, TEST_KEY)} + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + TESTABLE_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_STRING_METADATA_VALUES, +)) +def test_bad_type_values_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(**{TEST_KEY: value}, postings=[ + (acct2, 25), + (acct1, -25), + ]) + expected_msg = "{} has wrong type of {}: expected str but is a {}".format( + acct1, + TEST_KEY, + type(value).__name__, + ) + actual = {error.message for error in hook.run(txn)} + assert expected_msg in actual + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_check_fallback_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {'check': value}), + ]) + assert not list(hook.run(txn)) + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_LINK_METADATA_STRINGS, +)) +def test_bad_check_fallback_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {'check': value}), + ]) + actual = {error.message for error in hook.run(txn)} + assert actual == {"{} missing {} or check".format(acct1, TEST_KEY)} + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_STRING_METADATA_VALUES, +)) +def test_check_fallback_bad_type_on_postings(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {'check': value}), + ]) + expected = { + "{} missing {}".format(acct1, TEST_KEY), + "{} has wrong type of check: expected str but is a {}".format( + acct1, type(value).__name__, + ), + } + actual = {error.message for error in hook.run(txn)} + assert actual == expected + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_check_fallback_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(check=value, postings=[ + (acct2, 25), + (acct1, -25), + ]) + assert not list(hook.run(txn)) + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_LINK_METADATA_STRINGS, +)) +def test_bad_check_fallback_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(check=value, postings=[ + (acct2, 25), + (acct1, -25), + ]) + actual = {error.message for error in hook.run(txn)} + assert actual == {"{} missing {} or check".format(acct1, TEST_KEY)} + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + CHECKING_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.NON_STRING_METADATA_VALUES, +)) +def test_check_fallback_bad_type_on_transaction(hook, acct1, acct2, value): + txn = testutil.Transaction(check=value, postings=[ + (acct2, 25), + (acct1, -25), + ]) + expected = { + "{} missing {}".format(acct1, TEST_KEY), + "{} has wrong type of check: expected str but is a {}".format( + acct1, type(value).__name__, + ), + } + actual = {error.message for error in hook.run(txn)} + assert actual == expected + +@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values( + REQUIRED_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, + testutil.LINK_METADATA_STRINGS, +)) +def test_check_fallback_not_accepted_on_other_accounts(hook, acct1, acct2, value): + txn = testutil.Transaction(postings=[ + (acct2, 25), + (acct1, -25, {'check': value}), + ]) + actual = {error.message for error in hook.run(txn)} + assert actual == {"{} missing {}".format(acct1, TEST_KEY)} + +@pytest.mark.parametrize('acct1,acct2', testutil.combine_values( + TESTABLE_ACCOUNTS, + NON_REQUIRED_ACCOUNTS, +)) +def test_no_value_required_for_credits(hook, acct1, acct2): + txn = testutil.Transaction(postings=[ + (acct2, -25), + (acct1, 25), + ]) + assert not list(hook.run(txn)) diff --git a/tests/testutil.py b/tests/testutil.py index 8dd7e1b8687d07cffc866d428d2a13e94ccb7830..9963f75ed7d5fe5fde361683b6cdce7ff42defe7 100644 --- a/tests/testutil.py +++ b/tests/testutil.py @@ -77,6 +77,18 @@ def Posting(account, number, meta, ) +LINK_METADATA_STRINGS = { + 'Invoices/304321.pdf', + 'rt:123/456', + 'rt://ticket/234', +} + +NON_LINK_METADATA_STRINGS = { + '', + ' ', + ' ', +} + NON_STRING_METADATA_VALUES = [ Decimal(5), FY_MID_DATE,