test_plugin_run: Simplify testing strategy.

Avoid keeping state in the hook classes/instances.
This commit is contained in:
Brett Smith 2020-03-05 19:31:07 -05:00
parent e9e2bb9b00
commit d145e22734

View file

@ -23,23 +23,26 @@ from conservancy_beancount import plugin
CONFIG_MAP = {}
class TransactionCounter:
HOOK_GROUPS = frozenset(['Transaction'])
def __init__(self):
self.counter = 0
HOOK_GROUPS = frozenset(['Transaction', 'counter'])
def run(self, txn):
self.counter += 1
return ()
return ['txn:{}'.format(id(txn))]
class PostingCounter(TransactionCounter):
HOOK_GROUPS = frozenset(['Posting'])
HOOK_GROUPS = frozenset(['Posting', 'counter'])
def run(self, txn, post):
return super().run(txn)
return ['post:{}'.format(id(post))]
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():
txn_counter = TransactionCounter()
post_counter = PostingCounter()
@ -55,9 +58,9 @@ def test_with_multiple_hooks():
]
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, [txn_counter, post_counter])
assert len(out_entries) == 2
assert len(errors) == 0
assert txn_counter.counter == 2
assert post_counter.counter == 4
errmap = map_errors(errors)
assert len(errmap.get('txn', '')) == 2
assert len(errmap.get('post', '')) == 4
def test_with_posting_hooks_only():
post_counter = PostingCounter()
@ -73,5 +76,6 @@ def test_with_posting_hooks_only():
]
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, [post_counter])
assert len(out_entries) == 2
assert len(errors) == 0
assert post_counter.counter == 4
errmap = map_errors(errors)
assert len(errmap.get('txn', '')) == 0
assert len(errmap.get('post', '')) == 4