2020-03-29 02:19:49 +00:00
|
|
|
"""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/>.
|
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
import decimal
|
|
|
|
import enum
|
|
|
|
import itertools
|
|
|
|
import random
|
|
|
|
import typing
|
|
|
|
|
2020-03-29 02:19:49 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from . import testutil
|
|
|
|
|
|
|
|
from conservancy_beancount.plugin import meta_receipt
|
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
TEST_KEY = 'receipt'
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
class PostType(enum.IntFlag):
|
|
|
|
NONE = 0
|
|
|
|
CREDIT = 1
|
|
|
|
DEBIT = 2
|
|
|
|
BOTH = CREDIT | DEBIT
|
|
|
|
|
|
|
|
|
|
|
|
class AccountForTesting(typing.NamedTuple):
|
|
|
|
name: str
|
|
|
|
required_types: PostType
|
2020-05-15 20:46:20 +00:00
|
|
|
fallback_meta: typing.Sequence[str] = ()
|
2020-03-30 18:49:46 +00:00
|
|
|
|
|
|
|
def missing_message(self, include_fallback=True):
|
2020-05-15 20:46:20 +00:00
|
|
|
return "{} missing {}{}{}".format(
|
|
|
|
self.name,
|
|
|
|
TEST_KEY,
|
|
|
|
'/' if self.fallback_meta else '',
|
|
|
|
'/'.join(self.fallback_meta),
|
|
|
|
)
|
2020-03-30 18:49:46 +00:00
|
|
|
|
|
|
|
def wrong_type_message(self, wrong_value, key=TEST_KEY):
|
2020-04-04 14:54:08 +00:00
|
|
|
expect_type = 'Decimal' if key == 'check-id' else 'str'
|
|
|
|
return "{} has wrong type of {}: expected {} but is a {}".format(
|
2020-03-30 18:49:46 +00:00
|
|
|
self.name,
|
|
|
|
key,
|
2020-04-04 14:54:08 +00:00
|
|
|
expect_type,
|
2020-03-30 18:49:46 +00:00
|
|
|
type(wrong_value).__name__,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
ACCOUNTS = [AccountForTesting._make(t) for t in [
|
2020-05-15 20:48:45 +00:00
|
|
|
('Assets:Bank:CheckCard', PostType.CREDIT, ('check', 'invoice')),
|
2020-05-15 20:46:20 +00:00
|
|
|
('Assets:Bank:CheckCard', PostType.DEBIT, ('check-id',)),
|
|
|
|
('Assets:Cash', PostType.BOTH, ()),
|
2020-05-15 20:48:45 +00:00
|
|
|
('Assets:Checking', PostType.CREDIT, ('check', 'invoice')),
|
2020-05-15 20:46:20 +00:00
|
|
|
('Assets:Checking', PostType.DEBIT, ('check-id',)),
|
|
|
|
('Assets:Savings', PostType.BOTH, ()),
|
|
|
|
('Liabilities:CreditCard', PostType.CREDIT, ()),
|
|
|
|
('Liabilities:CreditCard', PostType.DEBIT, ('invoice',)),
|
2020-03-30 18:49:46 +00:00
|
|
|
]]
|
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
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
|
|
|
|
]
|
2020-03-30 18:49:46 +00:00
|
|
|
|
|
|
|
# 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([
|
2020-04-07 18:47:02 +00:00
|
|
|
# Only paypal-id is required for PayPal transactions
|
|
|
|
'Assets:PayPal',
|
2020-04-03 14:34:10 +00:00
|
|
|
'Assets:Prepaid:Expenses',
|
|
|
|
'Assets:Receivable:Accounts',
|
2020-04-09 19:12:04 +00:00
|
|
|
'Equity:Retained',
|
2020-03-29 02:19:49 +00:00
|
|
|
'Expenses:Other',
|
|
|
|
'Income:Other',
|
2020-04-03 14:34:10 +00:00
|
|
|
'Liabilities:Payable:Accounts',
|
|
|
|
'Liabilities:UnearnedIncome:Donations',
|
2020-03-30 18:49:46 +00:00
|
|
|
])
|
|
|
|
|
2020-04-04 14:54:08 +00:00
|
|
|
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()
|
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
def check(hook, test_acct, other_acct, expected, *,
|
2020-03-30 19:17:08 +00:00
|
|
|
txn_meta={}, post_meta={}, check_type=PostType.BOTH, min_amt=0):
|
2020-03-30 18:49:46 +00:00
|
|
|
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
|
2020-03-29 02:19:49 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope='module')
|
|
|
|
def hook():
|
|
|
|
config = testutil.TestConfig()
|
|
|
|
return meta_receipt.MetaReceipt(config)
|
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
def test_valid_receipt_on_post(hook, test_acct, other_acct, value):
|
|
|
|
check(hook, test_acct, other_acct, None, post_meta={TEST_KEY: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_STRING_METADATA_VALUES,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
def test_valid_receipt_on_txn(hook, test_acct, other_acct, value):
|
|
|
|
check(hook, test_acct, other_acct, None, txn_meta={TEST_KEY: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
|
|
|
ACCOUNTS,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_STRING_METADATA_VALUES,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
def test_invalid_fallback_on_post(hook, test_pair, other_acct, value):
|
|
|
|
test_acct, meta_key = test_pair
|
2020-03-30 18:49:46 +00:00
|
|
|
check(hook, test_acct, other_acct, {test_acct.missing_message()},
|
2020-05-15 20:46:20 +00:00
|
|
|
post_meta={meta_key: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_STRING_METADATA_VALUES,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
def test_bad_type_fallback_on_post(hook, test_pair, other_acct, value):
|
|
|
|
test_acct, meta_key = test_pair
|
2020-03-29 02:19:49 +00:00
|
|
|
expected = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
test_acct.wrong_type_message(value, meta_key),
|
2020-03-29 02:19:49 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, post_meta={meta_key: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
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})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
def test_invalid_fallback_on_txn(hook, test_pair, other_acct, value):
|
|
|
|
test_acct, meta_key = test_pair
|
2020-03-30 18:49:46 +00:00
|
|
|
check(hook, test_acct, other_acct, {test_acct.missing_message()},
|
2020-05-15 20:46:20 +00:00
|
|
|
txn_meta={meta_key: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-04 14:54:08 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.NON_STRING_METADATA_VALUES,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
def test_bad_type_fallback_on_txn(hook, test_pair, other_acct, value):
|
|
|
|
test_acct, meta_key = test_pair
|
2020-03-29 02:19:49 +00:00
|
|
|
expected = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
test_acct.wrong_type_message(value, meta_key),
|
2020-03-29 02:19:49 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, txn_meta={meta_key: value})
|
2020-03-29 02:19:49 +00:00
|
|
|
|
2020-04-04 14:54:08 +00:00
|
|
|
@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):
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, None, post_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
|
|
|
@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 = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
f"{test_acct.name} has invalid check-id: {value}",
|
2020-04-04 14:54:08 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
|
|
|
@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 = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
test_acct.wrong_type_message(value, 'check-id'),
|
2020-04-04 14:54:08 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, post_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
|
|
|
@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):
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, None, txn_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
|
|
|
@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 = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
f"{test_acct.name} has invalid check-id: {value}",
|
2020-04-04 14:54:08 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
|
|
|
@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 = {
|
2020-04-07 02:00:13 +00:00
|
|
|
test_acct.missing_message(),
|
2020-05-15 20:46:20 +00:00
|
|
|
test_acct.wrong_type_message(value, 'check-id'),
|
2020-04-04 14:54:08 +00:00
|
|
|
}
|
2020-05-15 20:46:20 +00:00
|
|
|
check(hook, test_acct, other_acct, expected, txn_meta={'check-id': value})
|
2020-04-04 14:54:08 +00:00
|
|
|
|
2020-03-30 18:49:46 +00:00
|
|
|
@pytest.mark.parametrize('test_acct,other_acct,key,value', testutil.combine_values(
|
2020-05-15 20:46:20 +00:00
|
|
|
[acct for acct in ACCOUNTS if not acct.fallback_meta],
|
2020-03-30 18:49:46 +00:00
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
2020-05-15 20:46:20 +00:00
|
|
|
{key for acct in ACCOUNTS for key in acct.fallback_meta},
|
2020-03-29 02:19:49 +00:00
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-03-30 18:49:46 +00:00
|
|
|
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})
|
2020-04-05 19:29:04 +00:00
|
|
|
|
2020-05-15 20:46:20 +00:00
|
|
|
@pytest.mark.parametrize('test_pair,other_acct,value', testutil.combine_values(
|
2020-04-05 19:29:04 +00:00
|
|
|
ACCOUNTS_WITH_LINK_FALLBACK,
|
|
|
|
NOT_REQUIRED_ACCOUNTS,
|
|
|
|
testutil.LINK_METADATA_STRINGS,
|
|
|
|
))
|
2020-05-15 20:46:20 +00:00
|
|
|
def test_fallback_on_zero_amount_postings(hook, test_pair, other_acct, value):
|
2020-04-05 19:29:04 +00:00
|
|
|
# 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.
|
2020-05-15 20:46:20 +00:00
|
|
|
test_acct, meta_key = test_pair
|
2020-04-05 19:29:04 +00:00
|
|
|
txn = testutil.Transaction(postings=[
|
|
|
|
('Income:Donations', '-.1'),
|
|
|
|
('Expenses:BankingFees', '.1'),
|
2020-05-15 20:46:20 +00:00
|
|
|
(test_acct.name, 0, {meta_key: value}),
|
2020-04-05 19:29:04 +00:00
|
|
|
])
|
|
|
|
assert not list(hook.run(txn))
|
2020-04-09 19:12:04 +00:00
|
|
|
|
2020-06-22 20:33:43 +00:00
|
|
|
@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)
|
|
|
|
|
2020-04-09 19:12:04 +00:00
|
|
|
@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)
|
2020-05-11 13:52:05 +00:00
|
|
|
|
|
|
|
@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': '!'})
|