Changeset - fe3560b748fa
[Not reviewed]
0 3 2
Brett Smith - 4 years ago 2021-02-11 18:38:11
brettcsmith@brettcsmith.org
meta_tax_reporting: New plugin validation.
5 files changed with 247 insertions and 7 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/data.py
Show inline comments
 
"""Enhanced Beancount data structures for Conservancy
 

	
 
The classes in this module are interface-compatible with Beancount's core data
 
structures, and provide additional business logic that we want to use
 
throughout Conservancy tools.
 
"""
 
# Copyright © 2020  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import collections
 
import datetime
 
import decimal
 
import functools
 
import re
 

	
 
from beancount.core import account as bc_account
 
from beancount.core import amount as bc_amount
 
from beancount.core import convert as bc_convert
 
from beancount.core import data as bc_data
 
from beancount.core import position as bc_position
 
from beancount.parser import options as bc_options
 

	
 
from typing import (
 
    cast,
 
    overload,
 
    Callable,
 
    Hashable,
 
    Iterable,
 
    Iterator,
 
    MutableMapping,
 
    Optional,
 
    Pattern,
 
    Sequence,
 
    TypeVar,
 
    Union,
 
)
 

	
 
from .beancount_types import (
 
    Close,
 
    Currency,
 
    Directive,
 
    Meta,
 
    MetaKey,
 
    MetaValue,
 
    Open,
 
    OptionsMap,
 
    Posting as BasePosting,
 
    Transaction,
 
)
 

	
 
DecimalCompat = Union[decimal.Decimal, int]
 

	
 
EQUITY_ACCOUNTS = frozenset([
 
    'Equity',
 
    'Expenses',
 
    'Income',
 
])
 
FUND_ACCOUNTS = EQUITY_ACCOUNTS | frozenset([
 
    'Assets:Prepaid',
 
    'Assets:Receivable',
 
    'Liabilities:Payable',
 
    'Liabilities:UnearnedIncome',
 
])
 
LINK_METADATA = frozenset([
 
    'approval',
 
    'bank-statement',
 
    'check',
 
    'contract',
 
    'invoice',
 
    'purchase-order',
 
    'receipt',
 
    'rt-id',
 
    'statement',
 
    'tax-reporting',
 
    'tax-statement',
 
])
 

	
 
class AccountMeta(MutableMapping[MetaKey, MetaValue]):
 
    """Access account metadata
 

	
 
    This class provides a consistent interface to all the metadata provided by
 
    Beancount's ``open`` and ``close`` directives: open and close dates,
 
    used currencies, booking method, and the metadata associated with each.
 

	
 
    For convenience, you can use this class as a Mapping to access the ``open``
 
    directive's metadata directly.
 
    """
 
    __slots__ = ('_opening', '_closing')
 

	
 
    def __init__(self, opening: Open, closing: Optional[Close]=None) -> None:
 
        self._opening = opening
 
        self._closing: Optional[Close] = None
 
        if closing is not None:
 
            self.add_closing(closing)
 

	
 
    def add_closing(self, closing: Close) -> None:
 
        if self._closing is not None and self._closing is not closing:
 
            raise ValueError(f"{self.account} already closed by {self._closing!r}")
 
        elif closing.account != self.account:
 
            raise ValueError(f"cannot close {self.account} with {closing.account}")
 
        elif closing.date < self.open_date:
 
            raise ValueError(f"close date {closing.date} predates open date {self.open_date}")
 
        else:
 
            self._closing = closing
 

	
 
    def __iter__(self) -> Iterator[MetaKey]:
 
        return iter(self._opening.meta)
 

	
 
    def __len__(self) -> int:
 
        return len(self._opening.meta)
 

	
 
    def __getitem__(self, key: MetaKey) -> MetaValue:
 
        return self._opening.meta[key]
 

	
 
    def __setitem__(self, key: MetaKey, value: MetaValue) -> None:
 
        self._opening.meta[key] = value
 

	
 
    def __delitem__(self, key: MetaKey) -> None:
 
        del self._opening.meta[key]
 

	
 
    @property
 
    def account(self) -> 'Account':
 
        return Account(self._opening.account)
 

	
 
    @property
 
    def booking(self) -> Optional[bc_data.Booking]:
 
        return self._opening.booking
 

	
 
    @property
 
    def close_date(self) -> Optional[datetime.date]:
 
        return None if self._closing is None else self._closing.date
 

	
 
    @property
 
    def close_meta(self) -> Optional[Meta]:
 
        return None if self._closing is None else self._closing.meta
 

	
 
    @property
 
    def currencies(self) -> Optional[Sequence[Currency]]:
 
        return self._opening.currencies
 

	
 
    @property
 
    def open_date(self) -> datetime.date:
 
        return self._opening.date
 

	
 
    @property
 
    def open_meta(self) -> Meta:
 
        return self._opening.meta
 

	
 

	
 
