Changeset - 536b50b478d8
[Not reviewed]
0 10 0
Brett Smith - 4 years ago 2020-05-11 13:52:05
brettcsmith@brettcsmith.org
plugin: Don't validate transactions flagged with !. RT#10591.
10 files changed with 77 insertions and 17 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -73,6 +73,2 @@ class Hook(Generic[Entry], metaclass=abc.ABCMeta):
 

	
 
class TransactionHook(Hook[Transaction]):
 
    DIRECTIVE = Transaction
 

	
 

	
 
### HELPER CLASSES
...
 
@@ -180,11 +176,16 @@ class MetadataEnum:
 

	
 
class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 
class TransactionHook(Hook[Transaction]):
 
    DIRECTIVE = Transaction
 
    TXN_DATE_RANGE: _GenericRange = _GenericRange(DEFAULT_START_DATE, DEFAULT_STOP_DATE)
 

	
 
    def __init_subclass__(cls) -> None:
 
        cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(['posting'])
 

	
 
    def _run_on_txn(self, txn: Transaction) -> bool:
 
        """Check whether we should run on a given transaction
 

	
 
        This method implements our usual checks for whether or not a hook
 
        should run on a given transaction. It's here for subclasses to use in
 
        their own implementations. See _PostingHook below for an example.
 
        """
 
        return (
 
            txn.date in self.TXN_DATE_RANGE
 
            txn.flag != '!'
 
            and txn.date in self.TXN_DATE_RANGE
 
            and not data.is_opening_balance_txn(txn)
...
 
@@ -192,2 +193,7 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 

	
 

	
 
class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 
    def __init_subclass__(cls) -> None:
 
        cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(['posting'])
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
conservancy_beancount/plugin/meta_repo_links.py
Show inline comments
...
 
@@ -65,5 +65,6 @@ class MetaRepoLinks(core.TransactionHook):
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        yield from self._check_links(txn.meta, txn)
 
        for post in txn.postings:
 
            if post.meta is not None:
 
                yield from self._check_links(post.meta, txn, post)
 
        if self._run_on_txn(txn):
 
            yield from self._check_links(txn.meta, txn)
 
            for post in txn.postings:
 
                if post.meta is not None:
 
                    yield from self._check_links(post.meta, txn, post)
conservancy_beancount/plugin/meta_rt_links.py
Show inline comments
...
 
@@ -61,5 +61,6 @@ class MetaRTLinks(core.TransactionHook):
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        yield from self._check_links(txn.meta, txn)
 
        for post in txn.postings:
 
            if post.meta is not None:
 
                yield from self._check_links(post.meta, txn, post)
 
        if self._run_on_txn(txn):
 
            yield from self._check_links(txn.meta, txn)
 
            for post in txn.postings:
 
                if post.meta is not None:
 
                    yield from self._check_links(post.meta, txn, post)
tests/test_meta_approval.py
Show inline comments
...
 
@@ -182 +182,8 @@ def test_approval_required_for_partial_transfer(hook):
 
    assert actual == {"Assets:Checking missing {}".format(TEST_KEY)}
 

	
 
def test_not_required_on_flagged(hook):
 
    txn = testutil.Transaction(flag='!', postings=[
 
        ('Assets:Checking', -25),
 
        ('Liabilities:Payable:Accounts', 25),
 
    ])
 
    assert not list(hook.run(txn))
tests/test_meta_invoice.py
Show inline comments
...
 
@@ -146 +146,12 @@ def test_not_required_on_opening(hook):
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('acct1,acct2', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    NON_REQUIRED_ACCOUNTS,
 
))
 
def test_not_required_on_flagged(acct1, acct2, hook):
 
    txn = testutil.Transaction(flag='!', postings=[
 
        (acct1, 25),
 
        (acct2, -25),
 
    ])
 
    assert not list(hook.run(txn))
tests/test_meta_payable_documentation.py
Show inline comments
...
 
@@ -173 +173,4 @@ def test_not_required_on_opening(hook):
 
    assert not list(hook.run(txn))
 

	
 
def test_not_required_on_flagged(hook):
 
    check(hook, None, txn_meta={'flag': '!'})
tests/test_meta_receipt.py
Show inline comments
...
 
@@ -351 +351,8 @@ def test_not_required_on_opening(hook, test_acct, equity_acct):
 
    check(hook, test_acct, equity_acct, None)
 

	
 
@pytest.mark.parametrize('test_acct,other_acct', testutil.combine_values(
 
    ACCOUNTS,
 
    NOT_REQUIRED_ACCOUNTS,
 
))
 
def test_not_required_on_flagged(hook, test_acct, other_acct):
 
    check(hook, test_acct, other_acct, None, txn_meta={'flag': '!'})
tests/test_meta_receivable_documentation.py
Show inline comments
...
 
@@ -215 +215,5 @@ def test_not_required_on_opening(hook):
 
    assert not list(hook.run(txn))
 

	
 
def test_not_required_on_flagged(hook):
 
    post_meta = seed_meta()
 
    check(hook, None, txn_meta={'flag': '!'}, post_meta=post_meta)
tests/test_meta_repo_links.py
Show inline comments
...
 
@@ -102,2 +102,12 @@ def test_bad_post_links(hook):
 

	
 
def test_flagged_txn_not_checked(hook):
 
    keys = iter(METADATA_KEYS)
 
    txn_meta = build_meta(keys, BAD_LINKS)
 
    txn_meta['flag'] = '!'
 
    txn = testutil.Transaction(**txn_meta, postings=[
 
        ('Income:Donations', -5, build_meta(keys, BAD_LINKS)),
 
        ('Assets:Checking', 5, build_meta(keys, BAD_LINKS)),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('value', testutil.NON_STRING_METADATA_VALUES)
tests/test_meta_rt_links.py
Show inline comments
...
 
@@ -148,2 +148,12 @@ def test_docs_outside_rt_not_checked(hook, ext_doc):
 

	
 
def test_flagged_txn_not_checked(hook):
 
    txn_meta = build_meta(None, MALFORMED_LINKS)
 
    txn_meta['flag'] = '!'
 
    keys = iter(METADATA_KEYS)
 
    txn = testutil.Transaction(**txn_meta, postings=[
 
        ('Income:Donations', -5, build_meta(keys, MALFORMED_LINKS)),
 
        ('Assets:Checking', 5, build_meta(keys, NOT_FOUND_LINKS)),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
def test_mixed_results(hook):
0 comments (0 inline, 0 general)