Files
@ 21f6d078a285
Branch filter:
Location: NPO-Accounting/import2ledger/tests/test_config.py
21f6d078a285
3.7 KiB
text/x-python
outfile: New module to take over functionality from config.
With the concept of the general "output path" going away, it doesn't make
sense for Config to have an open_output_file method.
With the concept of the general "output path" going away, it doesn't make
sense for Config to have an open_output_file method.
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 | import contextlib
import datetime
import itertools
import logging
import os
import pathlib
from unittest import mock
import pytest
from import2ledger import config, errors, strparse
from . import DATA_DIR, START_DATE
def config_from_file(path, arglist=[], stdout=None, stderr=None, *, init=True):
path = pathlib.Path(path)
if not path.is_absolute():
path = DATA_DIR / path
arglist = ['-C', path.as_posix(), *arglist, os.devnull]
retval = config.Configuration(stdout, stderr)
if init:
retval.read_args(arglist)
return retval
def test_get_section():
config = config_from_file('test_config.ini')
section = config.get_section('Templates')
assert section['output_path'] == 'Template.output'
assert section['signed_currencies'] == 'EUR'
def test_get_section_falls_back_to_default():
config = config_from_file('test_config.ini')
section = config.get_section('Nonexistent Templates Section')
assert section.get('output_path') != 'Template.output'
assert section['signed_currencies'] == 'EUR'
def _fix_date_s(date_s, new_sep, old_sep='/'):
return date_s.replace(old_sep, new_sep)
@pytest.mark.parametrize('range_s,sep', itertools.product([
'-',
'2016/06/01-2016/06/30',
'2016/06/01-',
'-2016/06/30',
], '/-'))
def test_date_range(range_s, sep):
raw_start, _, raw_end = range_s.partition('-')
expect_start = _fix_date_s(raw_start, sep)
expect_end = _fix_date_s(raw_end, sep)
config = config_from_file(os.devnull, [
'--date-range=' + _fix_date_s(range_s, sep),
'--date-format', _fix_date_s('%Y/%m/%d', sep),
])
section = config.get_section('Dates')
actual_start = section.get('import start date')
assert actual_start == (expect_start or None)
actual_end = section.get('import end date')
assert actual_end == (expect_end or None)
@pytest.mark.parametrize('config_filename,date_fmt,date_arg,expect_date', [
(os.devnull, '%Y-%m-%d', '2017-01-05', None),
(os.devnull, '%Y/%m/%d', '2017/01/06', None),
('test_config.ini', '%Y-%m-%d', None, '2017|10|08'),
('test_config.ini', '%Y-%m-%d', '2017-01-12', '2017|01|12'),
])
def test_default_date(config_filename, date_fmt, date_arg, expect_date):
arglist = ['--date-format', date_fmt]
if date_arg is not None:
arglist += ['--default-date', date_arg]
config = config_from_file(config_filename, arglist)
date_section = config.get_section('Dates')
actual_date = date_section['default date']
if expect_date is not None:
assert actual_date == expect_date
else:
default_date = strparse.date(actual_date, date_section['date format'])
if date_arg is not None:
target_date = strparse.date(date_arg, date_fmt)
assert default_date == target_date
else:
assert START_DATE <= default_date <= datetime.date.today()
@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'
config = config_from_file(os.devnull, init=False)
with bad_config(date_s):
config.read_args(['--date-format', '%Y%m%d', '--default-date', date_s, os.devnull])
def test_bad_loglevel():
with bad_config('wraning'):
config = config_from_file('test_config.ini', ['-c', 'Bad Loglevel'])
config.get_loglevel()
|