Changeset - 9f0c30738db8
[Not reviewed]
0 10 0
Brett Smith - 4 years ago 2020-04-09 19:12:04
brettcsmith@brettcsmith.org
plugin: Most validations skip opening balance transactions. RT#10642.
10 files changed with 78 insertions and 4 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/plugin/core.py
Show inline comments
...
 
@@ -182,13 +182,16 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 
    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:
 
        return txn.date in self.TXN_DATE_RANGE
 
        return (
 
            txn.date in self.TXN_DATE_RANGE
 
            and not data.is_opening_balance_txn(txn)
 
        )
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return True
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        if self._run_on_txn(txn):
conservancy_beancount/plugin/meta_entity.py
Show inline comments
...
 
@@ -46,12 +46,14 @@ class MetaEntity(core.TransactionHook):
 
    # However, current producers fail that regexp in a few different ways.
 
    # See the tests for specific cases.
 
    ENTITY_RE: Pattern[str] = regex.compile(f'^[{alnum}][-.{alnum}]*$', regex.VERSION1)
 
    del alnum
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        if data.is_opening_balance_txn(txn):
 
            return
 
        txn_entity = txn.meta.get(self.METADATA_KEY, txn.payee)
 
        if txn_entity is None:
 
            txn_entity_ok = None
 
        elif isinstance(txn_entity, str):
 
            txn_entity_ok = bool(self.ENTITY_RE.match(txn_entity))
 
        else:
conservancy_beancount/plugin/meta_project.py
Show inline comments
...
 
@@ -37,12 +37,13 @@ from typing import (
 
)
 

	
 
class MetaProject(core._NormalizePostingMetadataHook):
 
    DEFAULT_PROJECT = 'Conservancy'
 
    PROJECT_DATA_PATH = Path('Projects', 'project-data.yml')
 
    VALUES_ENUM = core.MetadataEnum('project', {DEFAULT_PROJECT})
 
    RESTRICTED_FUNDS_ACCT = 'Equity:Funds:Restricted'
 

	
 
    def __init__(self, config: configmod.Config, source_path: Path=PROJECT_DATA_PATH) -> None:
 
        repo_path = config.repository_path()
 
        if repo_path is None:
 
            self._config_error("no repository configured")
 
        project_data_path = repo_path / source_path
...
 
@@ -75,24 +76,42 @@ class MetaProject(core._NormalizePostingMetadataHook):
 
            source['filename'] = str(filename)
 
        raise errormod.ConfigurationError(
 
            "cannot load project data: " + msg,
 
            source=source,
 
        )
 

	
 
    def _run_on_post(self, txn: Transaction, post: data.Posting) -> bool:
 
    def _run_on_opening_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        return post.account.is_under(self.RESTRICTED_FUNDS_ACCT) is not None
 

	
 
    def _run_on_other_post(self, txn: Transaction, post: data.Posting) -> bool:
 
        if post.account.is_under('Liabilities'):
 
            return not post.account.is_credit_card()
 
        else:
 
            return post.account.is_under(
 
                'Assets:Receivable',
 
                'Expenses',
 
                'Income',
 
                self.RESTRICTED_FUNDS_ACCT,
 
            ) is not None
 

	
 
    def _default_value(self, txn: Transaction, post: data.Posting) -> MetaValueEnum:
 
        if post.account.is_under(
 
                'Expenses:Payroll',
 
                'Liabilities:Payable:Vacation',
 
        ):
 
            return self.DEFAULT_PROJECT
 
        else:
 
            raise errormod.InvalidMetadataError(txn, self.METADATA_KEY, None, post)
 

	
 
    def _run_on_txn(self, txn: Transaction) -> bool:
 
        return txn.date in self.TXN_DATE_RANGE
 

	
 
    def run(self, txn: Transaction) -> errormod.Iter:
 
        # mypy says we can't assign over a method.
 
        # I understand why it wants to enforce thas as a blanket rule, but
 
        # we're substituting in another type-compatible method, so it's pretty
 
        # safe.
 
        if data.is_opening_balance_txn(txn):
 
            self._run_on_post = self._run_on_opening_post  # type:ignore[assignment]
 
        else:
 
            self._run_on_post = self._run_on_other_post  # type:ignore[assignment]
 
        return super().run(txn)
tests/test_meta_entity.py
Show inline comments
...
 
@@ -162,6 +162,10 @@ def test_which_accounts_required_on(hook, account, required):
 
    if not required:
 
        assert not errors
 
    else:
 
        assert errors
 
        assert any(error.message == "{} missing entity".format(account)
 
                   for error in errors)
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction.opening_balance()
 
    assert not list(hook.run(txn))
tests/test_meta_invoice.py
Show inline comments
...
 
@@ -26,13 +26,13 @@ REQUIRED_ACCOUNTS = {
 
    'Liabilities:Payable:Accounts',
 
    'Liabilities:Payable:Vacation',
 
}
 

	
 
NON_REQUIRED_ACCOUNTS = {
 
    'Assets:Cash',
 
    'Equity:OpeningBalance',
 
    'Equity:Retained',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:CreditCard',
 
}
 

	
 
TEST_KEY = 'invoice'
...
 
@@ -137,6 +137,10 @@ def test_missing_invoice(hook, acct1, acct2):
 
    txn = testutil.Transaction(postings=[
 
        (acct2, -25),
 
        (acct1, 25),
 
    ])
 
    actual = {error.message for error in hook.run(txn)}
 
    assert actual == {"{} missing {}".format(acct1, TEST_KEY)}
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction.opening_balance()
 
    assert not list(hook.run(txn))
