Changeset - d145e2273483
[Not reviewed]
0 1 0
Brett Smith - 4 years ago 2020-03-06 00:31:07
brettcsmith@brettcsmith.org
test_plugin_run: Simplify testing strategy.

Avoid keeping state in the hook classes/instances.
1 file changed with 17 insertions and 13 deletions:
0 comments (0 inline, 0 general)
tests/test_plugin_run.py
Show inline comments
...
 
@@ -23,22 +23,25 @@ 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()
...
 
@@ -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
0 comments (0 inline, 0 general)