class Account(str):
 
    """Account name string
 

	
 
    This is a string that names an account, like Assets:Bank:Checking
 
    or Income:Donations. This class provides additional methods for common
 
    account name parsing and queries.
 
    """
 
    __slots__ = ()
 

	
 
    ACCOUNT_RE: Pattern
 
    SEP = bc_account.sep
 
    _meta_map: MutableMapping[str, AccountMeta] = {}
 
    _options_map: OptionsMap
 

	
 
    @classmethod
 
    def load_options_map(cls, options_map: OptionsMap) -> None:
 
        cls._options_map = options_map
 
        roots: Sequence[str] = bc_options.get_account_types(options_map)
 
        cls.ACCOUNT_RE = re.compile(
 
            r'^(?:{})(?:{}[A-Z0-9][-A-Za-z0-9]*)+$'.format(
 
                '|'.join(roots), cls.SEP,
 
            ))
 

	
 
    @classmethod
 
    def load_opening(cls, opening: Open) -> None:
 
        cls._meta_map[opening.account] = AccountMeta(opening)
 

	
 
    @classmethod
 
    def load_closing(cls, closing: Close) -> None:
 
        try:
 
            cls._meta_map[closing.account].add_closing(closing)
 
        except KeyError:
 
            raise ValueError(
 
                f"tried to load {closing.account} close directive before open",
 
            ) from None
 

	
 
    @classmethod
 
    def load_openings_and_closings(cls, entries: Iterable[Directive]) -> None:
 
        for entry in entries:
 
            # type ignores because Beancount's directives aren't type-checkable.
 
            if isinstance(entry, bc_data.Open):
 
                cls.load_opening(entry)  # type:ignore[arg-type]
 
            elif isinstance(entry, bc_data.Close):
 
                cls.load_closing(entry)  # type:ignore[arg-type]
 

	
 
    @classmethod
 
    def load_from_books(cls, entries: Iterable[Directive], options_map: OptionsMap) -> None:
 
        cls.load_options_map(options_map)
 
        cls.load_openings_and_closings(entries)
 

	
 
    @classmethod
 
    def is_account(cls, s: str) -> bool:
 
        return cls.ACCOUNT_RE.fullmatch(s) is not None
 

	
 
    @classmethod
 
    def iter_accounts_by_classification(cls, s: str) -> Iterator['Account']:
 
        class_re = re.compile(f'^{re.escape(s)}(?::|$)')
 
        for name, meta in cls._meta_map.items():
 
            try:
 
                match = class_re.match(meta['classification'])
 
            except KeyError:
 
                match = None
 
            if match:
 
                yield cls(name)
 

	
 
    @classmethod
 
    def iter_accounts_by_hierarchy(cls, s: str) -> Iterator['Account']:
 
        for name in cls._meta_map:
 
            account = cls(name)
 
            if account.is_under(s):
 
                yield account
 

	
 
    @classmethod
 
    def iter_accounts(cls, s: Optional[str]=None) -> Iterator['Account']:
 
        """Iterate account objects by name or classification
 

	
 
        With no argument, returns an iterator of all known account names.
 
        If you pass in a root account name, or a valid account string, returns
 
        an iterator of all accounts under that account in the hierarchy.
 
        Otherwise, returns an iterator of all accounts with the given
 
        ``classification`` metadata.
 
        """
 
        if s is None:
 
            return (cls(acct) for acct in cls._meta_map)
 
        # We append a stub subaccount to match root accounts.
 
        elif cls.is_account(f'{s}:RootsOK'):
 
            return cls.iter_accounts_by_hierarchy(s)
 
        else:
 
            return cls.iter_accounts_by_classification(s)
 

	
 
    @property
 
    def meta(self) -> AccountMeta:
 
        return self._meta_map[self]
 

	
 
    def is_cash_equivalent(self) -> bool:
 
        return (
 
            self.is_under('Assets:') is not None
 
            and self.is_under('Assets:Prepaid', 'Assets:Receivable') is None
 
        )
 

	
 
    def is_checking(self) -> bool:
 
        return self.is_cash_equivalent() and ':Check' in self
 

	
 
    def is_credit_card(self) -> bool:
 
        return self.is_under('Liabilities:CreditCard') is not None
 

	
 
    def is_open_on_date(self, date: datetime.date) -> Optional[bool]:
 
        """Return true if this account is open on the given date.
 

	
 
        This method considers the dates on the account's open and close
 
        directives. If there is no close directive, it just checks the date is
 
        on or after the opening date. If neither exists, returns None.
 
        """
 
        try:
 
            meta = self.meta
 
        except KeyError:
 
            return None
