expenseAllocation: Only check Expenses postings.

This commit is contained in:
Brett Smith 2020-03-05 12:10:05 -05:00
parent 7862919022
commit d436a388f7
3 changed files with 28 additions and 0 deletions

View file

@ -17,6 +17,7 @@
from . import errors as errormod from . import errors as errormod
class PostingChecker: class PostingChecker:
ACCOUNTS = ('',)
VALUES_ENUM = {} VALUES_ENUM = {}
def _meta_get(self, txn, post, key, default=None): def _meta_get(self, txn, post, key, default=None):
@ -33,8 +34,18 @@ class PostingChecker:
def _default_value(self, txn, post): def _default_value(self, txn, post):
raise errormod.InvalidMetadataError(txn, post, self.METADATA_KEY) 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): def check(self, txn, post):
errors = [] errors = []
if not self._should_check(txn, post):
return errors
source_value = self._meta_get(txn, post, self.METADATA_KEY) source_value = self._meta_get(txn, post, self.METADATA_KEY)
set_value = source_value set_value = source_value
if source_value is None: if source_value is None:

View file

@ -25,5 +25,6 @@ class ExpenseAllocations(enum.Enum):
class MetaExpenseAllocation(core.PostingChecker): class MetaExpenseAllocation(core.PostingChecker):
ACCOUNTS = ('Expenses:',)
METADATA_KEY = 'expenseAllocation' METADATA_KEY = 'expenseAllocation'
VALUES_ENUM = ExpenseAllocations VALUES_ENUM = ExpenseAllocations

View file

@ -38,3 +38,19 @@ def test_validity_on_postings(value, value_ok):
assert not errors assert not errors
else: else:
assert errors 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