test_plugin_run: Simplify testing strategy.
Avoid keeping state in the hook classes/instances.
This commit is contained in:
parent
e9e2bb9b00
commit
d145e22734
1 changed files with 17 additions and 13 deletions
|
@ -23,23 +23,26 @@ from conservancy_beancount import plugin
|
||||||
CONFIG_MAP = {}
|
CONFIG_MAP = {}
|
||||||
|
|
||||||
class TransactionCounter:
|
class TransactionCounter:
|
||||||
HOOK_GROUPS = frozenset(['Transaction'])
|
HOOK_GROUPS = frozenset(['Transaction', 'counter'])
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.counter = 0
|
|
||||||
|
|
||||||
def run(self, txn):
|
def run(self, txn):
|
||||||
self.counter += 1
|
return ['txn:{}'.format(id(txn))]
|
||||||
return ()
|
|
||||||
|
|
||||||
|
|
||||||
class PostingCounter(TransactionCounter):
|
class PostingCounter(TransactionCounter):
|
||||||
HOOK_GROUPS = frozenset(['Posting'])
|
HOOK_GROUPS = frozenset(['Posting', 'counter'])
|
||||||
|
|
||||||
def run(self, txn, post):
|
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():
|
def test_with_multiple_hooks():
|
||||||
txn_counter = TransactionCounter()
|
txn_counter = TransactionCounter()
|
||||||
post_counter = PostingCounter()
|
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])
|
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, [txn_counter, post_counter])
|
||||||
assert len(out_entries) == 2
|
assert len(out_entries) == 2
|
||||||
assert len(errors) == 0
|
errmap = map_errors(errors)
|
||||||
assert txn_counter.counter == 2
|
assert len(errmap.get('txn', '')) == 2
|
||||||
assert post_counter.counter == 4
|
assert len(errmap.get('post', '')) == 4
|
||||||
|
|
||||||
def test_with_posting_hooks_only():
|
def test_with_posting_hooks_only():
|
||||||
post_counter = PostingCounter()
|
post_counter = PostingCounter()
|
||||||
|
@ -73,5 +76,6 @@ def test_with_posting_hooks_only():
|
||||||
]
|
]
|
||||||
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, [post_counter])
|
out_entries, errors = plugin.run(in_entries, CONFIG_MAP, [post_counter])
|
||||||
assert len(out_entries) == 2
|
assert len(out_entries) == 2
|
||||||
assert len(errors) == 0
|
errmap = map_errors(errors)
|
||||||
assert post_counter.counter == 4
|
assert len(errmap.get('txn', '')) == 0
|
||||||
|
assert len(errmap.get('post', '')) == 4
|
||||||
|
|
Loading…
Add table
Reference in a new issue