Files
@ 1caeb9a1a63d
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_plugin_run.py - annotation
1caeb9a1a63d
2.5 KiB
text/x-python
setup: Introduce typecheck alias.
53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 3fbc14d377ac 53329c7a23b8 53329c7a23b8 d34db7154257 53329c7a23b8 d34db7154257 53329c7a23b8 3fbc14d377ac a41feb94b3e0 53329c7a23b8 e9e2bb9b001d d145e2273483 53329c7a23b8 53329c7a23b8 d34db7154257 53329c7a23b8 3fbc14d377ac a41feb94b3e0 53329c7a23b8 a41feb94b3e0 a41feb94b3e0 d145e2273483 53329c7a23b8 d145e2273483 d145e2273483 d145e2273483 d145e2273483 d145e2273483 d145e2273483 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 d34db7154257 53329c7a23b8 d145e2273483 d145e2273483 d145e2273483 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 53329c7a23b8 a41feb94b3e0 53329c7a23b8 d145e2273483 d145e2273483 d145e2273483 | """Test main plugin run loop"""
# 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 pytest
from . import testutil
from conservancy_beancount import beancount_types, plugin
CONFIG_MAP = {}
HOOK_REGISTRY = plugin.HookRegistry()
@HOOK_REGISTRY.add_hook
class TransactionCounter:
DIRECTIVE = beancount_types.Transaction
HOOK_GROUPS = frozenset()
def run(self, txn):
return ['txn:{}'.format(id(txn))]
@HOOK_REGISTRY.add_hook
class PostingCounter(TransactionCounter):
DIRECTIVE = beancount_types.Transaction
HOOK_GROUPS = frozenset(['posting'])
def run(self, txn):
return ['post:{}'.format(id(post)) for post in txn.postings]
def map_errors(errors):
retval = {}
for errkey in errors:
key, _, errid = errkey.partition(':')
retval.setdefault(key, set()).add(errid)
return retval
def test_with_multiple_hooks():
in_entries = [
testutil.Transaction(postings=[
('Income:Donations', -25),
('Assets:Cash', 25),
]),
testutil.Transaction(postings=[
('Expenses:General', 10),
('Liabilites:CreditCard', -10),
]),
]
out_entries, errors = plugin.run(in_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_with_posting_hooks_only():
in_entries = [
testutil.Transaction(postings=[
('Income:Donations', -25),
('Assets:Cash', 25),
]),
testutil.Transaction(postings=[
('Expenses:General', 10),
('Liabilites:CreditCard', -10),
]),
]
out_entries, errors = plugin.run(in_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
|