conservancy_beancount/plugin/__init__.py
Show inline comments
 
"""Beancount plugin entry point for Conservancy"""
 
# Copyright © 2020  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import importlib
 
import logging
 

	
 
import beancount.core.data as bc_data
 

	
 
from typing import (
 
    AbstractSet,
 
    Any,
 
    Dict,
 
    Iterator,
 
    List,
 
    Optional,
 
    Set,
 
    Tuple,
 
    Type,
 
)
 
from ..beancount_types import (
 
    ALL_DIRECTIVES,
 
    Directive,
 
    Entries,
 
    Errors,
 
    OptionsMap,
 
)
 
from .. import config as configmod
 
from .core import (
 
    Hook,
 
    HookName,
 
)
 
from ..errors import (
 
    ConfigurationError,
 
    Error,
 
)
 

	
 
__plugins__ = ['run']
 

	
 
logger = logging.getLogger('conservancy_beancount.plugin')
 

	
 
class HookRegistry:
 
    INCLUDED_HOOKS: Dict[str, Optional[List[str]]] = {
 
        '.meta_approval': None,
 
        '.meta_entity': None,
 
        '.meta_expense_type': None,
 
        '.meta_income_type': None,
 
        '.meta_invoice': None,
 
        # Enforcing this hook would be premature as of May 2020.  --brett
 
        # '.meta_payable_documentation': None,
 
        '.meta_paypal_id': ['MetaPayPalID'],
 
        '.meta_project': None,
 
        '.meta_receipt': None,
 
        '.meta_receivable_documentation': None,
 
        '.meta_repo_links': None,
 
        '.meta_rt_links': ['MetaRTLinks'],
 
        '.meta_tax_implication': None,
 
        '.meta_payroll_type': [
 
            'HealthInsuranceHook',
 
            'OtherBenefitsHook',
 
            'SalaryHook',
 
            'TaxHook',
 
        ],
 
        '.meta_project': None,
 
        '.meta_receipt': None,
 
        '.meta_receivable_documentation': None,
 
        '.meta_repo_links': None,
 
        '.meta_rt_links': ['MetaRTLinks'],
 
        '.meta_tax_implication': None,
 
        '.meta_tax_reporting': None,
 
        '.txn_date': ['TransactionDate'],
 
    }
 

	
 
    def __init__(self) -> None:
 
        self.group_name_map: Dict[HookName, Set[Type[Hook]]] = {
 
            t.__name__: set() for t in ALL_DIRECTIVES
 
        }
 
        self.group_name_map['all'] = set()
 

	
 
    def add_hook(self, hook_cls: Type[Hook]) -> Type[Hook]:
 
        self.group_name_map['all'].add(hook_cls)
 
        self.group_name_map[hook_cls.DIRECTIVE.__name__].add(hook_cls)
 
        for key in hook_cls.HOOK_GROUPS:
 
            self.group_name_map.setdefault(key, set()).add(hook_cls)
 
        return hook_cls  # to allow use as a decorator
 

	
 
    def import_hooks(self,
 
                     mod_name: str,
 
                     *hook_names: str,
 
                     package: Optional[str]=None,
 
    ) -> None:
 
        if not hook_names:
 
            _, _, hook_name = mod_name.rpartition('.')
 
            hook_names = (hook_name.title().replace('_', ''),)
 
        module = importlib.import_module(mod_name, package)
 
        for hook_name in hook_names:
 
            self.add_hook(getattr(module, hook_name))
 

	
 
    def load_included_hooks(self) -> None:
 
        for mod_name, hook_names in self.INCLUDED_HOOKS.items():
 
            self.import_hooks(mod_name, *(hook_names or []), package=self.__module__)
 

	
 
    def group_by_directive(self, config_str: str='') -> Iterator[Tuple[HookName, Type[Hook]]]:
 
        config_str = config_str.strip()
 
        if not config_str:
 
            config_str = 'all'
 
        elif config_str.startswith('-'):
 
            config_str = 'all ' + config_str
 
        available_hooks: Set[Type[Hook]] = set()
 
        for token in config_str.split():
 
            if token.startswith('-'):
 
                update_available = available_hooks.difference_update
 
                key = token[1:]
 
            else:
 
                update_available = available_hooks.update
 
                key = token
 
            try:
 
                update_set = self.group_name_map[key]
 
            except KeyError:
 
                raise ValueError("configuration refers to unknown hooks {!r}".format(key)) from None
 
            else:
 
                update_available(update_set)
 
        for directive in ALL_DIRECTIVES:
 
            key = directive.__name__
 
            for hook in self.group_name_map[key] & available_hooks:
 
                yield key, hook
 

	
 

	
 
