Files
@ 26762e11ef6a
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_plugin.py - annotation
26762e11ef6a
5.2 KiB
text/x-python
test_filters: Test non-string metadata values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 5566672cd6e0 5566672cd6e0 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e 0d370c445b9e 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 0d370c445b9e 0d370c445b9e d4d9bd655468 d4d9bd655468 d4d9bd655468 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 484c47a680c3 84d8adb7f664 484c47a680c3 e424173216c5 e424173216c5 d4d9bd655468 d4d9bd655468 d4d9bd655468 84d8adb7f664 d4d9bd655468 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee 0bf44ade7aee d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 d4d9bd655468 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 5566672cd6e0 | """Test main plugin"""
# 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/>.
import datetime
from decimal import Decimal
import pytest
from . import testutil
import beancount.core.data as bc_data
from conservancy_beancount import beancount_types, errors as errormod, plugin
HOOK_REGISTRY = plugin.HookRegistry()
class NonError(errormod.Error):
pass
class TransactionHook:
DIRECTIVE = beancount_types.Transaction
HOOK_GROUPS = frozenset()
def __init__(self, config):
self.config = config
def run(self, txn):
assert False, "something called base class run method"
@HOOK_REGISTRY.add_hook
class ConfigurationError(TransactionHook):
HOOK_GROUPS = frozenset(['unconfigured'])
def __init__(self, config):
raise errormod.ConfigurationError("testing error")
@HOOK_REGISTRY.add_hook
class TransactionError(TransactionHook):
HOOK_GROUPS = frozenset(['configured'])
def run(self, txn):
return [NonError('txn:{}'.format(id(txn)), txn)]
@HOOK_REGISTRY.add_hook
class PostingError(TransactionHook):
HOOK_GROUPS = frozenset(['configured', 'posting'])
def run(self, txn):
return [NonError('post:{}'.format(id(post)), txn)
for post in txn.postings]
@pytest.fixture
def config_map():
return {}
@pytest.fixture
def easy_entries():
return [
testutil.Transaction(postings=[
('Income:Donations', -25),
('Assets:Cash', 25),
]),
testutil.Transaction(postings=[
('Expenses:General', 10),
('Liabilites:CreditCard', -10),
]),
]
def map_errors(errors):
retval = {}
for error in errors:
key, _, errid = error.message.partition(':')
retval.setdefault(key, set()).add(errid)
return retval
@pytest.mark.parametrize('group_str,expected', [
(None, [TransactionError, PostingError]),
('', [TransactionError, PostingError]),
('all', [TransactionError, PostingError]),
('Transaction', [TransactionError, PostingError]),
('-posting', [TransactionError]),
('-configured posting', [PostingError]),
('configured -posting', [TransactionError]),
])
def test_registry_group_by_directive(group_str, expected):
args = () if group_str is None else (group_str,)
actual = {hook for _, hook in HOOK_REGISTRY.group_by_directive(*args)}
assert actual.issuperset(expected)
if len(expected) == 1:
assert not (TransactionError in actual and PostingError in actual)
def test_registry_unknown_group_name():
with pytest.raises(ValueError):
next(HOOK_REGISTRY.group_by_directive('UnKnownTestGroup'))
def test_registry_load_included_hooks():
registry = plugin.HookRegistry()
assert not list(registry.group_by_directive())
registry.load_included_hooks()
actual = {hook.__name__ for key, hook in registry.group_by_directive() if key == 'Transaction'}
assert len(actual) >= 5
assert 'MetaProject' in actual
assert 'MetaRTLinks' in actual
def test_run_with_multiple_hooks(easy_entries, config_map):
out_entries, errors = plugin.run(easy_entries, config_map, '', HOOK_REGISTRY)
assert len(out_entries) == 2
errmap = map_errors(errors)
assert len(errmap.get('txn', '')) == 2
assert len(errmap.get('post', '')) == 4
def test_run_with_one_hook(easy_entries, config_map):
out_entries, errors = plugin.run(easy_entries, config_map, 'posting', HOOK_REGISTRY)
assert len(out_entries) == 2
errmap = map_errors(errors)
assert len(errmap.get('txn', '')) == 0
assert len(errmap.get('post', '')) == 4
def test_run_on_all_directives(config_map):
meta = {
'filename': __file__,
'lineno': 125,
}
date = datetime.date(2020, 3, 1)
acct = 'Assets:Cash'
usd = 'USD'
entries = [
bc_data.Open(meta, date, acct, [usd], None),
bc_data.Close(meta, date.replace(year=date.year + 1), acct),
bc_data.Commodity(meta, date, usd),
bc_data.Pad(meta, date, acct, 'Income:Other'),
bc_data.Balance(meta, date, acct, 0, None, None),
bc_data.Transaction(meta, date, None, None, 'found cash', {}, {}, []),
bc_data.Note(meta, date, acct, 'test note'),
bc_data.Event(meta, date, 'test event', 'Test Event 1'),
bc_data.Query(meta, date, 'test query', ''),
bc_data.Price(meta, date, 'EUR', (Decimal('1.10508'), usd)),
bc_data.Document(meta, date, acct, '/TestDocument.txt', None, None),
bc_data.Custom(meta, date, 'test custom', ['test value']),
]
out_entries, errors = plugin.run(entries, config_map, '-all', HOOK_REGISTRY)
assert out_entries is entries
assert not errors
|