tests: Prepare test_meta_receipt to test more credits and fallbacks.
I'm getting to ready to extend this hook to deal with income receipts as well as expense receipts. These changes let me write the tests in a more declarative style, so I don't have to duplicate them all again to test credits as well as debits. Note that we're only testing debits right now, just like the existing tests, because the default check_type for check() is PostType.DEBIT. Part of making the changes will be changing that to PostType.BOTH.
This commit is contained in:
parent
959bda307b
commit
8a2721ec0f
1 changed files with 160 additions and 166 deletions
|
@ -14,235 +14,229 @@
|
|||
# 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
|
||||
|
||||
REQUIRED_ACCOUNTS = {
|
||||
'Assets:Cash',
|
||||
'Assets:Savings',
|
||||
'Liabilities:CreditCard',
|
||||
}
|
||||
TEST_KEY = 'receipt'
|
||||
|
||||
CHECKING_ACCOUNTS = {
|
||||
'Assets:Checking',
|
||||
'Assets:CheckCard',
|
||||
}
|
||||
class PostType(enum.IntFlag):
|
||||
NONE = 0
|
||||
CREDIT = 1
|
||||
DEBIT = 2
|
||||
BOTH = CREDIT | DEBIT
|
||||
|
||||
TESTABLE_ACCOUNTS = REQUIRED_ACCOUNTS | CHECKING_ACCOUNTS
|
||||
|
||||
NON_REQUIRED_ACCOUNTS = {
|
||||
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:Cash', PostType.BOTH, None),
|
||||
('Assets:Checking', PostType.BOTH, 'check'),
|
||||
('Assets:CheckCard', PostType.BOTH, 'check'),
|
||||
('Assets:Savings', PostType.BOTH, None),
|
||||
]]
|
||||
|
||||
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',
|
||||
}
|
||||
])
|
||||
|
||||
TEST_KEY = 'receipt'
|
||||
def check(hook, test_acct, other_acct, expected, *,
|
||||
txn_meta={}, post_meta={}, check_type=PostType.DEBIT, 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('acct1,acct2,value', testutil.combine_values(
|
||||
TESTABLE_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_valid_values_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {TEST_KEY: value}),
|
||||
])
|
||||
assert not list(hook.run(txn))
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
REQUIRED_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_invalid_values_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {TEST_KEY: value}),
|
||||
])
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == {"{} missing {}".format(acct1, TEST_KEY)}
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
TESTABLE_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_STRING_METADATA_VALUES,
|
||||
))
|
||||
def test_bad_type_values_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {TEST_KEY: value}),
|
||||
])
|
||||
expected_msg = "{} has wrong type of {}: expected str but is a {}".format(
|
||||
acct1,
|
||||
TEST_KEY,
|
||||
type(value).__name__,
|
||||
)
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert expected_msg in actual
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
TESTABLE_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_valid_values_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(**{TEST_KEY: value}, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
assert not list(hook.run(txn))
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
REQUIRED_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_invalid_values_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(**{TEST_KEY: value}, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == {"{} missing {}".format(acct1, TEST_KEY)}
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
TESTABLE_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_STRING_METADATA_VALUES,
|
||||
))
|
||||
def test_bad_type_values_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(**{TEST_KEY: value}, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
expected_msg = "{} has wrong type of {}: expected str but is a {}".format(
|
||||
acct1,
|
||||
TEST_KEY,
|
||||
type(value).__name__,
|
||||
)
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert expected_msg in actual
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_check_fallback_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {'check': value}),
|
||||
])
|
||||
assert not list(hook.run(txn))
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_bad_check_fallback_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {'check': value}),
|
||||
])
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == {"{} missing {} or check".format(acct1, TEST_KEY)}
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_STRING_METADATA_VALUES,
|
||||
))
|
||||
def test_check_fallback_bad_type_on_postings(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {'check': value}),
|
||||
])
|
||||
def test_bad_type_fallback_on_post(hook, test_acct, other_acct, value):
|
||||
expected = {
|
||||
"{} missing {}".format(acct1, TEST_KEY),
|
||||
"{} has wrong type of check: expected str but is a {}".format(
|
||||
acct1, type(value).__name__,
|
||||
),
|
||||
test_acct.missing_message(False),
|
||||
test_acct.wrong_type_message(value, test_acct.fallback_meta),
|
||||
}
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == expected
|
||||
check(hook, test_acct, other_acct, expected,
|
||||
post_meta={test_acct.fallback_meta: value})
|
||||
|
||||
@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_check_fallback_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(check=value, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
assert not list(hook.run(txn))
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_LINK_METADATA_STRINGS,
|
||||
))
|
||||
def test_bad_check_fallback_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(check=value, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == {"{} missing {} or check".format(acct1, TEST_KEY)}
|
||||
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('acct1,acct2,value', testutil.combine_values(
|
||||
CHECKING_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct,value', testutil.combine_values(
|
||||
ACCOUNTS_WITH_FALLBACKS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
testutil.NON_STRING_METADATA_VALUES,
|
||||
))
|
||||
def test_check_fallback_bad_type_on_transaction(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(check=value, postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25),
|
||||
])
|
||||
def test_bad_type_fallback_on_txn(hook, test_acct, other_acct, value):
|
||||
expected = {
|
||||
"{} missing {}".format(acct1, TEST_KEY),
|
||||
"{} has wrong type of check: expected str but is a {}".format(
|
||||
acct1, type(value).__name__,
|
||||
),
|
||||
test_acct.missing_message(False),
|
||||
test_acct.wrong_type_message(value, test_acct.fallback_meta),
|
||||
}
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == expected
|
||||
check(hook, test_acct, other_acct, expected,
|
||||
txn_meta={test_acct.fallback_meta: value})
|
||||
|
||||
@pytest.mark.parametrize('acct1,acct2,value', testutil.combine_values(
|
||||
REQUIRED_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@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_check_fallback_not_accepted_on_other_accounts(hook, acct1, acct2, value):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, 25),
|
||||
(acct1, -25, {'check': value}),
|
||||
])
|
||||
actual = {error.message for error in hook.run(txn)}
|
||||
assert actual == {"{} missing {}".format(acct1, TEST_KEY)}
|
||||
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('acct1,acct2', testutil.combine_values(
|
||||
TESTABLE_ACCOUNTS,
|
||||
NON_REQUIRED_ACCOUNTS,
|
||||
@pytest.mark.parametrize('test_acct,other_acct', testutil.combine_values(
|
||||
ACCOUNTS,
|
||||
NOT_REQUIRED_ACCOUNTS,
|
||||
))
|
||||
def test_no_value_required_for_credits(hook, acct1, acct2):
|
||||
txn = testutil.Transaction(postings=[
|
||||
(acct2, -25),
|
||||
(acct1, 25),
|
||||
])
|
||||
assert not list(hook.run(txn))
|
||||
def test_no_value_required_for_credits(hook, test_acct, other_acct):
|
||||
check(hook, test_acct, other_acct, None, check_type=PostType.CREDIT)
|
||||
|
|
Loading…
Reference in a new issue