File diff 1fc9363b26cb → a9eab2d4ea5f
tests/test_meta_receivable_documentation.py
Show inline comments
 
new file 100644
 
"""Test metadata for receivables includes supporting documentation"""
 
# Copyright © 2020  Brett Smith
 
#
 
# This program is free software: you can redistribute it and/or modify
 
# it under the terms of the GNU Affero General Public License as published by
 
# the Free Software Foundation, either version 3 of the License, or
 
# (at your option) any later version.
 
#
 
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
# GNU Affero General Public License for more details.
 
#
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
import random
 

	
 
import pytest
 

	
 
from . import testutil
 

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

	
 
TEST_ACCT = 'Accrued:AccountsReceivable'
 
OTHER_ACCT = 'Income:Donations'
 

	
 
SUPPORTING_METADATA = [
 
    'approval',
 
    'contract',
 
    'purchase-order',
 
]
 

	
 
NON_SUPPORTING_METADATA = [
 
    'check',
 
    'receipt',
 
    'statement',
 
]
 

	
 
ISSUED_INVOICE_LINKS = [
 
    'rt:1/6',
 
    'rt://ticket/2/attachments/10',
 
    'Financial/Invoices/Company_invoice-2020030405_as-sent.pdf',
 
    'Projects/Alpha/Invoices/ProjectInvoice-304567.pdf',
 
]
 

	
 
RECEIVED_INVOICE_LINKS = [
 
    'rt:1/4',
 
    'rt://ticket/2/attachments/14',
 
    'Financial/Invoices/Invoice-23456789.csv',
 
    'Projects/Bravo/Invoices/Statement304567.pdf',
 
]
 

	
 
MISSING_MSG = f"{TEST_ACCT} missing approval/contract/purchase-order"
 

	
 
# for supporting links, use the lists from testutil
 

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

	
 
def seed_meta(invoice=ISSUED_INVOICE_LINKS[0],
 
              support_values=testutil.NON_LINK_METADATA_STRINGS,
 
              **kwargs,
 
):
 
    meta = dict(testutil.combine_values(
 
        SUPPORTING_METADATA,
 
        support_values,
 
    ))
 
    meta.update(kwargs)
 
    meta['invoice'] = invoice
 
    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, amount, post_meta),
 
        (other_acct, f'-{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('invoice,support_key,support_value', testutil.combine_values(
 
    ISSUED_INVOICE_LINKS,
 
    SUPPORTING_METADATA,
 
    testutil.LINK_METADATA_STRINGS,
 
))
 
def test_valid_docs_in_post(hook, invoice, support_key, support_value):
 
    meta = seed_meta(invoice)
 
    meta[support_key] = support_value
 
    check(hook, None, post_meta=meta)
 

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

	
 
@pytest.mark.parametrize('invoice,meta_type', testutil.combine_values(
 
    ISSUED_INVOICE_LINKS,
 
    ['post_meta', 'txn_meta'],
 
))
 
def test_no_valid_docs(hook, invoice, meta_type):
 
    meta = seed_meta(invoice)
 
    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('invoice,meta_type', testutil.combine_values(
 
    ISSUED_INVOICE_LINKS,
 
    ['post_meta', 'txn_meta'],
 
))
 
def test_docs_all_bad_type(hook, invoice, meta_type):
 
    meta = seed_meta(invoice, testutil.NON_STRING_METADATA_VALUES)
 
    expected = {
 
        wrong_type_message(key, value)
 
        for key, value in meta.items()
 
        if key != 'invoice'
 
    }
 
    expected.add(MISSING_MSG)
 
    check(hook, expected, **{meta_type: meta})
 

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

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

	
 
@pytest.mark.parametrize('invoice,meta_type', testutil.combine_values(
 
    RECEIVED_INVOICE_LINKS,
 
    ['post_meta', 'txn_meta'],
 
))
 
def test_received_invoices_not_checked(hook, invoice, meta_type):
 
    check(hook, None, **{meta_type: {'invoice': invoice}})
 

	
 
def test_does_not_apply_to_payables(hook):
 
    meta = seed_meta()
 
    check(hook, None, 'Accrued:AccountsPayable', 'Expenses:Other', post_meta=meta)
 

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