tests/test_meta_payable_documentation.py
Show inline comments
...
 
@@ -160,6 +160,14 @@ def test_paid_accts_not_checked(hook):
 
    'Liabilities:Payable:Vacation',
 
    'Liabilities:UnearnedIncome:Donations',
 
])
 
def test_does_not_apply_to_other_accounts(hook, account):
 
    meta = seed_meta()
 
    check(hook, None, account, post_meta=meta)
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction(postings=[
 
        ('Liabilities:Payable:Accounts', -15),
 
        ('Liabilities:Payable:Vacation', -25),
 
        (next(testutil.OPENING_EQUITY_ACCOUNTS), 40),
 
    ])
 
    assert not list(hook.run(txn))
tests/test_meta_paypal_id.py
Show inline comments
...
 
@@ -184,6 +184,13 @@ def test_invoice_payment_transaction_ok(hook, txn_id, inv_id):
 
    txn = testutil.Transaction(**{TEST_KEY: txn_id}, postings=[
 
        ('Assets:Receivable:Accounts', -100, {TEST_KEY: inv_id}),
 
        ('Assets:PayPal', 97),
 
        ('Expenses:BankingFees', 3),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:PayPal', 1000),
 
        (next(testutil.OPENING_EQUITY_ACCOUNTS), -1000),
 
    ])
 
    assert not list(hook.run(txn))
tests/test_meta_project.py
Show inline comments
...
 
@@ -87,12 +87,14 @@ def test_invalid_values_on_transactions(hook, src_value):
 

	
 
@pytest.mark.parametrize('account,required', [
 
    ('Assets:Cash', False),
 
    ('Assets:Receivable:Accounts', True),
 
    ('Assets:Receivable:Loans', True),
 
    ('Equity:OpeningBalance', False),
 
    ('Equity:Funds:Restricted', True),
 
    ('Equity:Funds:Unrestricted', False),
 
    ('Expenses:General', True),
 
    ('Income:Donations', True),
 
    ('Liabilities:CreditCard', False),
 
    ('Liabilities:Payable:Accounts', True),
 
    # We do want a "project" for Lia:Pay:Vacation but it has a default value
 
    ('Liabilities:Payable:Vacation', False),
...
 
@@ -151,6 +153,16 @@ def test_missing_project_data(repo_path):
 
    ('..', 'LICENSE.txt'),
 
])
 
def test_invalid_project_data(repo_path_s, data_path_s):
 
    config = testutil.TestConfig(repo_path=repo_path_s)
 
    with pytest.raises(errormod.ConfigurationError):
 
        meta_project.MetaProject(config, Path(data_path_s))
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction.opening_balance('Equity:Funds:Unrestricted')
 
    assert not list(hook.run(txn))
 

	
 
def test_always_required_on_restricted_funds(hook):
 
    acct = 'Equity:Funds:Restricted'
 
    txn = testutil.Transaction.opening_balance(acct)
 
    actual = {error.message for error in hook.run(txn)}
 
    assert actual == {f'{acct} missing project'}
tests/test_meta_receipt.py
Show inline comments
...
 
@@ -83,13 +83,13 @@ KNOWN_FALLBACKS = {acct.fallback_meta for acct in ACCOUNTS if acct.fallback_meta
 
# trims unnecessary test time.
 
NOT_REQUIRED_ACCOUNTS = itertools.cycle([
 
    # Only paypal-id is required for PayPal transactions
 
    'Assets:PayPal',
 
    'Assets:Prepaid:Expenses',
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Equity:Retained',
 
    'Expenses:Other',
 
    'Income:Other',
 
    'Liabilities:Payable:Accounts',
 
    'Liabilities:UnearnedIncome:Donations',
 
])
 

	
...
 
@@ -339,6 +339,13 @@ def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, value):
 
    txn = testutil.Transaction(postings=[
 
        ('Income:Donations', '-.1'),
 
        ('Expenses:BankingFees', '.1'),
 
        (test_acct.name, 0, {test_acct.fallback_meta: value}),
 
    ])
 
    assert not list(hook.run(txn))
 

	
 
@pytest.mark.parametrize('test_acct,equity_acct', testutil.combine_values(
 
    ACCOUNTS,
 
    testutil.OPENING_EQUITY_ACCOUNTS,
 
))
 
def test_not_required_on_opening(hook, test_acct, equity_acct):
 
    check(hook, test_acct, equity_acct, None)
tests/test_meta_receivable_documentation.py
Show inline comments
...
 
@@ -202,6 +202,14 @@ def test_does_not_apply_to_other_accounts(hook, account):
 
    check(hook, None, account, 'Expenses:Other', post_meta=meta)
 

	
 
def test_configuration_error_without_rt():
 
    config = testutil.TestConfig()
 
    with pytest.raises(errormod.ConfigurationError):
 
        meta_receivable_documentation.MetaReceivableDocumentation(config)
 

	
 
def test_not_required_on_opening(hook):
 
    txn = testutil.Transaction(postings=[
 
        ('Assets:Receivable:Accounts', 100),
 
        ('Assets:Receivable:Loans', 200),
 
        (next(testutil.OPENING_EQUITY_ACCOUNTS), -300),
 
    ])
 
    assert not list(hook.run(txn))
0 comments (0 inline, 0 general)