Files
@ 59dfbb78d11f
Branch filter:
Location: NPO-Accounting/conservancy_beancount/tests/test_meta_receipt.py - annotation
59dfbb78d11f
13.0 KiB
text/x-python
reconcile: Add prototype CSV reconciliation report.
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | 2909c405e620 2909c405e620 1b7fdf4f3b00 2909c405e620 1b7fdf4f3b00 1b7fdf4f3b00 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 51db04dc200c 8a2721ec0fa4 8a2721ec0fa4 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 8a2721ec0fa4 8a2721ec0fa4 6658696d0687 6658696d0687 8a2721ec0fa4 8a2721ec0fa4 6658696d0687 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2b7c1acff47b 51db04dc200c 51db04dc200c 2b7c1acff47b 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 8a2721ec0fa4 8a2721ec0fa4 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 51db04dc200c 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 f7bb036366ca f7bb036366ca c712105bed3c c712105bed3c 9f0c30738db8 2909c405e620 2909c405e620 c712105bed3c c712105bed3c 8a2721ec0fa4 8a2721ec0fa4 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 8a2721ec0fa4 7cac21b780d3 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 51db04dc200c 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 8a2721ec0fa4 51db04dc200c 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 2909c405e620 fdb62dd1c641 51db04dc200c 2909c405e620 51db04dc200c 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 51db04dc200c 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 8a2721ec0fa4 51db04dc200c 2909c405e620 51db04dc200c 6658696d0687 8a2721ec0fa4 2909c405e620 2909c405e620 51db04dc200c 51db04dc200c 2909c405e620 fdb62dd1c641 51db04dc200c 2909c405e620 51db04dc200c 2909c405e620 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 51db04dc200c 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 fdb62dd1c641 51db04dc200c 6658696d0687 51db04dc200c 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 fdb62dd1c641 51db04dc200c 6658696d0687 51db04dc200c 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 51db04dc200c 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 fdb62dd1c641 51db04dc200c 6658696d0687 51db04dc200c 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 6658696d0687 fdb62dd1c641 51db04dc200c 6658696d0687 51db04dc200c 6658696d0687 8a2721ec0fa4 51db04dc200c 8a2721ec0fa4 51db04dc200c 2909c405e620 2909c405e620 8a2721ec0fa4 8a2721ec0fa4 8a2721ec0fa4 600c9d9d6f1d 51db04dc200c 600c9d9d6f1d 600c9d9d6f1d 600c9d9d6f1d 600c9d9d6f1d 51db04dc200c 600c9d9d6f1d 600c9d9d6f1d 600c9d9d6f1d 51db04dc200c 600c9d9d6f1d 600c9d9d6f1d 600c9d9d6f1d 51db04dc200c 600c9d9d6f1d 600c9d9d6f1d 9f0c30738db8 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 f9411e0ffe79 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 9f0c30738db8 536b50b478d8 536b50b478d8 536b50b478d8 536b50b478d8 536b50b478d8 536b50b478d8 536b50b478d8 | """Test validation of receipt metadata"""
# 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 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.Sequence[str] = ()
def missing_message(self, include_fallback=True):
return "{} missing {}{}{}".format(
self.name,
TEST_KEY,
'/' if self.fallback_meta else '',
'/'.join(self.fallback_meta),
)
def wrong_type_message(self, wrong_value, key=TEST_KEY):
expect_type = 'Decimal' if key == 'check-id' else 'str'
return "{} has wrong type of {}: expected {} but is a {}".format(
self.name,
key,
expect_type,
type(wrong_value).__name__,
)
ACCOUNTS = [AccountForTesting._make(t) for t in [
('Assets:Bank:CheckCard', PostType.CREDIT, ('check', 'invoice')),
('Assets:Bank:CheckCard', PostType.DEBIT, ('check-id',)),
('Assets:Cash', PostType.BOTH, ()),
('Assets:Checking', PostType.CREDIT, ('check', 'invoice')),
('Assets:Checking', PostType.DEBIT, ('check-id',)),
('Assets:Savings', PostType.BOTH, ()),
('Liabilities:CreditCard', PostType.CREDIT, ()),
('Liabilities:CreditCard', PostType.DEBIT, ('invoice',)),
]]
ACCOUNTS_WITH_LINK_FALLBACK = [
(acct, fallback_key)
for acct in ACCOUNTS
for fallback_key in acct.fallback_meta
if fallback_key != 'check-id'
]
ACCOUNTS_WITH_CHECK_ID_FALLBACK = [
acct for acct in ACCOUNTS if 'check-id' in acct.fallback_meta
]
# 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([
# Only paypal-id is required for PayPal transactions
'Assets:PayPal',
'Assets:Prepaid:Expenses',
'Assets:Receivable:Accounts',
'Equity:Retained',
'Expenses:Other',
'Income:Other',
'Liabilities:Payable:Accounts',
'Liabilities:UnearnedIncome:Donations',
])
CHECK_IDS = (decimal.Decimal(n) for n in itertools.count(1))
def BAD_CHECK_IDS():
# Valid check-id values are positive integers
yield decimal.Decimal(0)
yield -next(CHECK_IDS)
yield next(CHECK_IDS) * decimal.Decimal('1.1')
BAD_CHECK_IDS = BAD_CHECK_IDS()
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_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_fallback_on_post(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
check(hook, test_acct, other_acct, None, post_meta={meta_key: value})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_fallback_on_post(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
check(hook, test_acct, other_acct, {test_acct.missing_message()},
post_meta={meta_key: value})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_fallback_on_post(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
expected = {
test_acct.missing_message(),
test_acct.wrong_type_message(value, meta_key),
}
check(hook, test_acct, other_acct, expected, post_meta={meta_key: value})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_valid_fallback_on_txn(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
check(hook, test_acct, other_acct, None, txn_meta={meta_key: value})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_LINK_METADATA_STRINGS,
))
def test_invalid_fallback_on_txn(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
check(hook, test_acct, other_acct, {test_acct.missing_message()},
txn_meta={meta_key: value})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_fallback_on_txn(hook, test_pair, other_acct, value):
test_acct, meta_key = test_pair
expected = {
test_acct.missing_message(),
test_acct.wrong_type_message(value, meta_key),
}
check(hook, test_acct, other_acct, expected, txn_meta={meta_key: value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
CHECK_IDS,
))
def test_valid_check_id_on_post(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None, post_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
BAD_CHECK_IDS,
))
def test_invalid_check_id_on_post(hook, test_acct, other_acct, value):
expected = {
test_acct.missing_message(),
f"{test_acct.name} has invalid check-id: {value}",
}
check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_check_id_on_post(hook, test_acct, other_acct, value):
if isinstance(value, decimal.Decimal):
value = ''
expected = {
test_acct.missing_message(),
test_acct.wrong_type_message(value, 'check-id'),
}
check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
CHECK_IDS,
))
def test_valid_check_id_on_txn(hook, test_acct, other_acct, value):
check(hook, test_acct, other_acct, None, txn_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
BAD_CHECK_IDS,
))
def test_invalid_check_id_on_txn(hook, test_acct, other_acct, value):
expected = {
test_acct.missing_message(),
f"{test_acct.name} has invalid check-id: {value}",
}
check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_CHECK_ID_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.NON_STRING_METADATA_VALUES,
))
def test_bad_type_check_id_on_txn(hook, test_acct, other_acct, value):
if isinstance(value, decimal.Decimal):
value = ''
expected = {
test_acct.missing_message(),
test_acct.wrong_type_message(value, 'check-id'),
}
check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
@pytest.mark.parametrize('test_acct,other_acct,key,value', testutil.combine_values(
[acct for acct in ACCOUNTS if not acct.fallback_meta],
NOT_REQUIRED_ACCOUNTS,
{key for acct in ACCOUNTS for key in acct.fallback_meta},
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})
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
ACCOUNTS_WITH_LINK_FALLBACK,
NOT_REQUIRED_ACCOUNTS,
testutil.LINK_METADATA_STRINGS,
))
def test_fallback_on_zero_amount_postings(hook, test_pair, other_acct, value):
# Unfortunately it does happen that we get donations that go 100% to
# banking fees, and our importer writes a zero-amount posting to the
# Assets account.
test_acct, meta_key = test_pair
txn = testutil.Transaction(postings=[
('Income:Donations', '-.1'),
('Expenses:BankingFees', '.1'),
(test_acct.name, 0, {meta_key: value}),
])
assert not list(hook.run(txn))
@pytest.mark.parametrize('test_acct', (
acct for acct in ACCOUNTS
if acct.name.startswith('Assets:')
and acct.required_types & PostType.CREDIT
))
def test_not_required_on_interest(hook, test_acct):
check(hook, test_acct, 'Income:Interest', None,
check_type=PostType.CREDIT)
@pytest.mark.parametrize('test_acct', (
acct for acct in ACCOUNTS
if acct.name.startswith('Assets:')
and acct.required_types & PostType.DEBIT
))
def test_required_on_reverse_interest(hook, test_acct):
check(hook, test_acct, 'Income:Interest', {test_acct.missing_message()},
check_type=PostType.DEBIT)
@pytest.mark.parametrize('test_acct,equity_acct', testutil.combine_values(
ACCOUNTS,
testutil.OPENING_EQUITY_ACCOUNTS,
))
def test_not_required_on_opening(hook, test_acct, equity_acct):
check(hook, test_acct, equity_acct, None)
@pytest.mark.parametrize('test_acct,other_acct', testutil.combine_values(
ACCOUNTS,
NOT_REQUIRED_ACCOUNTS,
))
def test_not_required_on_flagged(hook, test_acct, other_acct):
check(hook, test_acct, other_acct, None, txn_meta={'flag': '!'})
|