Files
@ 499f18ff623c
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_meta_receipt.py
499f18ff623c
8.6 KiB
text/x-python
meta_entity: Adjust what entities are allowed based on today's books.
See the comments throughout for more discussion about what cases are
or aren't allowed, and why.
See the comments throughout for more discussion about what cases are
or aren't allowed, and why.
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | """Test validation of receipt metadata"""
# 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 decimal
import enum
import itertools
import random
import typing
import pytest
from . import testutil
from conservancy_beancount.plugin import meta_receipt
TEST_KEY = 'receipt'
class PostType(enum.IntFlag):
NONE = 0
CREDIT = 1
DEBIT = 2
BOTH = CREDIT | DEBIT
class AccountForTesting(typing.NamedTuple):
name: str
required_types: PostType
fallback_meta: typing.Optional[str]
def missing_message(self, include_fallback=True):
if self.fallback_meta is None or not include_fallback:
rest = ""
else:
rest = f" or {self.fallback_meta}"
return f"{self.name} missing {TEST_KEY}{rest}"
def wrong_type_message(self, wrong_value, key=TEST_KEY):
return "{} has wrong type of {}: expected str but is a {}".format(
self.name,
key,
type(wrong_value).__name__,
)
ACCOUNTS = [AccountForTesting._make(t) for t in [
('Assets:Bank:CheckCard', PostType.BOTH, 'check'),
('Assets:Cash', PostType.BOTH, None),
('Assets:Checking', PostType.BOTH, 'check'),
('Assets:PayPal', PostType.CREDIT, 'paypal-id'),
('Assets:PayPal', PostType.DEBIT, None),
('Assets:Savings', PostType.BOTH, None),
('Liabilities:CreditCard', PostType.CREDIT, None),
('Liabilities:CreditCard', PostType.DEBIT, 'invoice'),
]]
ACCOUNTS_WITH_FALLBACKS = [acct for acct in ACCOUNTS if acct.fallback_meta]
ACCOUNTS_WITHOUT_FALLBACKS = [acct for acct in ACCOUNTS if not acct.fallback_meta]
KNOWN_FALLBACKS = {acct.fallback_meta for acct in ACCOUNTS_WITH_FALLBACKS}
# These are mostly fill-in values.
# We don't need to run every test on every value for these, just enough to
# convince ourselves the hook never reports errors against these accounts.
# Making this a iterator rather than a sequence means testutil.combine_values
# doesn't require the decorated test to go over every value, which in turn
# trims unnecessary test time.
NOT_REQUIRED_ACCOUNTS = itertools.cycle([
'Accrued:AccountsPayable',
'Accrued:AccountsReceivable',
'Assets:PrepaidExpenses',
'Assets:PrepaidVacation',
'Expenses:Other',
'Income:Other',
'UnearnedIncome:Donations',
])
def check(hook, test_acct, other_acct, expected, *,
txn_meta={}, post_meta={}, check_type=PostType.BOTH, min_amt=0):
check_type &= test_acct.required_types
assert check_type, "tried to test a non-applicable account"
if check_type == PostType.BOTH:
check(hook, test_acct, other_acct, expected,
txn_meta=txn_meta, post_meta=post_meta, check_type=PostType.CREDIT)
check_type = PostType.DEBIT
amount = decimal.Decimal('{:.02f}'.format(min_amt + random.random() * 100))
if check_type == PostType.DEBIT:
amount = -amount
txn = testutil.Transaction(**txn_meta, postings=[
(test_acct.name, 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.fixture(scope='module')
def hook():
config = testutil.TestConfig()
return meta_receipt.MetaReceipt(config)
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_receipt_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None, post_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_receipt_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, {test_acct.missing_message()},
post_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_receipt_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, test_acct.wrong_type_message(value),
post_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_receipt_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None, txn_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_receipt_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, {test_acct.missing_message()},
txn_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_receipt_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, test_acct.wrong_type_message(value),
txn_meta={TEST_KEY: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_fallback_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None,
post_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_fallback_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, {test_acct.missing_message()},
post_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_fallback_on_post(hook, test_acct, other_acct, value):
expected = {
test_acct.missing_message(False),
test_acct.wrong_type_message(value, test_acct.fallback_meta),
}
check(hook, test_acct, other_acct, expected,
post_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_fallback_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None,
txn_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_fallback_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, {test_acct.missing_message()},
txn_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_fallback_on_txn(hook, test_acct, other_acct, value):
expected = {
test_acct.missing_message(False),
test_acct.wrong_type_message(value, test_acct.fallback_meta),
}
check(hook, test_acct, other_acct, expected,
txn_meta={test_acct.fallback_meta: value})
@pytest.mark.parametrize('test_acct,other_acct,key,value', testutil.combine_values(
ACCOUNTS_WITHOUT_FALLBACKS,
NOT_REQUIRED_ACCOUNTS,
KNOWN_FALLBACKS,
testutil.LINK_METADATA_STRINGS,
))
def test_fallback_not_accepted_on_other_accounts(hook, test_acct, other_acct, key, value):
check(hook, test_acct, other_acct, {test_acct.missing_message()},
post_meta={key: value})
|