Changeset - 16c47c64b23d
[Not reviewed]
0 3 0
Brett Smith - 5 years ago 2020-03-05 19:37:47
brettcsmith@brettcsmith.org
expenseAllocation: Date-limit the transactions we work on.

This prevents the plugin from giving meaning to postings that
might not really be there.
3 files changed with 43 insertions and 1 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -5,46 +5,67 @@
 
# 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 datetime
 

	
 
from . import errors as errormod
 

	
 
DEFAULT_START_DATE = datetime.date(2020, 3, 1)
 
DEFAULT_STOP_DATE = datetime.date(datetime.MAXYEAR, 1, 1)
 

	
 
class _GenericRange:
 
    def __init__(self, start, stop):
 
        self.start = start
 
        self.stop = stop
 

	
 
    def __repr__(self):
 
        return "{clsname}({self.start!r}, {self.stop!r})".format(
 
            clsname=type(self).__name__,
 
            self=self,
 
        )
 

	
 
    def __contains__(self, item):
 
        return self.start <= item < self.stop
 

	
 

	
 
class PostingChecker:
 
    ACCOUNTS = ('',)
 
    TXN_DATE_RANGE = _GenericRange(DEFAULT_START_DATE, DEFAULT_STOP_DATE)
 
    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
 
        ok = txn.date in self.TXN_DATE_RANGE
 
        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
tests/test_meta_expenseAllocation.py
Show inline comments
...
 
@@ -100,12 +100,30 @@ def test_non_expense_accounts_skipped(account):
 
    ('Expenses:Services:Development', 'program'),
 
    ('Expenses:Services:Fundraising', 'fundraising'),
 
])
 
def test_default_values(account, set_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Liabilites:CreditCard', -25),
 
        (account, 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.check(txn, txn.postings[-1])
 
    assert not errors
 
    assert txn.postings[-1].meta['expenseAllocation'] == set_value
 

	
 
@pytest.mark.parametrize('date,set_value', [
 
    (testutil.EXTREME_FUTURE_DATE, False),
 
    (testutil.FUTURE_DATE, True),
 
    (testutil.FY_START_DATE, True),
 
    (testutil.FY_MID_DATE, True),
 
    (testutil.PAST_DATE, False),
 
])
 
def test_default_value_set_in_date_range(date, set_value):
 
    txn = testutil.Transaction(date=date, postings=[
 
        ('Liabilites:CreditCard', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.check(txn, txn.postings[-1])
 
    assert not errors
 
    got_value = (txn.postings[-1].meta or {}).get('expenseAllocation')
 
    assert bool(got_value) == bool(set_value)
tests/testutil.py
Show inline comments
...
 
@@ -12,26 +12,29 @@
 
# 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 datetime
 

	
 
import beancount.core.amount as bc_amount
 
import beancount.core.data as bc_data
 

	
 
from decimal import Decimal
 

	
 
EXTREME_FUTURE_DATE = datetime.date(datetime.MAXYEAR, 12, 30)
 
FUTURE_DATE = datetime.date.today() + datetime.timedelta(days=365 * 99)
 
FY_START_DATE = datetime.date(2020, 3, 1)
 
FY_MID_DATE = datetime.date(2020, 9, 1)
 
PAST_DATE = datetime.date(2000, 1, 1)
 

	
 
def parse_date(s, fmt='%Y-%m-%d'):
 
    return datetime.datetime.strptime(s, fmt).date()
 

	
 
def Posting(account, number,
 
            currency='USD', cost=None, price=None, flag=None,
 
            **meta):
 
    return bc_data.Posting(
 
        account,
 
        bc_amount.Amount(Decimal(number), currency),
 
        cost,
 
        price,
0 comments (0 inline, 0 general)