diff --git a/tests/test_templates.py b/tests/test_templates.py index b0a1740d3db2a2cd6fd6419afbcef39c113f060c..5cc03dc87cfeea2c3b1ad4d918c8e353c2070394 100644 --- a/tests/test_templates.py +++ b/tests/test_templates.py @@ -1,11 +1,14 @@ import collections import configparser +import contextlib import datetime import decimal +import io import pathlib import pytest from import2ledger import errors, template +from import2ledger.hooks import ledger_entry from . import DATA_DIR, normalize_whitespace @@ -199,3 +202,48 @@ def test_line1_not_custom_payee(): def test_bad_amount_expression(amount_expr): with pytest.raises(errors.UserInputError): template.Template(" Income " + amount_expr) + +class Config: + def __init__(self): + self.stdout = io.StringIO() + + @contextlib.contextmanager + def open_output_file(self): + yield self.stdout + + def get_template(self, key): + try: + return template_from(key) + except KeyError: + raise errors.UserInputConfigurationError( + "template not defined in test config", key) + + +def run_hook(entry_data): + hook_config = Config() + hook = ledger_entry.LedgerEntryHook(hook_config) + assert hook.run(entry_data) is None + stdout = hook_config.stdout.getvalue() + return normalize_whitespace(stdout).splitlines() + +def hook_vars(template_key, payee, amount): + return template_vars(payee, amount, other_vars={'template': template_key}) + +def test_hook_renders_template(): + entry_data = hook_vars('Simplest', 'BB', '0.99') + lines = run_hook(entry_data) + assert lines == [ + "", + "2015/03/14 BB", + " Accrued:Accounts Receivable 0.99 USD", + " Income:Donations -0.99 USD", + ] + +def test_hook_handles_empty_template(): + entry_data = hook_vars('Empty', 'CC', 1) + assert not run_hook(entry_data) + +def test_hook_handles_template_undefined(): + entry_data = hook_vars('Nonexistent', 'DD', 1) + assert not run_hook(entry_data) +