Files @ 5784068904e8
Branch filter:

Location: NPO-Accounting/conservancy_beancount/tests/test_meta_payable_documentation.py

bkuhn
payroll-type — US:403b:Employee:Roth — needed separate since taxable

Since Roth contributions are taxable, there are some reports that
need to include these amounts in total salary (i.e., when running a
report that seeks to show total taxable income for an employee). As
such, we need a `payroll-type` specifically for Roth 403(b)
contributions.
"""Test metadata for payables includes supporting documentation"""
# 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 random

import pytest

from . import testutil

from conservancy_beancount import errors as errormod
from conservancy_beancount.plugin import meta_payable_documentation

SUPPORTING_METADATA = [
    'approval',
    'contract',
]

NON_SUPPORTING_METADATA = [
    'check',
    'invoice',
    'purchase-order',
    'receipt',
    'statement',
]

TEST_ACCT = 'Liabilities:Payable:Accounts'
OTHER_ACCT = 'Expenses:Other'
MISSING_MSG = f"{TEST_ACCT} missing approval/contract"

# for supporting links, use the lists from testutil

@pytest.fixture(scope='module')
def hook():
    config = testutil.TestConfig(rt_client=testutil.RTClient())
    return meta_payable_documentation.MetaPayableDocumentation(config)

def seed_meta(support_values=testutil.NON_LINK_METADATA_STRINGS, **kwargs):
    meta = dict(testutil.combine_values(
        SUPPORTING_METADATA,
        support_values,
    ))
    meta.update(kwargs)
    return meta

def wrong_type_message(key, wrong_value):
    return  "{} has wrong type of {}: expected str but is a {}".format(
        TEST_ACCT,
        key,
        type(wrong_value).__name__,
    )

def check(hook, expected, test_acct=TEST_ACCT, other_acct=OTHER_ACCT, *,
          txn_meta={}, post_meta={}, min_amt=10):
    amount = '{:.02f}'.format(min_amt + random.random() * 100)
    txn = testutil.Transaction(**txn_meta, postings=[
        (test_acct, f'-{amount}', post_meta),
        (other_acct, amount),
    ])
    actual = {error.message for error in hook.run(txn)}
    if expected is None:
        assert not actual
    elif isinstance(expected, str):
        assert expected in actual
    else:
        assert actual == expected

@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
    SUPPORTING_METADATA,
    testutil.LINK_METADATA_STRINGS,
))
def test_valid_docs_in_post(hook, support_key, support_value):
    meta = seed_meta()
    meta[support_key] = support_value
    check(hook, None, post_meta=meta)

@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
    SUPPORTING_METADATA,
    testutil.LINK_METADATA_STRINGS,
))
def test_valid_docs_in_txn(hook, support_key, support_value):
    meta = seed_meta()
    meta[support_key] = support_value
    check(hook, None, txn_meta=meta)

@pytest.mark.parametrize('meta_type', ['post_meta', 'txn_meta'])
def test_no_valid_docs(hook, meta_type):
    meta = seed_meta()
    meta.update((key, value) for key, value in testutil.combine_values(
        NON_SUPPORTING_METADATA,
        testutil.LINK_METADATA_STRINGS,
    ))
    check(hook, {MISSING_MSG}, **{meta_type: meta})

@pytest.mark.parametrize('meta_type', ['post_meta', 'txn_meta'])
def test_docs_all_bad_type(hook, meta_type):
    meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
    expected = {
        wrong_type_message(key, value)
        for key, value in meta.items()
    }
    expected.add(MISSING_MSG)
    check(hook, expected, **{meta_type: meta})

@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
    SUPPORTING_METADATA,
    testutil.LINK_METADATA_STRINGS,
))
def test_type_errors_reported_with_valid_post_docs(hook, support_key, support_value):
    meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
    meta[support_key] = support_value
    expected = {
        wrong_type_message(key, value)
        for key, value in meta.items()
        if key != support_key
    }
    check(hook, expected, post_meta=meta)

@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
    SUPPORTING_METADATA,
    testutil.LINK_METADATA_STRINGS,
))
def test_type_errors_reported_with_valid_txn_docs(hook, support_key, support_value):
    meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
    meta[support_key] = support_value
    expected = {
        wrong_type_message(key, value)
        for key, value in meta.items()
        if key != support_key
    }
    check(hook, expected, txn_meta=meta)

def test_paid_accts_not_checked(hook):
    txn = testutil.Transaction(postings=[
        (TEST_ACCT, 250),
        (OTHER_ACCT, -250),
    ])
    assert not list(hook.run(txn))

@pytest.mark.parametrize('account', [
    'Assets:Bank:Checking',
    'Assets:Cash',
    'Assets:Receivable:Accounts',
    'Equity:OpeningBalance',
    'Expenses:BankingFees',
    'Income:Donations',
    'Liabilities:CreditCard',
    '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))

def test_not_required_on_flagged(hook):
    check(hook, None, txn_meta={'flag': '!'})