def run(
 
        entries: Entries,
 
        options_map: OptionsMap,
 
        config: str='',
 
        hook_registry: Optional[HookRegistry]=None,
 
) -> Tuple[Entries, Errors]:
 
    if hook_registry is None:
 
        hook_registry = HookRegistry()
 
        hook_registry.load_included_hooks()
 
    errors: Errors = []
 
    hooks: Dict[HookName, List[Hook]] = {
 
        # mypy thinks NamedTuples don't have __name__ but they do at runtime.
 
        t.__name__: [] for t in bc_data.ALL_DIRECTIVES  # type:ignore[attr-defined]
 
    }
 
    user_config = configmod.Config()
 
    try:
 
        user_config.load_file()
 
    except OSError as error:
 
        logger.debug("error reading configuration file %s: %s",
 
                     error.filename, error.strerror, exc_info=True)
 
        errors.append(ConfigurationError(
 
            f"error reading configuration file {error.filename}: {error.strerror}",
 
            source={'filename': error.filename},
 
        ))
 
    for key, hook_type in hook_registry.group_by_directive(config):
 
        try:
 
            hook = hook_type(user_config)
 
        except Error as error:
 
            errors.append(error)
 
        else:
 
            hooks[key].append(hook)
 
    for entry in entries:
 
        entry_type = type(entry).__name__
 
        for hook in hooks[entry_type]:
 
            errors.extend(hook.run(entry))
 
    return entries, errors
 

	
conservancy_beancount/plugin/meta_tax_reporting.py
Show inline comments
 
new file 100644
 
"""meta_tax_reporting - Validate tax-reporting metadata links"""
 
# Copyright © 2021  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import datetime
 

	
 
from . import core
 
from .. import config as configmod
 
from .. import data
 
from .. import errors as errormod
 
from ..beancount_types import (
 
    Transaction,
 
)
 

	
 
from .meta_tax_implication import MetaTaxImplication
 
from ..ranges import DateRange
 

	
 
class MetaTaxReporting(core._RequireLinksPostingMetadataHook):
 
    CHECKED_IMPLICATIONS = frozenset(
 
        # We load values through the MetadataEnum to future-proof against
 
        # changes to tax-implication. This ensures that the set contains
 
        # canonical values, or else this code will crash if canonical values
 
        # can't be found.
 
        MetaTaxImplication.VALUES_ENUM[value] for value in [
 
            '1099-MISC-Other',
 
            '1099-NEC',
 
            'Foreign-Grantee',
 
            'Foreign-Individual-Contractor',
 
            'USA-501c3',
 
            'USA-Grantee',
 
        ])
 
    CHECKED_METADATA = ['tax-reporting']
 
    SKIP_FLAGS = '!'
 
    TXN_DATE_RANGE = DateRange(datetime.date(2020, 3, 1), datetime.date.max)
 

	
 
    def __init__(self, config: configmod.Config) -> None:
 
        self._implication_hook = MetaTaxImplication(config)
 
        # Yes, we create our own MetaTaxImplication hook. This is a little
 
        # weird but it does two things for us:
 
        # 1. We can check MetaTaxImplication._run_on_post() as part of our own
 
        # implementation without duplicating the logic.
 
        # 2. We can canonicalize values through the hook. We don't strictly
 
        # need an instance for that, but we have it anyway so doing it this way
 
        # is nicer.
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        if not self._implication_hook._run_on_post(txn, post):
 
            return False
 
        implication = str(post.meta.get('tax-implication') or '')
 
        normalized = self._implication_hook.VALUES_ENUM.get(implication)
 
        return normalized in self.CHECKED_IMPLICATIONS
