@@ -39,25 +39,64 @@ NON_PAYMENT_ACCOUNTS = {
'UnearnedIncome:MatchPledges',
}
AMOUNTS = [
None,
'-25.50',
0,
'25.75',
]
def Posting(account, number,
currency='USD', cost=None, price=None, flag=None,
**meta):
if not meta:
meta = None
if number is not None:
number = Decimal(number)
return data.Posting(
data.Account(account),
bc_amount.Amount(Decimal(number), currency),
bc_amount.Amount(number, currency),
cost,
price,
flag,
meta,
)
def check_all_thresholds(post, threshold, expected):
assert post.is_payment(threshold) is expected
assert post.is_payment(-threshold) is expected
assert post.is_payment(Decimal(threshold)) is expected
assert post.is_payment(Decimal(-threshold)) is expected
def check_all_thresholds(expected, method, threshold, *args):
assert method(threshold, *args) is expected
assert method(Decimal(threshold), *args) is expected
@pytest.mark.parametrize('amount', AMOUNTS)
def test_is_credit(amount):
expected = None if amount is None else float(amount) > 0
assert Posting('Assets:Cash', amount).is_credit() is expected
def test_is_credit_threshold():
post = Posting('Assets:Cash', 25)
check_all_thresholds(True, post.is_credit, 0)
check_all_thresholds(True, post.is_credit, 20)
check_all_thresholds(False, post.is_credit, 40)
def test_is_credit_default():
post = Posting('Assets:Cash', None)
assert post.is_credit(default=True) is True
assert post.is_credit(default=False) is False
def test_is_debit(amount):
expected = None if amount is None else float(amount) < 0
assert Posting('Assets:Cash', amount).is_debit() is expected
def test_is_debit_threshold():
post = Posting('Assets:Cash', -25)
check_all_thresholds(True, post.is_debit, 0)
check_all_thresholds(True, post.is_debit, 20)
check_all_thresholds(False, post.is_debit, 40)
def test_is_debit_default():
assert post.is_debit(default=True) is True
assert post.is_debit(default=False) is False
@pytest.mark.parametrize('acct', PAYMENT_ACCOUNTS)
def test_is_payment(acct):
@@ -71,24 +110,24 @@ def test_is_payment(acct):
def test_is_not_payment_account(acct, amount, threshold):
post = Posting(acct, -amount)
assert not post.is_payment()
check_all_thresholds(post, threshold, False)
check_all_thresholds(False, post.is_payment, threshold)
def test_is_payment_with_threshold(acct):
threshold = len(acct) * 10
post = Posting(acct, -500)
check_all_thresholds(post, threshold, True)
check_all_thresholds(True, post.is_payment, threshold)
def test_is_not_payment_by_threshold(acct):
post = Posting(acct, -9)
def test_is_not_payment_but_credit(acct):
post = Posting(acct, 9)
check_all_thresholds(post, 0, False)
check_all_thresholds(post, 5, False)
check_all_thresholds(post, 10, False)
check_all_thresholds(False, post.is_payment, 0)
check_all_thresholds(False, post.is_payment, 5)
check_all_thresholds(False, post.is_payment, 10)