Changeset - d436a388f7fb
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-03-05 17:10:05
brettcsmith@brettcsmith.org
expenseAllocation: Only check Expenses postings.
3 files changed with 28 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -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:
conservancy_beancount/plugin/meta_expense_allocation.py
Show inline comments
...
 
@@ -25,5 +25,6 @@ class ExpenseAllocations(enum.Enum):
 

	
 

	
 
class MetaExpenseAllocation(core.PostingChecker):
 
    ACCOUNTS = ('Expenses:',)
 
    METADATA_KEY = 'expenseAllocation'
 
    VALUES_ENUM = ExpenseAllocations
tests/test_meta_expenseAllocation.py
Show inline comments
...
 
@@ -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
0 comments (0 inline, 0 general)