setup.py
Show inline comments
 
#!/usr/bin/env python3
 

	
 
from setuptools import setup
 

	
 
setup(
 
    name='conservancy_beancount',
 
    description="Plugin, library, and reports for reading Conservancy's books",
 
    version='1.17.1',
 
    version='1.18.0',
 
    author='Software Freedom Conservancy',
 
    author_email='info@sfconservancy.org',
 
    license='GNU AGPLv3+',
 

	
 
    install_requires=[
 
        'babel>=2.6',  # Debian:python3-babel
 
        'beancount>=2.2',  # Debian:beancount
 
        'GitPython>=2.0',  # Debian:python3-git
 
        # 1.4.1 crashes when trying to save some documents.
 
        'odfpy>=1.4.0,!=1.4.1',  # Debian:python3-odf
 
        'pdfminer.six>=20200101',
 
        'PyYAML>=3.0',  # Debian:python3-yaml
 
        'regex',  # Debian:python3-regex
 
        'rt>=2.0',
 
    ],
 
    setup_requires=[
 
        'pytest-mypy',
 
        'pytest-runner',  # Debian:python3-pytest-runner
 
    ],
 
    tests_require=[
 
        'mypy>=0.770',  # Debian:python3-mypy
 
        'pytest',  # Debian:python3-pytest
 
    ],
 

	
 
    packages=[
 
        'conservancy_beancount',
 
        'conservancy_beancount.pdfforms',
 
        'conservancy_beancount.pdfforms.extract',
 
        'conservancy_beancount.plugin',
 
        'conservancy_beancount.reconcile',
 
        'conservancy_beancount.reports',
 
        'conservancy_beancount.tools',
 
    ],
 
    entry_points={
 
        'console_scripts': [
 
            'accrual-report = conservancy_beancount.reports.accrual:entry_point',
 
            'assemble-audit-reports = conservancy_beancount.tools.audit_report:entry_point',
 
            'balance-sheet-report = conservancy_beancount.reports.balance_sheet:entry_point',
 
            'budget-report = conservancy_beancount.reports.budget:entry_point',
 
            'bean-sort = conservancy_beancount.tools.sort_entries:entry_point',
 
            'extract-odf-links = conservancy_beancount.tools.extract_odf_links:entry_point',
 
            'fund-report = conservancy_beancount.reports.fund:entry_point',
 
            'ledger-report = conservancy_beancount.reports.ledger:entry_point',
 
            'opening-balances = conservancy_beancount.tools.opening_balances:entry_point',
 
            'pdfform-extract = conservancy_beancount.pdfforms.extract:entry_point',
 
            'pdfform-extract-irs990scheduleA = conservancy_beancount.pdfforms.extract.irs990scheduleA:entry_point',
 
            'pdfform-fill = conservancy_beancount.pdfforms.fill:entry_point',
 
            'reconcile-paypal = conservancy_beancount.reconcile.paypal:entry_point',
 
            'reconcile-statement = conservancy_beancount.reconcile.statement:entry_point',
 
            'split-ods-links = conservancy_beancount.tools.split_ods_links:entry_point',
 
        ],
 
    },
 
)
tests/test_meta_tax_reporting.py
Show inline comments
 
new file 100644
 
"""test_meta_tax_reporting.py - Unit tests for tax-reporting metadata validation"""
 
# Copyright © 2021  Brett Smith
 
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
 
#
 
# Full copyright and licensing details can be found at toplevel file
 
# LICENSE.txt in the repository.
 

	
 
import pytest
 

	
 
from . import testutil
 

	
 
from conservancy_beancount.plugin import meta_tax_reporting
 

	
 
TEST_KEY = 'tax-reporting'
 
IMPLICATION_KEY = 'tax-implication'
 

	
 
