Changeset - fdb62dd1c641
[Not reviewed]
0 7 0
Brett Smith - 4 years ago 2020-04-07 02:02:14
brettcsmith@brettcsmith.org
plugin.core: _RequireLinksPostingMetadataHook can check several metadata.

Extend the base class from checking 1 metadata value to checking N.

This is preparation for RT#10643, letting payables be documented with
invoice or contract.

This does unify error reporting, because now we always report all
type/invalid value errors *plus* a missing error if appropriate.
I think this consistency and thoroughness is appropriate, although
it did require some adjustments to the tests.
7 files changed with 73 insertions and 103 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -33,2 +33,3 @@ from typing import (
 
    Optional,
 
    Sequence,
 
    Type,
...
 
@@ -245,5 +246,5 @@ class _RequireLinksPostingMetadataHook(_PostingHook):
 
    # This base class confirms that a posting's metadata has one or more links
 
    # under METADATA_KEY.
 
    # Most subclasses only need to define METADATA_KEY and _run_on_post.
 
    METADATA_KEY: str
 
    # under one of the metadata keys listed in CHECKED_METADATA.
 
    # Most subclasses only need to define CHECKED_METADATA and _run_on_post.
 
    CHECKED_METADATA: Sequence[MetaKey]
 

	
...
 
@@ -251,18 +252,21 @@ class _RequireLinksPostingMetadataHook(_PostingHook):
 
        super().__init_subclass__()
 
        cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(['metadata', cls.METADATA_KEY])
 

	
 
    def _check_links(self, txn: Transaction, post: data.Posting, key: MetaKey) -> None:
 
        try:
 
            problem = not post.meta.get_links(key)
 
            value = None
 
        except TypeError:
 
            problem = True
 
            value = post.meta[key]
 
        if problem:
 
            raise errormod.InvalidMetadataError(txn, key, value, post)
 
        cls.HOOK_GROUPS = cls.HOOK_GROUPS.union(cls.CHECKED_METADATA).union('metadata')
 

	
 
    def _check_metadata(self,
 
                        txn: Transaction,
 
                        post: data.Posting,
 
                        keys: Sequence[MetaKey],
 
    ) -> Iterable[errormod.InvalidMetadataError]:
 
        have_docs = False
 
        for key in keys:
 
            try:
 
                links = post.meta.get_links(key)
 
            except TypeError as error:
 
                yield errormod.InvalidMetadataError(txn, key, post.meta[key], post)
 
            else:
 
                have_docs = have_docs or any(links)
 
        if not have_docs:
 
            yield errormod.InvalidMetadataError(txn, '/'.join(keys), None, post)
 

	
 
    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
        try:
 
            self._check_links(txn, post, self.METADATA_KEY)
 
        except errormod.Error as error:
 
            yield error
 
        return self._check_metadata(txn, post, self.CHECKED_METADATA)
conservancy_beancount/plugin/meta_approval.py
Show inline comments
...
 
@@ -27,4 +27,3 @@ from ..beancount_types import (
 
class MetaApproval(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'approval'
 
    CREDIT_CARD_ACCT = 'Liabilities:CreditCard'
 
    CHECKED_METADATA = ['approval']
 

	
conservancy_beancount/plugin/meta_invoice.py
Show inline comments
...
 
@@ -25,3 +25,3 @@ from ..beancount_types import (
 
class MetaInvoice(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'invoice'
 
    CHECKED_METADATA = ['invoice']
 

	
conservancy_beancount/plugin/meta_receipt.py
Show inline comments
...
 
@@ -31,6 +31,4 @@ from typing import (
 

	
 
_CheckMethod = Callable[[Transaction, data.Posting, MetaKey], None]
 

	
 
class MetaReceipt(core._RequireLinksPostingMetadataHook):
 
    METADATA_KEY = 'receipt'
 
    CHECKED_METADATA = ['receipt']
 

	
...
 
@@ -46,41 +44,33 @@ class MetaReceipt(core._RequireLinksPostingMetadataHook):
 

	
 
    def _check_check_id(self, txn: Transaction, post: data.Posting, key: MetaKey) -> None:
 
        value = post.meta.get(key)
 
        if (not isinstance(value, Decimal)
 
            or value < 1
 
            or value % 1):
 
            raise errormod.InvalidMetadataError(txn, key, value, post, Decimal)
 

	
 
    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
    def _run_checking_debit(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
        receipt_errors = list(self._check_metadata(txn, post, self.CHECKED_METADATA))
 
        if not receipt_errors:
 
            return
 
        for error in receipt_errors:
 
            if error.value is not None:
 
                yield error
 
        try:
 
            self._check_links(txn, post, self.METADATA_KEY)
 
        except errormod.InvalidMetadataError as error:
 
            receipt_error = error
 
            check_id = post.meta['check-id']
 
        except KeyError:
 
            check_id_ok = False
 
        else:
 
            return
 
            check_id_ok = (isinstance(check_id, Decimal)
 
                           and check_id >= 1
 
                           and not check_id % 1)
 
            if not check_id_ok:
 
                yield errormod.InvalidMetadataError(txn, 'check-id', check_id, post, Decimal)
 
        if not check_id_ok:
 
            yield errormod.InvalidMetadataError(txn, 'receipt/check-id', post=post)
 

	
 
        check_method: _CheckMethod = self._check_links
 
        if post.account.is_checking():
 
            if post.is_debit():
 
                check_method = self._check_check_id
 
                fallback_key = 'check-id'
 
            else:
 
                fallback_key = 'check'
 
    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
        keys = list(self.CHECKED_METADATA)
 
        is_checking = post.account.is_checking()
 
        if is_checking and post.is_debit():
 
            return self._run_checking_debit(txn, post)
 
        elif is_checking:
 
            keys.append('check')
 
        elif post.account.is_credit_card() and not post.is_credit():
 
            fallback_key = 'invoice'
 
            keys.append('invoice')
 
        elif post.account.is_under('Assets:PayPal') and not post.is_debit():
 
            fallback_key = 'paypal-id'
 
        else:
 
            yield receipt_error
 
            return
 

	
 
        try:
 
            check_method(txn, post, fallback_key)
 
        except errormod.InvalidMetadataError as fallback_error:
 
            if receipt_error.value is None and fallback_error.value is None:
 
                yield errormod.InvalidMetadataError(
 
                    txn, f"{self.METADATA_KEY} or {fallback_key}", None, post,
 
                )
 
            else:
 
                yield receipt_error
 
                yield fallback_error
 
            keys.append('paypal-id')
 
        return self._check_metadata(txn, post, keys)
conservancy_beancount/plugin/meta_receivable_documentation.py
Show inline comments
...
 
@@ -34,8 +34,3 @@ class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
 
    HOOK_GROUPS = frozenset(['network', 'rt'])
 
    SUPPORTING_METADATA = frozenset([
 
        'approval',
 
        'contract',
 
        'purchase-order',
 
    ])
 
    METADATA_KEY = '/'.join(sorted(SUPPORTING_METADATA))
 
    CHECKED_METADATA = ['approval', 'contract', 'purchase-order']
 
    # Conservancy invoice filenames have followed two patterns.
...
 
@@ -76,19 +71 @@ class MetaReceivableDocumentation(core._RequireLinksPostingMetadataHook):
 
        return self.ISSUED_INVOICE_RE.search(invoice_link) is not None
 

	
 
    def post_run(self, txn: Transaction, post: data.Posting) -> errormod.Iter:
 
        errors: Dict[MetaKey, Optional[errormod.InvalidMetadataError]] = {
 
            key: None for key in self.SUPPORTING_METADATA
 
        }
 
        have_support = False
 
        for key in errors:
 
            try:
 
                self._check_links(txn, post, key)
 
            except errormod.InvalidMetadataError as key_error:
 
                errors[key] = key_error
 
            else:
 
                have_support = True
 
        for key, error in errors.items():
 
            if error is not None and error.value is not None:
 
                yield error
 
        if not have_support:
 
            yield errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)
tests/test_meta_invoice.py
Show inline comments
...
 
@@ -39,2 +39,5 @@ TEST_KEY = 'invoice'
 

	
 
MISSING_MSG = f'{{}} missing {TEST_KEY}'.format
 
WRONG_TYPE_MSG = f'{{}} has wrong type of {TEST_KEY}: expected str but is a {{}}'.format
 

	
 
@pytest.fixture(scope='module')
...
 
@@ -79,9 +82,8 @@ def test_bad_type_values_on_postings(hook, acct1, acct2, value):
 
    ])
 
    expected_msg = "{} has wrong type of {}: expected str but is a {}".format(
 
        acct1,
 
        TEST_KEY,
 
        type(value).__name__,
 
    )
 
    expected = {
 
        MISSING_MSG(acct1),
 
        WRONG_TYPE_MSG(acct1, type(value).__name__),
 
    }
 
    actual = {error.message for error in hook.run(txn)}
 
    assert actual == {expected_msg}
 
    assert actual == expected
 

	
...
 
@@ -122,9 +124,8 @@ def test_bad_type_values_on_transaction(hook, acct1, acct2, value):
 
    ])
 
    expected_msg = "{} has wrong type of {}: expected str but is a {}".format(
 
        acct1,
 
        TEST_KEY,
 
        type(value).__name__,
 
    )
 
    expected = {
 
        MISSING_MSG(acct1),
 
        WRONG_TYPE_MSG(acct1, type(value).__name__),
 
    }
 
    actual = {error.message for error in hook.run(txn)}
 
    assert actual == {expected_msg}
 
    assert actual == expected
 

	
tests/test_meta_receipt.py
Show inline comments
...
 
@@ -46,3 +46,3 @@ class AccountForTesting(typing.NamedTuple):
 
        else:
 
            rest = f" or {self.fallback_meta}"
 
            rest = f"/{self.fallback_meta}"
 
        return f"{self.name} missing {TEST_KEY}{rest}"
...
 
@@ -208,3 +208,3 @@ def test_bad_type_fallback_on_post(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
...
 
@@ -239,3 +239,3 @@ def test_bad_type_fallback_on_txn(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
...
 
@@ -261,3 +261,3 @@ def test_invalid_check_id_on_post(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        f"{test_acct.name} has invalid {test_acct.fallback_meta}: {value}",
...
 
@@ -276,3 +276,3 @@ def test_bad_type_check_id_on_post(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
...
 
@@ -298,3 +298,3 @@ def test_invalid_check_id_on_txn(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        f"{test_acct.name} has invalid {test_acct.fallback_meta}: {value}",
...
 
@@ -313,3 +313,3 @@ def test_bad_type_check_id_on_txn(hook, test_acct, other_acct, value):
 
    expected = {
 
        test_acct.missing_message(False),
 
        test_acct.missing_message(),
 
        test_acct.wrong_type_message(value, test_acct.fallback_meta),
...
 
@@ -329,3 +329,2 @@ def test_fallback_not_accepted_on_other_accounts(hook, test_acct, other_acct, ke
 

	
 

	
 
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
0 comments (0 inline, 0 general)