File diff d51c41490d1f → 73cd942a4606
tests/test_hooks_ledger_entry.py
Show inline comments
 
import collections
 
import configparser
 
import contextlib
 
import datetime
 
import decimal
...
 
@@ -7,19 +6,31 @@ import io
 
import pathlib
 

	
 
import pytest
 
import yaml
 
from import2ledger import errors
 
from import2ledger.hooks import ledger_entry
 

	
 
from . import DATA_DIR, normalize_whitespace
 
from . import DATA_DIR, normalize_whitespace, Config as BaseConfig
 

	
 
DATE = datetime.date(2015, 3, 14)
 
with pathlib.Path(DATA_DIR, 'templates.yml').open() as conffile:
 
    _config_dict = yaml.load(conffile)
 

	
 
class Config(BaseConfig):
 
    def __init__(self, options_dict=_config_dict):
 
        super().__init__(options_dict)
 
        self.stdout = io.StringIO()
 

	
 
config = configparser.ConfigParser(comment_prefixes='#')
 
with pathlib.Path(DATA_DIR, 'templates.ini').open() as conffile:
 
    config.read_file(conffile)
 
    @contextlib.contextmanager
 
    def open_output_file(self):
 
        yield self.stdout
 

	
 

	
 
DATE = datetime.date(2015, 3, 14)
 

	
 
def template_from(section_name, *args, **kwargs):
 
    return ledger_entry.Template(config[section_name]['template'], *args, **kwargs)
 
def template_from(template_name, *args, **kwargs):
 
    section = Config().get_section('Ledger output')
 
    template_s = section['{} ledger entry'.format(template_name)]
 
    return ledger_entry.Template(template_s, *args, **kwargs)
 

	
 
def template_vars(payee, amount, currency='USD', date=DATE, other_vars=None):
 
    call_vars = {
...
 
@@ -27,7 +38,6 @@ def template_vars(payee, amount, currency='USD', date=DATE, other_vars=None):
 
        'currency': currency,
 
        'date': date,
 
        'payee': payee,
 
        'ledger template': 'template',
 
    }
 
    if other_vars is None:
 
        return call_vars
...
 
@@ -204,24 +214,12 @@ def test_bad_amount_expression(amount_expr):
 
    with pytest.raises(errors.UserInputError):
 
        ledger_entry.Template(" Income  " + amount_expr)
 

	
 
class Config:
 
    def __init__(self, use_section):
 
        self.section_name = use_section
 
        self.stdout = io.StringIO()
 

	
 
    @contextlib.contextmanager
 
    def open_output_file(self):
 
        yield self.stdout
 

	
 
    def get_section(self, name=None):
 
        return config[self.section_name]
 

	
 

	
 
def run_hook(entry_data, config_section):
 
    hook_config = Config(config_section)
 
    hook = ledger_entry.LedgerEntryHook(hook_config)
 
def run_hook(entry_data, template_name):
 
    config = Config()
 
    entry_data['ledger template'] = '{} ledger entry'.format(template_name)
 
    hook = ledger_entry.LedgerEntryHook(config)
 
    assert hook.run(entry_data) is None
 
    stdout = hook_config.stdout.getvalue()
 
    stdout = config.stdout.getvalue()
 
    return normalize_whitespace(stdout).splitlines()
 

	
 
def test_hook_renders_template():
...
 
@@ -242,3 +240,7 @@ def test_hook_handles_template_undefined():
 
    entry_data = template_vars('DD', 1)
 
    assert not run_hook(entry_data, 'Nonexistent')
 

	
 
def test_unconfigured():
 
    config = Config({})
 
    with pytest.raises(errors.NotConfiguredError):
 
        ledger_entry.LedgerEntryHook(config)