Changeset - c9ff4ab74617
[Not reviewed]
2 3 2
Brett Smith - 4 years ago 2020-03-15 14:36:49
brettcsmith@brettcsmith.org
plugin: Settle on words-with-dashes metadata keys.
5 files changed with 29 insertions and 25 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/meta_expense_allocation.py
Show inline comments
 
"""meta_expense_allocation - Validate expenseAllocation metadata"""
 
"""meta_expense_allocation - Validate expense-allocation metadata"""
 
# 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.
...
 
@@ -15,13 +15,13 @@
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
from . import core
 

	
 
class MetaExpenseAllocation(core.PostingChecker):
 
    ACCOUNTS = ('Expenses:',)
 
    METADATA_KEY = 'expenseAllocation'
 
    METADATA_KEY = 'expense-allocation'
 
    VALUES_ENUM = core.MetadataEnum(METADATA_KEY, {
 
        'administration',
 
        'fundraising',
 
        'program',
 
    }, {
 
        'admin': 'administration',
conservancy_beancount/plugin/meta_tax_implication.py
Show inline comments
 
"""meta_tax_implication - Validate taxImplication metadata"""
 
"""meta_tax_implication - Validate tax-implication metadata"""
 
# 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.
...
 
@@ -19,13 +19,13 @@ import decimal
 
from . import core
 

	
 
DEFAULT_STOP_AMOUNT = decimal.Decimal(0)
 

	
 
class MetaTaxImplication(core.PostingChecker):
 
    ACCOUNTS = ('Assets:',)
 
    METADATA_KEY = 'taxImplication'
 
    METADATA_KEY = 'tax-implication'
 
    VALUES_ENUM = core.MetadataEnum(METADATA_KEY, [
 
        '1099',
 
        'Accountant-Advises-No-1099',
 
        'Bank-Transfer',
 
        'Foreign-Corporation',
 
        'Foreign-Individual-Contractor',
tests/test_meta_expense_allocation.py
Show inline comments
 
file renamed from tests/test_meta_expenseAllocation.py to tests/test_meta_expense_allocation.py
 
"""Test handling of expenseAllocation metadata"""
 
"""Test handling of expense-allocation metadata"""
 
# 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.
...
 
@@ -32,47 +32,49 @@ INVALID_VALUES = {
 
    'porgram',
 
    'adimn',
 
    'fundrasing',
 
    '',
 
}
 

	
 
TEST_KEY = 'expense-allocation'
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_postings(src_value, set_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25, {'expenseAllocation': src_value}),
 
        ('Expenses:General', 25, {TEST_KEY: src_value}),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    assert txn.postings[-1].meta.get('expenseAllocation') == set_value
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_postings(src_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25, {'expenseAllocation': src_value}),
 
        ('Expenses:General', 25, {TEST_KEY: src_value}),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert errors
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_transactions(src_value, set_value):
 
    txn = testutil.Transaction(expenseAllocation=src_value, postings=[
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    assert txn.postings[-1].meta.get('expenseAllocation') == set_value
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(src_value):
 
    txn = testutil.Transaction(expenseAllocation=src_value, postings=[
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Assets:Cash', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert errors
...
 
@@ -84,13 +86,13 @@ def test_invalid_values_on_transactions(src_value):
 
    'Liabilities:CreditCard',
 
    'UnearnedIncome:Donations',
 
])
 
def test_non_expense_accounts_skipped(account):
 
    txn = testutil.Transaction(postings=[
 
        (account, -25),
 
        ('Expenses:General', 25, {'expenseAllocation': 'program'}),
 
        ('Expenses:General', 25, {TEST_KEY: 'program'}),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[0], 0)
 
    assert not errors
 

	
 
@pytest.mark.parametrize('account,set_value', [
...
 
@@ -105,13 +107,13 @@ def test_default_values(account, set_value):
 
        ('Liabilites:CreditCard', -25),
 
        (account, 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    assert txn.postings[-1].meta['expenseAllocation'] == set_value
 
    assert txn.postings[-1].meta[TEST_KEY] == 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),
...
 
@@ -122,8 +124,8 @@ def test_default_value_set_in_date_range(date, set_value):
 
        ('Liabilites:CreditCard', -25),
 
        ('Expenses:General', 25),
 
    ])
 
    checker = meta_expense_allocation.MetaExpenseAllocation()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    got_value = (txn.postings[-1].meta or {}).get('expenseAllocation')
 
    got_value = (txn.postings[-1].meta or {}).get(TEST_KEY)
 
    assert bool(got_value) == bool(set_value)
tests/test_meta_tax_implication.py
Show inline comments
 
file renamed from tests/test_meta_taxImplication.py to tests/test_meta_tax_implication.py
 
"""Test handling of taxImplication metadata"""
 
"""Test handling of tax-implication metadata"""
 
# 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.
...
 
@@ -44,47 +44,49 @@ INVALID_VALUES = {
 
    '199',
 
    'W3',
 
    'Payrol',
 
    '',
 
}
 

	
 
TEST_KEY = 'tax-implication'
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_postings(src_value, set_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Assets:Cash', -25, {'taxImplication': src_value}),
 
        ('Assets:Cash', -25, {TEST_KEY: src_value}),
 
    ])
 
    checker = meta_tax_implication.MetaTaxImplication()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    assert txn.postings[-1].meta.get('taxImplication') == set_value
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_postings(src_value):
 
    txn = testutil.Transaction(postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Assets:Cash', -25, {'taxImplication': src_value}),
 
        ('Assets:Cash', -25, {TEST_KEY: src_value}),
 
    ])
 
    checker = meta_tax_implication.MetaTaxImplication()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert errors
 

	
 
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
 
def test_valid_values_on_transactions(src_value, set_value):
 
    txn = testutil.Transaction(taxImplication=src_value, postings=[
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Assets:Cash', -25),
 
    ])
 
    checker = meta_tax_implication.MetaTaxImplication()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert not errors
 
    assert txn.postings[-1].meta.get('taxImplication') == set_value
 
    assert txn.postings[-1].meta.get(TEST_KEY) == set_value
 

	
 
@pytest.mark.parametrize('src_value', INVALID_VALUES)
 
def test_invalid_values_on_transactions(src_value):
 
    txn = testutil.Transaction(taxImplication=src_value, postings=[
 
    txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
 
        ('Accrued:AccountsPayable', 25),
 
        ('Assets:Cash', -25),
 
    ])
 
    checker = meta_tax_implication.MetaTaxImplication()
 
    errors = checker.run(txn, txn.postings[-1], -1)
 
    assert errors
...
 
@@ -94,13 +96,13 @@ def test_invalid_values_on_transactions(src_value):
 
    'Expenses:General',
 
    'Liabilities:CreditCard',
 
])
 
def test_non_asset_accounts_skipped(account):
 
    txn = testutil.Transaction(postings=[
 
        (account, 25),
 
        ('Assets:Cash', -25, {'taxImplication': 'USA-Corporation'}),
 
        ('Assets:Cash', -25, {TEST_KEY: 'USA-Corporation'}),
 
    ])
 
    checker = meta_tax_implication.MetaTaxImplication()
 
    errors = checker.run(txn, txn.postings[0], 0)
 
    assert not errors
 

	
 
def test_asset_credits_skipped():
tests/test_plugin_HookRegistry.py
Show inline comments
...
 
@@ -28,25 +28,25 @@ def test_default_registrations():
 
    post_hook_names = hook_names(hooks, 'Posting')
 
    assert len(post_hook_names) >= 2
 
    assert 'MetaExpenseAllocation' in post_hook_names
 
    assert 'MetaTaxImplication' in post_hook_names
 

	
 
def test_exclude_single():
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('-expenseAllocation')
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('-expense-allocation')
 
    post_hook_names = hook_names(hooks, 'Posting')
 
    assert post_hook_names
 
    assert 'MetaExpenseAllocation' not in post_hook_names
 

	
 
def test_exclude_group_then_include_single():
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('-metadata expenseAllocation')
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('-metadata expense-allocation')
 
    post_hook_names = hook_names(hooks, 'Posting')
 
    assert 'MetaExpenseAllocation' in post_hook_names
 
    assert 'MetaTaxImplication' not in post_hook_names
 

	
 
def test_include_group_then_exclude_single():
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('metadata -taxImplication')
 
    hooks = plugin.HOOK_REGISTRY.group_by_directive('metadata -tax-implication')
 
    post_hook_names = hook_names(hooks, 'Posting')
 
    assert 'MetaExpenseAllocation' in post_hook_names
 
    assert 'MetaTaxImplication' not in post_hook_names
 

	
 
def test_unknown_group_name():
 
    with pytest.raises(ValueError):
0 comments (0 inline, 0 general)