tests: Add check_post_meta.
This makes it simple to conveniently check all posting metadata in tests.
This commit is contained in:
parent
3fbc14d377
commit
e7720b8fb8
3 changed files with 34 additions and 16 deletions
|
@ -46,7 +46,7 @@ def test_valid_values_on_postings(src_value, set_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert txn.postings[-1].meta.get(TEST_KEY) == set_value
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value', INVALID_VALUES)
|
||||
def test_invalid_values_on_postings(src_value):
|
||||
|
@ -57,6 +57,7 @@ def test_invalid_values_on_postings(src_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert errors
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: src_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
|
||||
def test_valid_values_on_transactions(src_value, set_value):
|
||||
|
@ -67,7 +68,7 @@ def test_valid_values_on_transactions(src_value, set_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert txn.postings[-1].meta.get(TEST_KEY) == set_value
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value', INVALID_VALUES)
|
||||
def test_invalid_values_on_transactions(src_value):
|
||||
|
@ -78,6 +79,7 @@ def test_invalid_values_on_transactions(src_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert errors
|
||||
testutil.check_post_meta(txn, None, None)
|
||||
|
||||
@pytest.mark.parametrize('account', [
|
||||
'Accrued:AccountsReceivable',
|
||||
|
@ -87,13 +89,15 @@ def test_invalid_values_on_transactions(src_value):
|
|||
'UnearnedIncome:Donations',
|
||||
])
|
||||
def test_non_expense_accounts_skipped(account):
|
||||
meta = {TEST_KEY: 'program'}
|
||||
txn = testutil.Transaction(postings=[
|
||||
(account, -25),
|
||||
('Expenses:General', 25, {TEST_KEY: 'program'}),
|
||||
('Expenses:General', 25, meta.copy()),
|
||||
])
|
||||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
testutil.check_post_meta(txn, None, meta)
|
||||
|
||||
@pytest.mark.parametrize('account,set_value', [
|
||||
('Expenses:Services:Accounting', 'administration'),
|
||||
|
@ -110,14 +114,14 @@ def test_default_values(account, set_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert txn.postings[-1].meta[TEST_KEY] == set_value
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
|
||||
|
||||
@pytest.mark.parametrize('date,set_value', [
|
||||
(testutil.EXTREME_FUTURE_DATE, False),
|
||||
(testutil.FUTURE_DATE, True),
|
||||
(testutil.FY_START_DATE, True),
|
||||
(testutil.FY_MID_DATE, True),
|
||||
(testutil.PAST_DATE, False),
|
||||
(testutil.EXTREME_FUTURE_DATE, None),
|
||||
(testutil.FUTURE_DATE, 'program'),
|
||||
(testutil.FY_START_DATE, 'program'),
|
||||
(testutil.FY_MID_DATE, 'program'),
|
||||
(testutil.PAST_DATE, None),
|
||||
])
|
||||
def test_default_value_set_in_date_range(date, set_value):
|
||||
txn = testutil.Transaction(date=date, postings=[
|
||||
|
@ -127,5 +131,5 @@ def test_default_value_set_in_date_range(date, set_value):
|
|||
checker = meta_expense_allocation.MetaExpenseAllocation()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
got_value = (txn.postings[-1].meta or {}).get(TEST_KEY)
|
||||
assert bool(got_value) == bool(set_value)
|
||||
expect_meta = None if set_value is None else {TEST_KEY: set_value}
|
||||
testutil.check_post_meta(txn, None, expect_meta)
|
||||
|
|
|
@ -58,7 +58,7 @@ def test_valid_values_on_postings(src_value, set_value):
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert txn.postings[-1].meta.get(TEST_KEY) == set_value
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value', INVALID_VALUES)
|
||||
def test_invalid_values_on_postings(src_value):
|
||||
|
@ -69,6 +69,7 @@ def test_invalid_values_on_postings(src_value):
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert errors
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: src_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
|
||||
def test_valid_values_on_transactions(src_value, set_value):
|
||||
|
@ -79,7 +80,7 @@ def test_valid_values_on_transactions(src_value, set_value):
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert txn.postings[-1].meta.get(TEST_KEY) == set_value
|
||||
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
|
||||
|
||||
@pytest.mark.parametrize('src_value', INVALID_VALUES)
|
||||
def test_invalid_values_on_transactions(src_value):
|
||||
|
@ -90,6 +91,7 @@ def test_invalid_values_on_transactions(src_value):
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert errors
|
||||
testutil.check_post_meta(txn, None, None)
|
||||
|
||||
@pytest.mark.parametrize('account', [
|
||||
'Accrued:AccountsPayable',
|
||||
|
@ -97,13 +99,15 @@ def test_invalid_values_on_transactions(src_value):
|
|||
'Liabilities:CreditCard',
|
||||
])
|
||||
def test_non_asset_accounts_skipped(account):
|
||||
meta = {TEST_KEY: 'USA-Corporation'}
|
||||
txn = testutil.Transaction(postings=[
|
||||
(account, 25),
|
||||
('Assets:Cash', -25, {TEST_KEY: 'USA-Corporation'}),
|
||||
('Assets:Cash', -25, meta.copy()),
|
||||
])
|
||||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
testutil.check_post_meta(txn, None, meta)
|
||||
|
||||
def test_asset_credits_skipped():
|
||||
txn = testutil.Transaction(postings=[
|
||||
|
@ -113,7 +117,7 @@ def test_asset_credits_skipped():
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert not errors
|
||||
assert not txn.postings[-1].meta
|
||||
testutil.check_post_meta(txn, None, None)
|
||||
|
||||
@pytest.mark.parametrize('date,need_value', [
|
||||
(testutil.EXTREME_FUTURE_DATE, False),
|
||||
|
@ -122,7 +126,7 @@ def test_asset_credits_skipped():
|
|||
(testutil.FY_MID_DATE, True),
|
||||
(testutil.PAST_DATE, False),
|
||||
])
|
||||
def test_default_value_set_in_date_range(date, need_value):
|
||||
def test_validation_only_in_date_range(date, need_value):
|
||||
txn = testutil.Transaction(date=date, postings=[
|
||||
('Liabilites:CreditCard', 25),
|
||||
('Assets:Cash', -25),
|
||||
|
@ -130,3 +134,4 @@ def test_default_value_set_in_date_range(date, need_value):
|
|||
checker = meta_tax_implication.MetaTaxImplication()
|
||||
errors = list(checker.run(txn))
|
||||
assert bool(errors) == bool(need_value)
|
||||
testutil.check_post_meta(txn, None, None)
|
||||
|
|
|
@ -27,6 +27,15 @@ FY_START_DATE = datetime.date(2020, 3, 1)
|
|||
FY_MID_DATE = datetime.date(2020, 9, 1)
|
||||
PAST_DATE = datetime.date(2000, 1, 1)
|
||||
|
||||
def check_post_meta(txn, *expected_meta, default=None):
|
||||
assert len(txn.postings) == len(expected_meta)
|
||||
for post, expected in zip(txn.postings, expected_meta):
|
||||
if not expected:
|
||||
assert not post.meta
|
||||
else:
|
||||
assert all(post.meta.get(key, default) == value
|
||||
for key, value in expected.items())
|
||||
|
||||
def parse_date(s, fmt='%Y-%m-%d'):
|
||||
return datetime.datetime.strptime(s, fmt).date()
|
||||
|
||||
|
|
Loading…
Reference in a new issue