"""Test metadata for receivables 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_receivable_documentation TEST_ACCT = 'Assets:Receivable:Accounts' 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}}) @pytest.mark.parametrize('invoice,other_acct', testutil.combine_values( ISSUED_INVOICE_LINKS, ['Assets:Checking', 'Assets:Savings'], )) def test_paid_invoices_not_checked(hook, invoice, other_acct): txn = testutil.Transaction(postings=[ (TEST_ACCT, -250, {'invoice': invoice}), (other_acct, 250), ]) assert not list(hook.run(txn)) @pytest.mark.parametrize('account', [ 'Assets:Bank:Checking', 'Assets:Cash', 'Equity:OpeningBalance', 'Expenses:BankingFees', 'Liabilities:CreditCard', 'Liabilities:Payable:Accounts', ]) def test_does_not_apply_to_other_accounts(hook, account): meta = seed_meta() 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)) def test_not_required_on_flagged(hook): post_meta = seed_meta() check(hook, None, txn_meta={'flag': '!'}, post_meta=post_meta)