Files
@ 9595d3334d5b
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_meta_receivable_documentation.py - annotation
9595d3334d5b
7.1 KiB
text/x-python
books: Add Loader.load_all method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f c712105bed3c a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 4437a130d656 c712105bed3c c712105bed3c c712105bed3c c712105bed3c c712105bed3c c712105bed3c c712105bed3c c712105bed3c c712105bed3c a9eab2d4ea5f c712105bed3c a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f a9eab2d4ea5f 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 536b50b478d8 536b50b478d8 536b50b478d8 536b50b478d8 | """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 = '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)
|