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
...
 
@@ -187,3 +187,6 @@ class _PostingHook(TransactionHook, metaclass=abc.ABCMeta):
 
    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)
 
        )
 

	
conservancy_beancount/plugin/meta_entity.py
Show inline comments
...
 
@@ -51,2 +51,4 @@ class MetaEntity(core.TransactionHook):
 
    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)
conservancy_beancount/plugin/meta_project.py
Show inline comments
...
 
@@ -42,2 +42,3 @@ class MetaProject(core._NormalizePostingMetadataHook):
 
    VALUES_ENUM = core.MetadataEnum('project', {DEFAULT_PROJECT})
 
    RESTRICTED_FUNDS_ACCT = 'Equity:Funds:Restricted'
 

	
...
 
@@ -80,3 +81,6 @@ class MetaProject(core._NormalizePostingMetadataHook):
 

	
 
    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'):
...
 
@@ -88,2 +92,3 @@ class MetaProject(core._NormalizePostingMetadataHook):
 
                'Income',
 
                self.RESTRICTED_FUNDS_ACCT,
 
            ) is not None
...
 
@@ -98 +103,15 @@ class MetaProject(core._NormalizePostingMetadataHook):
 
            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
...
 
@@ -167 +167,5 @@ def test_which_accounts_required_on(hook, account, required):
 
                   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
...
 
@@ -31,3 +31,3 @@ NON_REQUIRED_ACCOUNTS = {
 
    'Assets:Cash',
 
    'Equity:OpeningBalance',
 
    'Equity:Retained',
 
    'Expenses:Other',
...
 
@@ -142 +142,5 @@ def test_missing_invoice(hook, acct1, acct2):
 
    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
...
 
@@ -165 +165,9 @@ def test_does_not_apply_to_other_accounts(hook, account):
 
    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
...
 
@@ -189 +189,8 @@ def test_invoice_payment_transaction_ok(hook, txn_id, inv_id):
 
    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
...
 
@@ -92,2 +92,4 @@ def test_invalid_values_on_transactions(hook, src_value):
 
    ('Equity:OpeningBalance', False),
 
    ('Equity:Funds:Restricted', True),
 
    ('Equity:Funds:Unrestricted', False),
 
    ('Expenses:General', True),
...
 
@@ -156 +158,11 @@ def test_invalid_project_data(repo_path_s, data_path_s):
 
        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
...
 
@@ -88,3 +88,3 @@ NOT_REQUIRED_ACCOUNTS = itertools.cycle([
 
    'Assets:Receivable:Accounts',
 
    'Equity:OpeningBalance',
 
    'Equity:Retained',
 
    'Expenses:Other',
...
 
@@ -344 +344,8 @@ def test_fallback_on_zero_amount_postings(hook, test_acct, other_acct, 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
...
 
@@ -207 +207,9 @@ def test_configuration_error_without_rt():
 
        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)