tests: Turn tested hooks into fixtures.

This is in preparation for passing configuration to hooks.
That'll be a big change already, so I wanted this to be a
boring diff first.
This commit is contained in:
Brett Smith 2020-03-19 15:04:53 -04:00
parent 71531913d5
commit 501bd251cb
3 changed files with 58 additions and 69 deletions

View file

@ -37,47 +37,47 @@ INVALID_VALUES = {
TEST_KEY = 'expense-allocation'
@pytest.fixture(scope='module')
def hook():
return meta_expense_allocation.MetaExpenseAllocation()
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
def test_valid_values_on_postings(src_value, set_value):
def test_valid_values_on_postings(hook, src_value, set_value):
txn = testutil.Transaction(postings=[
('Assets:Cash', -25),
('Expenses:General', 25, {TEST_KEY: src_value}),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_postings(hook, src_value):
txn = testutil.Transaction(postings=[
('Assets:Cash', -25),
('Expenses:General', 25, {TEST_KEY: src_value}),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.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):
def test_valid_values_on_transactions(hook, src_value, set_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Assets:Cash', -25),
('Expenses:General', 25),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_transactions(hook, src_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Assets:Cash', -25),
('Expenses:General', 25),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert errors
testutil.check_post_meta(txn, None, None)
@ -88,14 +88,13 @@ def test_invalid_values_on_transactions(src_value):
'Liabilities:CreditCard',
'UnearnedIncome:Donations',
])
def test_non_expense_accounts_skipped(account):
def test_non_expense_accounts_skipped(hook, account):
meta = {TEST_KEY: 'program'}
txn = testutil.Transaction(postings=[
(account, -25),
('Expenses:General', 25, meta.copy()),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, meta)
@ -106,13 +105,12 @@ def test_non_expense_accounts_skipped(account):
('Expenses:Services:Development', 'program'),
('Expenses:Services:Fundraising', 'fundraising'),
])
def test_default_values(account, set_value):
def test_default_values(hook, account, set_value):
txn = testutil.Transaction(postings=[
('Liabilites:CreditCard', -25),
(account, 25),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
@ -123,13 +121,12 @@ def test_default_values(account, set_value):
(testutil.FY_MID_DATE, 'program'),
(testutil.PAST_DATE, None),
])
def test_default_value_set_in_date_range(date, set_value):
def test_default_value_set_in_date_range(hook, date, set_value):
txn = testutil.Transaction(date=date, postings=[
('Liabilites:CreditCard', -25),
('Expenses:General', 25),
])
checker = meta_expense_allocation.MetaExpenseAllocation()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
expect_meta = None if set_value is None else {TEST_KEY: set_value}
testutil.check_post_meta(txn, None, expect_meta)

View file

@ -37,47 +37,47 @@ INVALID_VALUES = {
TEST_KEY = 'income-type'
@pytest.fixture(scope='module')
def hook():
return meta_income_type.MetaIncomeType()
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
def test_valid_values_on_postings(src_value, set_value):
def test_valid_values_on_postings(hook, src_value, set_value):
txn = testutil.Transaction(postings=[
('Assets:Cash', 25),
('Income:Other', -25, {TEST_KEY: src_value}),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_postings(hook, src_value):
txn = testutil.Transaction(postings=[
('Assets:Cash', 25),
('Income:Other', -25, {TEST_KEY: src_value}),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.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):
def test_valid_values_on_transactions(hook, src_value, set_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Assets:Cash', 25),
('Income:Other', -25),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_transactions(hook, src_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Assets:Cash', 25),
('Income:Other', -25),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert errors
testutil.check_post_meta(txn, None, None)
@ -87,14 +87,13 @@ def test_invalid_values_on_transactions(src_value):
'Expenses:General',
'Liabilities:CreditCard',
])
def test_non_income_accounts_skipped(account):
def test_non_income_accounts_skipped(hook, account):
meta = {TEST_KEY: 'RBI'}
txn = testutil.Transaction(postings=[
(account, 25),
('Income:Other', -25, meta.copy()),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, meta)
@ -110,26 +109,24 @@ def test_non_income_accounts_skipped(account):
('UnearnedIncome:Conferences:Registrations', 'RBI'),
('UnearnedIncome:MatchPledges', 'Donations'),
])
def test_default_values(account, set_value):
def test_default_values(hook, account, set_value):
txn = testutil.Transaction(postings=[
('Assets:Cash', 25),
(account, -25),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, {TEST_KEY: set_value})
@pytest.mark.parametrize('account', [
'Income:Other',
])
def test_no_default_value(account):
def test_no_default_value(hook, account):
txn = testutil.Transaction(postings=[
('Assets:Cash', 25),
(account, -25),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert errors
testutil.check_post_meta(txn, None, None)
@ -140,13 +137,12 @@ def test_no_default_value(account):
(testutil.FY_MID_DATE, 'Donations'),
(testutil.PAST_DATE, None),
])
def test_default_value_set_in_date_range(date, set_value):
def test_default_value_set_in_date_range(hook, date, set_value):
txn = testutil.Transaction(date=date, postings=[
('Assets:Cash', 25),
('Income:Donations', -25),
])
checker = meta_income_type.MetaIncomeType()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
expect_meta = None if set_value is None else {TEST_KEY: set_value}
testutil.check_post_meta(txn, None, expect_meta)

View file

@ -49,47 +49,47 @@ INVALID_VALUES = {
TEST_KEY = 'tax-implication'
@pytest.fixture(scope='module')
def hook():
return meta_tax_implication.MetaTaxImplication()
@pytest.mark.parametrize('src_value,set_value', VALID_VALUES.items())
def test_valid_values_on_postings(src_value, set_value):
def test_valid_values_on_postings(hook, src_value, set_value):
txn = testutil.Transaction(postings=[
('Accrued:AccountsPayable', 25),
('Assets:Cash', -25, {TEST_KEY: src_value}),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_postings(hook, src_value):
txn = testutil.Transaction(postings=[
('Accrued:AccountsPayable', 25),
('Assets:Cash', -25, {TEST_KEY: src_value}),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.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):
def test_valid_values_on_transactions(hook, src_value, set_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Accrued:AccountsPayable', 25),
('Assets:Cash', -25),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
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):
def test_invalid_values_on_transactions(hook, src_value):
txn = testutil.Transaction(**{TEST_KEY: src_value}, postings=[
('Accrued:AccountsPayable', 25),
('Assets:Cash', -25),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert errors
testutil.check_post_meta(txn, None, None)
@ -98,34 +98,31 @@ def test_invalid_values_on_transactions(src_value):
'Expenses:General',
'Liabilities:CreditCard',
])
def test_non_asset_accounts_skipped(account):
def test_non_asset_accounts_skipped(hook, account):
meta = {TEST_KEY: 'USA-Corporation'}
txn = testutil.Transaction(postings=[
(account, 25),
('Assets:Cash', -25, meta.copy()),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, meta)
def test_prepaid_expenses_skipped():
def test_prepaid_expenses_skipped(hook, ):
txn = testutil.Transaction(postings=[
('Expenses:General', 25),
('Assets:PrepaidExpenses', -25),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, None)
def test_asset_credits_skipped():
def test_asset_credits_skipped(hook, ):
txn = testutil.Transaction(postings=[
('Income:Donations', -25),
('Assets:Cash', 25),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert not errors
testutil.check_post_meta(txn, None, None)
@ -136,12 +133,11 @@ def test_asset_credits_skipped():
(testutil.FY_MID_DATE, True),
(testutil.PAST_DATE, False),
])
def test_validation_only_in_date_range(date, need_value):
def test_validation_only_in_date_range(hook, date, need_value):
txn = testutil.Transaction(date=date, postings=[
('Liabilites:CreditCard', 25),
('Assets:Cash', -25),
])
checker = meta_tax_implication.MetaTaxImplication()
errors = list(checker.run(txn))
errors = list(hook.run(txn))
assert bool(errors) == bool(need_value)
testutil.check_post_meta(txn, None, None)