tests: Test HookRegistry using test instance, not real one.

This is better testing praxis and more prep for hooks-get-config.
This commit is contained in:
Brett Smith 2020-03-19 15:49:56 -04:00
parent 501bd251cb
commit d4d9bd6554
3 changed files with 120 additions and 136 deletions

120
tests/test_plugin.py Normal file
View file

@ -0,0 +1,120 @@
"""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 pytest
from . import testutil
from conservancy_beancount import beancount_types, plugin
HOOK_REGISTRY = plugin.HookRegistry()
class TransactionHook:
DIRECTIVE = beancount_types.Transaction
HOOK_GROUPS = frozenset()
def run(self, txn):
assert False, "something called base class run method"
@HOOK_REGISTRY.add_hook
class TransactionError(TransactionHook):
HOOK_GROUPS = frozenset(['configured'])
def run(self, txn):
return ['txn:{}'.format(id(txn))]
@HOOK_REGISTRY.add_hook
class PostingError(TransactionHook):
HOOK_GROUPS = frozenset(['configured', 'posting'])
def run(self, txn):
return ['post:{}'.format(id(post)) 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 hook_types(hooks, key):
return {type(hook) for hook in hooks[key]}
def map_errors(errors):
retval = {}
for errkey in errors:
key, _, errid = errkey.partition(':')
retval.setdefault(key, set()).add(errid)
return retval
def test_registry_all_by_default():
hook_groups = HOOK_REGISTRY.group_by_directive()
hooks = hook_types(hook_groups, 'Transaction')
assert len(hooks) >= 2
assert TransactionError in hooks
assert PostingError in hooks
def test_registry_one_exclude():
hook_groups = HOOK_REGISTRY.group_by_directive('-posting')
hooks = hook_types(hook_groups, 'Transaction')
assert len(hooks) >= 1
assert TransactionError in hooks
assert PostingError not in hooks
def test_registry_exclude_then_include():
hook_groups = HOOK_REGISTRY.group_by_directive('-configured posting')
hooks = hook_types(hook_groups, 'Transaction')
assert len(hooks) >= 1
assert TransactionError not in hooks
assert PostingError in hooks
def test_registry_include_then_exclude():
hook_groups = HOOK_REGISTRY.group_by_directive('configured -posting')
hooks = hook_types(hook_groups, 'Transaction')
assert len(hooks) >= 1
assert TransactionError in hooks
assert PostingError not in hooks
def test_registry_unknown_group_name():
with pytest.raises(ValueError):
HOOK_REGISTRY.group_by_directive('UnKnownTestGroup')
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

View file

@ -1,53 +0,0 @@
"""Test main plugin's HookRegistry"""
# 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 plugin
def hook_names(hooks, key):
return {type(hook).__name__ for hook in hooks[key]}
def test_default_registrations():
hooks = plugin.HOOK_REGISTRY.group_by_directive()
txn_hook_names = hook_names(hooks, 'Transaction')
assert len(txn_hook_names) >= 2
assert 'MetaExpenseAllocation' in txn_hook_names
assert 'MetaTaxImplication' in txn_hook_names
def test_exclude_single():
hooks = plugin.HOOK_REGISTRY.group_by_directive('-expense-allocation')
txn_hook_names = hook_names(hooks, 'Transaction')
assert txn_hook_names
assert 'MetaExpenseAllocation' not in txn_hook_names
def test_exclude_group_then_include_single():
hooks = plugin.HOOK_REGISTRY.group_by_directive('-metadata expense-allocation')
txn_hook_names = hook_names(hooks, 'Transaction')
assert 'MetaExpenseAllocation' in txn_hook_names
assert 'MetaTaxImplication' not in txn_hook_names
def test_include_group_then_exclude_single():
hooks = plugin.HOOK_REGISTRY.group_by_directive('metadata -tax-implication')
txn_hook_names = hook_names(hooks, 'Transaction')
assert 'MetaExpenseAllocation' in txn_hook_names
assert 'MetaTaxImplication' not in txn_hook_names
def test_unknown_group_name():
with pytest.raises(ValueError):
plugin.HOOK_REGISTRY.group_by_directive('UnKnownTestGroup')

View file

@ -1,83 +0,0 @@
"""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