diff --git a/conservancy_beancount/plugin/core.py b/conservancy_beancount/plugin/core.py index 7ff940eebe6043de1aa47c5d735b73a9ea2b617b..d3f42b577fdd9bc047984aabf1e1495f7a2e6403 100644 --- a/conservancy_beancount/plugin/core.py +++ b/conservancy_beancount/plugin/core.py @@ -17,6 +17,7 @@ from . import errors as errormod class PostingChecker: + ACCOUNTS = ('',) VALUES_ENUM = {} def _meta_get(self, txn, post, key, default=None): @@ -33,8 +34,18 @@ class PostingChecker: def _default_value(self, txn, post): raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY) + def _should_check(self, txn, post): + ok = True + if isinstance(self.ACCOUNTS, tuple): + ok = ok and post.account.startswith(self.ACCOUNTS) + else: + ok = ok and re.search(self.ACCOUNTS, post.account) + return ok + def check(self, txn, post): errors = [] + if not self._should_check(txn, post): + return errors source_value = self._meta_get(txn, post, self.METADATA_KEY) set_value = source_value if source_value is None: diff --git a/conservancy_beancount/plugin/meta_expense_allocation.py b/conservancy_beancount/plugin/meta_expense_allocation.py index 68e2d9e67378434261294668f520b3cdeace4d86..37e92fc996f2b4d44a8468cc2d8fcdd22a0b0c43 100644 --- a/conservancy_beancount/plugin/meta_expense_allocation.py +++ b/conservancy_beancount/plugin/meta_expense_allocation.py @@ -25,5 +25,6 @@ class ExpenseAllocations(enum.Enum): class MetaExpenseAllocation(core.PostingChecker): + ACCOUNTS = ('Expenses:',) METADATA_KEY = 'expenseAllocation' VALUES_ENUM = ExpenseAllocations diff --git a/tests/test_meta_expenseAllocation.py b/tests/test_meta_expenseAllocation.py index b5edd5d6fd715088bb34adf16e690885d3e32957..d1985d5c07c9c32eba36166ca0a1c55090001b3c 100644 --- a/tests/test_meta_expenseAllocation.py +++ b/tests/test_meta_expenseAllocation.py @@ -38,3 +38,19 @@ def test_validity_on_postings(value, value_ok): assert not errors else: assert errors + +@pytest.mark.parametrize('account', [ + 'Accrued:AccountsReceivable', + 'Assets:Cash', + 'Income:Donations', + 'Liabilities:CreditCard', + 'UnearnedIncome:Donations', +]) +def test_non_expense_accounts_skipped(account): + txn = testutil.Transaction(postings=[ + (account, -25), + ('Expenses:General', 25, {'expenseAllocation': 'program'}), + ]) + checker = meta_expense_allocation.MetaExpenseAllocation() + errors = checker.check(txn, txn.postings[0]) + assert not errors