diff --git a/tests/test_reports_accrual.py b/tests/test_reports_accrual.py index f0f2e73..0e661f7 100644 --- a/tests/test_reports_accrual.py +++ b/tests/test_reports_accrual.py @@ -93,6 +93,13 @@ def check_link_regexp(regexp, match_s, first_link_only=False): else: assert end_match +def relate_accruals_by_meta(postings, value, key='invoice'): + return core.RelatedPostings( + post for post in postings + if post.meta.get(key) == value + and post.account.is_under('Assets:Receivable', 'Liabilities:Payable') + ) + @pytest.mark.parametrize('link_fmt', [ '{}', 'rt:{}', @@ -246,6 +253,17 @@ def check_output(output, expect_patterns): output.seek(0) testutil.check_lines_match(iter(output), expect_patterns) +def run_outgoing(invoice, postings, rt_client=None): + if rt_client is None: + rt_client = RTClient() + if not isinstance(postings, core.RelatedPostings): + postings = relate_accruals_by_meta(postings, invoice) + output = io.StringIO() + errors = io.StringIO() + rt_cache = rtutil.RT(rt_client) + accrual.outgoing_report({invoice: postings}, output, errors, rt_client, rt_cache) + return output, errors + @pytest.mark.parametrize('invoice,expected', [ ('rt:505/5050', "Zero balance outstanding since 2020-05-05"), ('rt:510/5100', "Zero balance outstanding since 2020-05-10"), @@ -253,29 +271,15 @@ def check_output(output, expect_patterns): ('rt://ticket/515/attachments/5150', "1,500.00 USD outstanding since 2020-05-15",), ]) def test_balance_report(accrual_postings, invoice, expected): - related = core.RelatedPostings( - post for post in accrual_postings - if post.meta.get('invoice') == invoice - and post.account.is_under('Assets:Receivable', 'Liabilities:Payable') - ) + related = relate_accruals_by_meta(accrual_postings, invoice) output = io.StringIO() accrual.balance_report({invoice: related}, output) check_output(output, [invoice, expected]) def test_outgoing_report(accrual_postings): invoice = 'rt:510/6100' - related = core.RelatedPostings( - post for post in accrual_postings - if post.meta.get('invoice') == invoice - and post.account.is_under('Assets:Receivable', 'Liabilities:Payable') - ) - output = io.StringIO() - errors = io.StringIO() - rt_client = RTClient() - rt_cache = rtutil.RT(rt_client) - accrual.outgoing_report({invoice: related}, output, errors, rt_client, rt_cache) - assert not errors.getvalue() - rt_url = rt_client.DEFAULT_URL[:-9] + output, errors = run_outgoing(invoice, accrual_postings) + rt_url = RTClient.DEFAULT_URL[:-9] rt_id_url = rf'\b{re.escape(f"{rt_url}Ticket/Display.html?id=510")}\b' contract_url = rf'\b{re.escape(f"{rt_url}Ticket/Attachment/4000/4000/contract.pdf")}\b' print(output.getvalue()) @@ -299,16 +303,8 @@ def test_outgoing_report(accrual_postings): def test_outgoing_report_custom_field_fallbacks(accrual_postings): invoice = 'rt:510/6100' - related = core.RelatedPostings( - post for post in accrual_postings - if post.meta.get('invoice') == invoice - and post.account.is_under('Assets:Receivable', 'Liabilities:Payable') - ) - output = io.StringIO() - errors = io.StringIO() rt_client = RTClient(want_cfs=False) - rt_cache = rtutil.RT(rt_client) - accrual.outgoing_report({invoice: related}, output, errors, rt_client, rt_cache) + output, errors = run_outgoing(invoice, accrual_postings, rt_client) assert not errors.getvalue() check_output(output, [ r'^PAYMENT FOR APPROVAL:$', @@ -319,16 +315,7 @@ def test_outgoing_report_custom_field_fallbacks(accrual_postings): def test_outgoing_report_fx_amounts(accrual_postings): invoice = 'rt:520/5200' - related = core.RelatedPostings( - post for post in accrual_postings - if post.meta.get('invoice') == invoice - and post.account.is_under('Assets:Receivable', 'Liabilities:Payable') - ) - output = io.StringIO() - errors = io.StringIO() - rt_client = RTClient() - rt_cache = rtutil.RT(rt_client) - accrual.outgoing_report({invoice: related}, output, errors, rt_client, rt_cache) + output, errors = run_outgoing(invoice, accrual_postings) assert not errors.getvalue() check_output(output, [ r'^PAYMENT FOR APPROVAL:$',