REQUIRED_ACCOUNTS = {
 
    'Assets:Checking',
 
    'Assets:Bank:Savings',
 
}
 

	
 
NON_REQUIRED_ACCOUNTS = {
 
    'Assets:Prepaid:Expenses',
 
    'Assets:Receivable:Accounts',
 
    'Liabilities:CreditCard',
 
}
 

	
 
REQUIRED_AMOUNTS = {-50, -500}
 
NON_REQUIRED_AMOUNTS = {-5, 500}
 

	
 
REQUIRED_IMPLICATIONS = {
 
    '1099',
 
    '1099-Misc-Other',
 
    'foreign-grantee',
 
    'Foreign-Individual-Contractor',
 
    'USA-501c3',
 
    'US-Grantee',
 
}
 

	
 
NON_REQUIRED_IMPLICATIONS = {
 
    'Bank-Transfer',
 
    'chargeback',
 
    'Foreign-Corp',
 
    'Loan',
 
    'refund',
 
    'Reimbursement',
 
    'retirement-pretax',
 
    'Tax-Payment',
 
    'us-corp',
 
    'w2',
 
}
 

	
 
@pytest.fixture(scope='module')
 
def hook():
 
    config = testutil.TestConfig(payment_threshold=10)
 
    return meta_tax_reporting.MetaTaxReporting(config)
 

	
 
@pytest.mark.parametrize('account,amount,implication,value', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
    testutil.LINK_METADATA_STRINGS,
 
))
 
def test_pass_on_txn(hook, account, amount, implication, value):
 
    txn_meta = {
 
        IMPLICATION_KEY: implication,
 
        TEST_KEY: value,
 
    }
 
    txn = testutil.Transaction(**txn_meta, postings=[
 
        (account, amount),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication,value', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
    testutil.LINK_METADATA_STRINGS,
 
))
 
def test_pass_on_post(hook, account, amount, implication, value):
 
    post_meta = {
 
        IMPLICATION_KEY: implication,
 
        TEST_KEY: value,
 
    }
 
    txn = testutil.Transaction(postings=[
 
        (account, amount, post_meta),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
))
 
def test_error_when_missing(hook, account, amount, implication):
 
    txn = testutil.Transaction(postings=[
 
        (account, amount, {IMPLICATION_KEY: implication}),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication,value', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
    testutil.NON_LINK_METADATA_STRINGS,
 
))
 
def test_error_when_empty(hook, account, amount, implication, value):
 
    txn_meta = {
 
        IMPLICATION_KEY: implication,
 
        TEST_KEY: value,
 
    }
 
    txn = testutil.Transaction(**txn_meta, postings=[
 
        (account, amount),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication,value', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
    testutil.NON_STRING_METADATA_VALUES,
 
))
 
def test_error_when_wrong_type(hook, account, amount, implication, value):
 
    txn_meta = {
 
        IMPLICATION_KEY: implication,
 
        TEST_KEY: value,
 
    }
 
    txn = testutil.Transaction(**txn_meta, postings=[
 
        (account, amount),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication', testutil.combine_values(
 
    NON_REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
))
 
def test_skip_by_account(hook, account, amount, implication):
 
    txn = testutil.Transaction(postings=[
 
        (account, amount, {IMPLICATION_KEY: implication}),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    NON_REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
))
 
def test_skip_by_amount(hook, account, amount, implication):
 
    txn = testutil.Transaction(postings=[
 
        (account, amount, {IMPLICATION_KEY: implication}),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    NON_REQUIRED_IMPLICATIONS,
 
))
 
def test_skip_by_implication(hook, account, amount, implication):
 
    txn = testutil.Transaction(postings=[
 
        (account, amount, {IMPLICATION_KEY: implication}),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('account,amount,implication', testutil.combine_values(
 
    REQUIRED_ACCOUNTS,
 
    REQUIRED_AMOUNTS,
 
    REQUIRED_IMPLICATIONS,
 
))
 
def test_skip_by_flag(hook, account, amount, implication):
 
    txn = testutil.Transaction(flag='!', postings=[
 
        (account, amount, {IMPLICATION_KEY: implication}),
 
        ('Expenses:Other', -amount),
 
    ])
 
    assert not list(hook.run(txn))
0 comments (0 inline, 0 general)