Files
@ f888b13c56b0
Branch filter:
Location: NPO-Accounting/import2ledger/tests/test_config.py
f888b13c56b0
3.7 KiB
text/x-python
hooks.add_entity: Better handle common name prefix parts.
Keep these with the name they're attached to, rather than breaking the name
in the middle.
Keep these with the name they're attached to, rather than breaking the name
in the middle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import contextlib
import datetime
import logging
import os
import pathlib
START_DATE = datetime.date.today()
from unittest import mock
import pytest
from import2ledger import config, errors
from . import DATA_DIR
def config_from_file(path, arglist=[]):
path = pathlib.Path(path)
if not path.is_absolute():
path = DATA_DIR / path
arglist = ['-C', path.as_posix(), *arglist, os.devnull]
return config.Configuration(arglist)
def test_defaults():
config = config_from_file('test_config.ini', ['--sign', 'GBP', '-O', 'out_arg'])
factory = mock.Mock(name='Template')
template = config.get_template('one', 'Templates', factory)
assert factory.called
kwargs = factory.call_args[1]
assert list(kwargs.pop('signed_currencies', '')) == ['GBP']
assert kwargs == {
'date_fmt': '%Y-%m-%d',
'signed_currency_fmt': kwargs['signed_currency_fmt'],
'unsigned_currency_fmt': kwargs['unsigned_currency_fmt'],
}
def test_template_parsing():
config = config_from_file('test_config.ini')
factory = mock.Mock(name='Template')
template = config.get_template('two', 'Templates', factory)
try:
tmpl_s = factory.call_args[0][0]
except IndexError as error:
assert False, error
assert "\n;Tag1: {value}\n" in tmpl_s
assert "\nIncome:Donations -100%\n" in tmpl_s
assert "\n;IncomeTag: Donations\n" in tmpl_s
@pytest.mark.parametrize('arg_s', [None, '-', 'output.ledger'])
def test_output_path(arg_s):
arglist = [] if arg_s is None else ['-O', arg_s]
config = config_from_file(os.devnull, arglist)
output_path = config.get_output_path()
if (arg_s is None) or (arg_s == '-'):
assert output_path is None
else:
assert output_path == pathlib.Path(arg_s)
def test_output_path_from_section():
expected_path = pathlib.Path('Template.output')
config = config_from_file('test_config.ini', ['-O', 'output.ledger'])
assert config.get_output_path('Templates') == expected_path
assert config.get_output_path() != expected_path
with config.from_section('Templates'):
assert config.get_output_path() == expected_path
@pytest.mark.parametrize('arglist,expect_date', [
([], None),
(['-d', '2017-10-12'], datetime.date(2017, 10, 12)),
(['-c', 'Date'], datetime.date(2017, 10, 8)),
])
def test_default_date(arglist, expect_date):
config = config_from_file('test_config.ini', arglist)
default_date = config.get_default_date()
if expect_date is None:
assert START_DATE <= default_date <= datetime.date.today()
else:
assert default_date == expect_date
assert config.get_default_date('Date') == datetime.date(2017, 10, 8)
@pytest.mark.parametrize('level_s,expect_level', [
(s, getattr(logging, s.upper()))
for s in ['critical', 'debug', 'error', 'info', 'warning']
])
def test_loglevel(level_s, expect_level):
config = config_from_file(os.devnull, ['--loglevel', level_s])
assert config.get_loglevel() == expect_level
@contextlib.contextmanager
def bad_config(expect_input):
with pytest.raises(errors.UserInputConfigurationError) as exc_info:
yield exc_info
assert exc_info.value.user_input == expect_input
def test_bad_default_date():
date_s = '2017-10-06'
with bad_config(date_s):
config = config_from_file(os.devnull, ['--date', date_s])
config.get_default_date()
def test_bad_loglevel():
with bad_config('wraning'):
config = config_from_file('test_config.ini', ['-c', 'Bad Loglevel'])
config.get_loglevel()
def test_undefined_template():
template_name = 'template nonexistent'
config = config_from_file(os.devnull)
with bad_config(template_name):
config.get_template(template_name)
|