Changeset - d436a388f7fb
[Not reviewed]
0 3 0
Brett Smith - 5 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
 
"""Base classes for plugin checks"""
 
# 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 <https://www.gnu.org/licenses/>.
 

	
 
from . import errors as errormod
 

	
 
class PostingChecker:
 
    ACCOUNTS = ('',)
 
    VALUES_ENUM = {}
 

	
 
    def _meta_get(self, txn, post, key, default=None):
 
        try:
 
            return post.meta[key]
 
        except (KeyError, TypeError):
 
            return txn.meta.get(key, default)
 

	
 
    def _meta_set(self, post, key, value):
 
        if post.meta is None:
 
            post.meta = {}
 
        post.meta[key] = value
 

	
 
    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:
 
            try:
 
                set_value = self._default_value(txn, post)
 
            except errormod._BaseError as error:
 
                errors.append(error)
 
        else:
 
            try:
 
                set_value = self.VALUES_ENUM[source_value].value
 
            except KeyError:
 
                errors.append(errormod.InvalidMetadataError(
 
                    txn, post, self.METADATA_KEY, source_value,
 
                ))
 
        if not errors:
 
            self._meta_set(post, self.METADATA_KEY, set_value)
 
        return errors
conservancy_beancount/plugin/meta_expense_allocation.py
Show inline comments
...
 
@@ -4,26 +4,27 @@
 
# 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 <https://www.gnu.org/licenses/>.
 

	
 
import enum
 

	
 
from . import core
 

	
 
class ExpenseAllocations(enum.Enum):
 
    administration = 'administration'
 
    fundraising = 'fundraising'
 
    program = 'program'
 

	
 

	
 
class MetaExpenseAllocation(core.PostingChecker):
 
    ACCOUNTS = ('Expenses:',)
 
    METADATA_KEY = 'expenseAllocation'
 
    VALUES_ENUM = ExpenseAllocations
tests/test_meta_expenseAllocation.py
Show inline comments
...
 
@@ -17,24 +17,40 @@
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount.plugin import meta_expense_allocation
 

	
 
@pytest.mark.parametrize('value,value_ok', [
 
    ('program', True),
 
    ('administration', True),
 
    ('fundraising', True),
 
    ('invalid', False),
 
    ('', False),
 
])
 
def test_validity_on_postings(value, value_ok):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25, {'expenseAllocation': value}),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.check(txn, txn.postings[-1])
 
    if 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)