From 501bd251cbf34142b260504de67d2e4ec8606481 2020-03-19 19:04:53 From: Brett Smith Date: 2020-03-19 19:04:53 Subject: [PATCH] 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. --- diff --git a/tests/test_meta_expense_allocation.py b/tests/test_meta_expense_allocation.py index 623dc9668a6d31817a90fc88bf8c2d2949e2dad4..408f4b7973d6b074b628ebbecbe3455e17516440 100644 --- a/tests/test_meta_expense_allocation.py +++ b/tests/test_meta_expense_allocation.py @@ -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) diff --git a/tests/test_meta_income_type.py b/tests/test_meta_income_type.py index dc31011847a2e3e5484d1761786566022bb48afe..7003758ad83b5ddf3f2ecb1bef8409e4b3dfb52b 100644 --- a/tests/test_meta_income_type.py +++ b/tests/test_meta_income_type.py @@ -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) diff --git a/tests/test_meta_tax_implication.py b/tests/test_meta_tax_implication.py index d62658a9f79ff8bb52c4c1e64b0435dba2fc4099..c01054ca86e0858a4ef3361db64d24bcf05c263b 100644 --- a/tests/test_meta_tax_implication.py +++ b/tests/test_meta_tax_implication.py @@ -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)