Skip to content

Commit

Permalink
test for ini_read
Browse files Browse the repository at this point in the history
  • Loading branch information
nihlaeth committed Feb 14, 2017
1 parent ed80e9b commit d3814f7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
51 changes: 51 additions & 0 deletions tests/test_ini.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Test ini backend."""
from collections import OrderedDict
from pathlib import Path
import pytest

from user_config import (
Expand Down Expand Up @@ -37,3 +38,53 @@ def test_validate():
with pytest.raises(InvalidConfigTree):
ini_validate(None, OrderedDict(
nested_section=Section(another_section=Section())))

def test_read():
config_directory = Path(__file__).parents[0] / 'test_read'
config_tree = OrderedDict(
section_one=Section(
string=StringOption(),
multiline_string=StringOption(),
integer=IntegerOption(),
float=FloatOption(),
boolean=BooleanOption()),
section_two=Section(
empty_string=StringOption(),
empty_integer=IntegerOption(),
empty_float=FloatOption(),
empty_boolean=BooleanOption(),
missing_key=StringOption()),
missing_section=Section())
data = OrderedDict()
for section in config_tree:
data[section] = config_tree[section].get_default()
ini_read(None, config_directory / 'data_types.cfg', config_tree, None)
assert data['section_one'].string == "some text"
assert ' '.join([
word.strip() for word in data[
'section_one'].multiline_string.split('\n')]) == "some lines of text"
assert data['section_one'].integer == 5
assert data['section_one'].float == 2.3
assert data['section_one'].boolean is True
assert data['section_two'].empty_string is None
assert data['section_two'].empty_integer is None
assert data['section_two'].empty_float is None
assert data['section_two'].empty_boolean is None
assert data['section_two'].missing_key is None

# test invalid values
config_tree = OrderedDict(section_three=Section(
not_an_integer=IntegerOption()))
with pytest.raises(ValueError):
ini_read(
None, config_directory / 'data_types.cfg', config_tree, None)
config_tree = OrderedDict(section_three=Section(
not_a_float=FloatOption()))
with pytest.raises(ValueError):
ini_read(
None, config_directory / 'data_types.cfg', config_tree, None)
config_tree = OrderedDict(section_three=Section(
not_a_boolean=BooleanOption()))
with pytest.raises(ValueError):
ini_read(
None, config_directory / 'data_types.cfg', config_tree, None)
20 changes: 20 additions & 0 deletions tests/test_read/data_types.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[section_one]
string = some text
multiline_string = some
lines
of
text
integer = 5
float = 2.3
boolean = True

[section_two]
empty_string =
empty_integer =
empty_float =
empty_boolean =

[section_three]
not_an_integer = 5.6
not_a_float = yes
not_a_boolean = text
12 changes: 10 additions & 2 deletions user_config/ini.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def ini_validate(_, elements):
raise InvalidConfigTree(
'nested sections are not supported for ini files')

def ini_read(_, path, elements, data):
def ini_read(_, path, elements, _data):
"""
Read ini configuration file and populate `data`.
Expand Down Expand Up @@ -82,8 +82,16 @@ def ini_read(_, path, elements, data):
try:
if keys[key].type_ == bool:
values[key] = config.getboolean(section, key)
elif keys[key].type_ == str:
value = config.get(section, key)
if value != '':
values[key] = value
else:
values[key] = keys[key].type_(config.get(section, key))
except ValueError:
# if the value is empty string, not defined, ignore
if config.get(section, key) != '':
raise
except configparser.NoOptionError:
pass
except configparser.NoSectionError:
Expand Down Expand Up @@ -122,7 +130,7 @@ def _print_item(key, item, value):
print(" {}".format(line))
print("")

def ini_write(_, elements, data, doc):
def ini_write(_, elements, _data, doc):
"""
Print default ini file.
Expand Down

0 comments on commit d3814f7

Please sign in to comment.