From 9f0c30738db8db3d28c95881f2ba58111aef80d5 Mon Sep 17 00:00:00 2001 From: Brett Smith Date: Thu, 9 Apr 2020 15:12:04 -0400 Subject: [PATCH] plugin: Most validations skip opening balance transactions. RT#10642. --- conservancy_beancount/plugin/core.py | 5 ++++- conservancy_beancount/plugin/meta_entity.py | 2 ++ conservancy_beancount/plugin/meta_project.py | 21 +++++++++++++++++++- tests/test_meta_entity.py | 4 ++++ tests/test_meta_invoice.py | 6 +++++- tests/test_meta_payable_documentation.py | 8 ++++++++ tests/test_meta_paypal_id.py | 7 +++++++ tests/test_meta_project.py | 12 +++++++++++ tests/test_meta_receipt.py | 9 ++++++++- tests/test_meta_receivable_documentation.py | 8 ++++++++ 10 files changed, 78 insertions(+), 4 deletions(-) diff --git a/conservancy_beancount/plugin/core.py b/conservancy_beancount/plugin/core.py index 550b5a2..a5667e7 100644 --- a/conservancy_beancount/plugin/core.py +++ b/conservancy_beancount/plugin/core.py @@ -185,7 +185,10 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta): cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(['posting']) def _run_on_txn(self, txn: Transaction) -> bool: - return txn.date in self.TXN_DATE_RANGE + return ( + txn.date in self.TXN_DATE_RANGE + and not data.is_opening_balance_txn(txn) + ) def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: return True diff --git a/conservancy_beancount/plugin/meta_entity.py b/conservancy_beancount/plugin/meta_entity.py index 7f4ecf1..d39997d 100644 --- a/conservancy_beancount/plugin/meta_entity.py +++ b/conservancy_beancount/plugin/meta_entity.py @@ -49,6 +49,8 @@ class MetaEntity(core.TransactionHook): del alnum def run(self, txn: Transaction) -> errormod.Iter: + if data.is_opening_balance_txn(txn): + return txn_entity = txn.meta.get(self.METADATA_KEY, txn.payee) if txn_entity is None: txn_entity_ok = None diff --git a/conservancy_beancount/plugin/meta_project.py b/conservancy_beancount/plugin/meta_project.py index b47d9fe..0014706 100644 --- a/conservancy_beancount/plugin/meta_project.py +++ b/conservancy_beancount/plugin/meta_project.py @@ -40,6 +40,7 @@ class MetaProject(core._NormalizePostingMetadataHook): DEFAULT_PROJECT = 'Conservancy' PROJECT_DATA_PATH = Path('Projects', 'project-data.yml') VALUES_ENUM = core.MetadataEnum('project', {DEFAULT_PROJECT}) + RESTRICTED_FUNDS_ACCT = 'Equity:Funds:Restricted' def __init__(self, config: configmod.Config, source_path: Path=PROJECT_DATA_PATH) -> None: repo_path = config.repository_path() @@ -78,7 +79,10 @@ class MetaProject(core._NormalizePostingMetadataHook): source=source, ) - def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool: + def _run_on_opening_post(self, txn: Transaction, post: data.Posting) -> bool: + return post.account.is_under(self.RESTRICTED_FUNDS_ACCT) is not None + + def _run_on_other_post(self, txn: Transaction, post: data.Posting) -> bool: if post.account.is_under('Liabilities'): return not post.account.is_credit_card() else: @@ -86,6 +90,7 @@ class MetaProject(core._NormalizePostingMetadataHook): 'Assets:Receivable', 'Expenses', 'Income', + self.RESTRICTED_FUNDS_ACCT, ) is not None def _default_value(self, txn: Transaction, post: data.Posting) -> MetaValueEnum: @@ -96,3 +101,17 @@ class MetaProject(core._NormalizePostingMetadataHook): return self.DEFAULT_PROJECT else: raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post) + + def _run_on_txn(self, txn: Transaction) -> bool: + return txn.date in self.TXN_DATE_RANGE + + def run(self, txn: Transaction) -> errormod.Iter: + # mypy says we can't assign over a method. + # I understand why it wants to enforce thas as a blanket rule, but + # we're substituting in another type-compatible method, so it's pretty + # safe. + if data.is_opening_balance_txn(txn): + self._run_on_post = self._run_on_opening_post # type:ignore[assignment] + else: + self._run_on_post = self._run_on_other_post # type:ignore[assignment] + return super().run(txn) diff --git a/tests/test_meta_entity.py b/tests/test_meta_entity.py index f9caac6..58646e4 100644 --- a/tests/test_meta_entity.py +++ b/tests/test_meta_entity.py @@ -165,3 +165,7 @@ def test_which_accounts_required_on(hook, account, required): assert errors assert any(error.message == "{} missing entity".format(account) for error in errors) + +def test_not_required_on_opening(hook): + txn = testutil.Transaction.opening_balance() + assert not list(hook.run(txn)) diff --git a/tests/test_meta_invoice.py b/tests/test_meta_invoice.py index 7de49c5..4cd70e1 100644 --- a/tests/test_meta_invoice.py +++ b/tests/test_meta_invoice.py @@ -29,7 +29,7 @@ REQUIRED_ACCOUNTS = { NON_REQUIRED_ACCOUNTS = { 'Assets:Cash', - 'Equity:OpeningBalance', + 'Equity:Retained', 'Expenses:Other', 'Income:Other', 'Liabilities:CreditCard', @@ -140,3 +140,7 @@ def test_missing_invoice(hook, acct1, acct2): ]) actual = {error.message for error in hook.run(txn)} assert actual == {"{} missing {}".format(acct1, TEST_KEY)} + +def test_not_required_on_opening(hook): + txn = testutil.Transaction.opening_balance() + assert not list(hook.run(txn)) diff --git a/tests/test_meta_payable_documentation.py b/tests/test_meta_payable_documentation.py index 4b6a4fa..7cabd8e 100644 --- a/tests/test_meta_payable_documentation.py +++ b/tests/test_meta_payable_documentation.py @@ -163,3 +163,11 @@ def test_paid_accts_not_checked(hook): def test_does_not_apply_to_other_accounts(hook, account): meta = seed_meta() check(hook, None, account, post_meta=meta) + +def test_not_required_on_opening(hook): + txn = testutil.Transaction(postings=[ + ('Liabilities:Payable:Accounts', -15), + ('Liabilities:Payable:Vacation', -25), + (next(testutil.OPENING_EQUITY_ACCOUNTS), 40), + ]) + assert not list(hook.run(txn)) diff --git a/tests/test_meta_paypal_id.py b/tests/test_meta_paypal_id.py index c70d99a..49d736e 100644 --- a/tests/test_meta_paypal_id.py +++ b/tests/test_meta_paypal_id.py @@ -187,3 +187,10 @@ def test_invoice_payment_transaction_ok(hook, txn_id, inv_id): ('Expenses:BankingFees', 3), ]) assert not list(hook.run(txn)) + +def test_not_required_on_opening(hook): + txn = testutil.Transaction(postings=[ + ('Assets:PayPal', 1000), + (next(testutil.OPENING_EQUITY_ACCOUNTS), -1000), + ]) + assert not list(hook.run(txn)) diff --git a/tests/test_meta_project.py b/tests/test_meta_project.py index 5c372dc..5149e35 100644 --- a/tests/test_meta_project.py +++ b/tests/test_meta_project.py @@ -90,6 +90,8 @@ def test_invalid_values_on_transactions(hook, src_value): ('Assets:Receivable:Accounts', True), ('Assets:Receivable:Loans', True), ('Equity:OpeningBalance', False), + ('Equity:Funds:Restricted', True), + ('Equity:Funds:Unrestricted', False), ('Expenses:General', True), ('Income:Donations', True), ('Liabilities:CreditCard', False), @@ -154,3 +156,13 @@ def test_invalid_project_data(repo_path_s, data_path_s): config = testutil.TestConfig(repo_path=repo_path_s) with pytest.raises(errormod.ConfigurationError): meta_project.MetaProject(config, Path(data_path_s)) + +def test_not_required_on_opening(hook): + txn = testutil.Transaction.opening_balance('Equity:Funds:Unrestricted') + assert not list(hook.run(txn)) + +def test_always_required_on_restricted_funds(hook): + acct = 'Equity:Funds:Restricted' + txn = testutil.Transaction.opening_balance(acct) + actual = {error.message for error in hook.run(txn)} + assert actual == {f'{acct} missing project'} diff --git a/tests/test_meta_receipt.py b/tests/test_meta_receipt.py index b251dda..af7a370 100644 --- a/tests/test_meta_receipt.py +++ b/tests/test_meta_receipt.py @@ -86,7 +86,7 @@ NOT_REQUIRED_ACCOUNTS = itertools.cycle([ 'Assets:PayPal', 'Assets:Prepaid:Expenses', 'Assets:Receivable:Accounts', - 'Equity:OpeningBalance', + 'Equity:Retained', 'Expenses:Other', 'Income:Other', 'Liabilities:Payable:Accounts', @@ -342,3 +342,10 @@ def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value): (test_acct.name, 0, {test_acct.fallback_meta: value}), ]) assert not list(hook.run(txn)) + +@pytest.mark.parametrize('test_acct,equity_acct', testutil.combine_values( + ACCOUNTS, + testutil.OPENING_EQUITY_ACCOUNTS, +)) +def test_not_required_on_opening(hook, test_acct, equity_acct): + check(hook, test_acct, equity_acct, None) diff --git a/tests/test_meta_receivable_documentation.py b/tests/test_meta_receivable_documentation.py index 0843e2c..ad41abb 100644 --- a/tests/test_meta_receivable_documentation.py +++ b/tests/test_meta_receivable_documentation.py @@ -205,3 +205,11 @@ def test_configuration_error_without_rt(): config = testutil.TestConfig() with pytest.raises(errormod.ConfigurationError): meta_receivable_documentation.MetaReceivableDocumentation(config) + +def test_not_required_on_opening(hook): + txn = testutil.Transaction(postings=[ + ('Assets:Receivable:Accounts', 100), + ('Assets:Receivable:Loans', 200), + (next(testutil.OPENING_EQUITY_ACCOUNTS), -300), + ]) + assert not list(hook.run(txn))