2020-04-07 19:29:15 +00:00
|
|
|
"""Test metadata for payables 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_payable_documentation
|
|
|
|
|
|
|
|
SUPPORTING_METADATA = [
|
2020-04-07 19:31:58 +00:00
|
|
|
'approval',
|
2020-04-07 19:29:15 +00:00
|
|
|
'contract',
|
|
|
|
]
|
|
|
|
|
|
|
|
NON_SUPPORTING_METADATA = [
|
|
|
|
'check',
|
2020-04-07 19:31:58 +00:00
|
|
|
'invoice',
|
2020-04-07 19:29:15 +00:00
|
|
|
'purchase-order',
|
|
|
|
'receipt',
|
|
|
|
'statement',
|
|
|
|
]
|
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
TEST_ACCT = 'Liabilities:Payable:Accounts'
|
2020-04-07 19:29:15 +00:00
|
|
|
OTHER_ACCT = 'Expenses:Other'
|
2020-04-07 19:44:40 +00:00
|
|
|
MISSING_MSG = f"{TEST_ACCT} missing approval/contract"
|
2020-04-07 19:29:15 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
def wrong_type_message(key, wrong_value):
|
2020-04-07 19:29:15 +00:00
|
|
|
return "{} has wrong type of {}: expected str but is a {}".format(
|
2020-04-07 19:44:40 +00:00
|
|
|
TEST_ACCT,
|
2020-04-07 19:29:15 +00:00
|
|
|
key,
|
|
|
|
type(wrong_value).__name__,
|
|
|
|
)
|
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
def check(hook, expected, test_acct=TEST_ACCT, other_acct=OTHER_ACCT, *,
|
2020-04-07 19:29:15 +00:00
|
|
|
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
|
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
|
2020-04-07 19:29:15 +00:00
|
|
|
SUPPORTING_METADATA,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-04-07 19:44:40 +00:00
|
|
|
def test_valid_docs_in_post(hook, support_key, support_value):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta()
|
|
|
|
meta[support_key] = support_value
|
2020-04-07 19:44:40 +00:00
|
|
|
check(hook, None, post_meta=meta)
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
|
2020-04-07 19:29:15 +00:00
|
|
|
SUPPORTING_METADATA,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-04-07 19:44:40 +00:00
|
|
|
def test_valid_docs_in_txn(hook, support_key, support_value):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta()
|
|
|
|
meta[support_key] = support_value
|
2020-04-07 19:44:40 +00:00
|
|
|
check(hook, None, txn_meta=meta)
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('meta_type', ['post_meta', 'txn_meta'])
|
|
|
|
def test_no_valid_docs(hook, meta_type):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta()
|
|
|
|
meta.update((key, value) for key, value in testutil.combine_values(
|
|
|
|
NON_SUPPORTING_METADATA,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-04-07 19:44:40 +00:00
|
|
|
check(hook, {MISSING_MSG}, **{meta_type: meta})
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('meta_type', ['post_meta', 'txn_meta'])
|
|
|
|
def test_docs_all_bad_type(hook, meta_type):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
|
|
|
|
expected = {
|
2020-04-07 19:44:40 +00:00
|
|
|
wrong_type_message(key, value)
|
2020-04-07 19:29:15 +00:00
|
|
|
for key, value in meta.items()
|
|
|
|
}
|
2020-04-07 19:44:40 +00:00
|
|
|
expected.add(MISSING_MSG)
|
|
|
|
check(hook, expected, **{meta_type: meta})
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
|
2020-04-07 19:29:15 +00:00
|
|
|
SUPPORTING_METADATA,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-04-07 19:44:40 +00:00
|
|
|
def test_type_errors_reported_with_valid_post_docs(hook, support_key, support_value):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
|
|
|
|
meta[support_key] = support_value
|
|
|
|
expected = {
|
2020-04-07 19:44:40 +00:00
|
|
|
wrong_type_message(key, value)
|
2020-04-07 19:29:15 +00:00
|
|
|
for key, value in meta.items()
|
|
|
|
if key != support_key
|
|
|
|
}
|
2020-04-07 19:44:40 +00:00
|
|
|
check(hook, expected, post_meta=meta)
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
@pytest.mark.parametrize('support_key,support_value', testutil.combine_values(
|
2020-04-07 19:29:15 +00:00
|
|
|
SUPPORTING_METADATA,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-04-07 19:44:40 +00:00
|
|
|
def test_type_errors_reported_with_valid_txn_docs(hook, support_key, support_value):
|
2020-04-07 19:29:15 +00:00
|
|
|
meta = seed_meta(testutil.NON_STRING_METADATA_VALUES)
|
|
|
|
meta[support_key] = support_value
|
|
|
|
expected = {
|
2020-04-07 19:44:40 +00:00
|
|
|
wrong_type_message(key, value)
|
2020-04-07 19:29:15 +00:00
|
|
|
for key, value in meta.items()
|
|
|
|
if key != support_key
|
|
|
|
}
|
2020-04-07 19:44:40 +00:00
|
|
|
check(hook, expected, txn_meta=meta)
|
2020-04-07 19:29:15 +00:00
|
|
|
|
2020-04-07 19:44:40 +00:00
|
|
|
def test_paid_accts_not_checked(hook):
|
2020-04-07 19:29:15 +00:00
|
|
|
txn = testutil.Transaction(postings=[
|
2020-04-07 19:44:40 +00:00
|
|
|
(TEST_ACCT, 250),
|
|
|
|
(OTHER_ACCT, -250),
|
2020-04-07 19:29:15 +00:00
|
|
|
])
|
|
|
|
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',
|
2020-04-07 19:44:40 +00:00
|
|
|
'Liabilities:Payable:Vacation',
|
2020-04-07 19:29:15 +00:00
|
|
|
'Liabilities:UnearnedIncome:Donations',
|
|
|
|
])
|
|
|
|
def test_does_not_apply_to_other_accounts(hook, account):
|
|
|
|
meta = seed_meta()
|
|
|
|
check(hook, None, account, post_meta=meta)
|