Skip to content

Commit

Permalink
tests for the rest of the ConfigElement classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nihlaeth committed Feb 12, 2017
1 parent 807c1c8 commit 239d712
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 2 deletions.
181 changes: 180 additions & 1 deletion tests/test_config_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
import argparse
import pytest

from user_config import ConfigElement, InvalidData
from user_config import (
ConfigElement,
MissingData,
InvalidData,
Section,
StringOption,
IntegerOption,
FloatOption,
BooleanOption)


def validate_length(value):
"""Custom validator."""
Expand Down Expand Up @@ -131,3 +140,173 @@ def test_validate(self):
config_element.validate_data({'test': 5})
config_element.validate("ok")
config_element.validate_data({'test': "ok"})

class TestStringOption(object):

"""
At this time, StringOption is equivalent to ConfigElement.
"""

class TestIntegerOption(object):

def test_init(self):
with pytest.raises(InvalidData):
config_element = IntegerOption(default="5")
config_element = IntegerOption(default=5)
assert config_element.get_default() == 5

def test_parser(self, capsys):
config_element = IntegerOption(doc="helpful text")
config_element.element_name = "number"
parser = argparse.ArgumentParser(prog="test application")
config_element.construct_parser(parser)
arguments = vars(parser.parse_args(['--number', '5']))
assert arguments['number'] == 5
data = {}
config_element.extract_data_from_parser(arguments, data)
assert data['number'] == 5

parser.print_help()
out, err = capsys.readouterr()
assert out == """usage: test application [-h] [--number NUMBER]
optional arguments:
-h, --help show this help message and exit
--number NUMBER helpful text
"""
assert err == ""

class TestFloatOption(object):

def test_init(self):
with pytest.raises(InvalidData):
config_element = FloatOption(default="5")
with pytest.raises(InvalidData):
config_element = FloatOption(default=5)
config_element = FloatOption(default=5.2)
assert config_element.get_default() == 5.2

def test_parser(self):
config_element = FloatOption(doc="helpful text")
config_element.element_name = "number"
parser = argparse.ArgumentParser(prog="test application")
config_element.construct_parser(parser)
arguments = vars(parser.parse_args(['--number', '5.3']))
assert arguments['number'] == 5.3
data = {}
config_element.extract_data_from_parser(arguments, data)
assert data['number'] == 5.3

class TestBooleanOption(object):

def test_init(self):
with pytest.raises(InvalidData):
config_element = BooleanOption(default="true")
with pytest.raises(InvalidData):
config_element = BooleanOption(default=0)
config_element = BooleanOption(default=True)
assert config_element.get_default() is True

def test_parser(self):
config_element = BooleanOption(doc="helpful text")
config_element.element_name = "all_good"
parser = argparse.ArgumentParser(prog="test application")
config_element.construct_parser(parser)
arguments = vars(parser.parse_args(['--all_good', 'yes']))
assert arguments['all_good'] is True
data = {}
config_element.extract_data_from_parser(arguments, data)
assert data['all_good'] is True

class TestSection(object):

def test_init(self):
section = Section(default=42)
assert section.get_default() is section
section = Section(default=None)
assert section.has_default()
assert len(section) == 0
with pytest.raises(AttributeError):
section = Section(keyword="not a config element")
section = Section(name=StringOption(default="test"))
section.get_default()

# test map methods
assert len(section) == 1
assert section.name == "test"
assert section['name'] == "test"
section.name = "something else"
assert section.name == "something else"
with pytest.raises(InvalidData):
# pylint: disable=redefined-variable-type
section.name = False
section.not_field = None
assert section.not_field is None
with pytest.raises(AttributeError):
section['not_field'] = None
for field in section:
assert field == "name"
iterated = True
assert iterated
assert "name" in section
assert section.get("nonexisting", "I'm fine") == "I'm fine"
# TODO: test update, keys, values and items

def test_incomplete_count(self):
# test optional section
section = Section(
required=False,
one=IntegerOption(),
two=IntegerOption(required=False))
section.get_default()
section.element_name = "section"
parser = argparse.ArgumentParser(prog="test application")
section.construct_parser(parser)

# missing required value
arguments = vars(parser.parse_args([]))
section.extract_data_from_parser(arguments, None)
section.validate_data(None)
assert section.incomplete_count == 1

# missing required value, optional value provided
arguments = vars(parser.parse_args(['--two', '42']))
section.extract_data_from_parser(arguments, None)
section.validate_data(None)
assert section.incomplete_count == 1

# required value provided
arguments = vars(parser.parse_args(['--one', '5']))
section.extract_data_from_parser(arguments, None)
section.validate_data(None)
assert section.incomplete_count == 0

# test required section
section = Section(
required=True,
one=IntegerOption(),
two=IntegerOption(required=False))
section.get_default()
section.element_name = "section"
parser = argparse.ArgumentParser(prog="test application")
section.construct_parser(parser)

# missing required value
arguments = vars(parser.parse_args([]))
section.extract_data_from_parser(arguments, None)
with pytest.raises(MissingData):
section.validate_data(None)
assert section.incomplete_count == 0

# missing required value, optional value provided
arguments = vars(parser.parse_args(['--two', '42']))
section.extract_data_from_parser(arguments, None)
with pytest.raises(MissingData):
section.validate_data(None)
assert section.incomplete_count == 0

# required value provided
arguments = vars(parser.parse_args(['--one', '5']))
section.extract_data_from_parser(arguments, None)
section.validate_data(None)
assert section.incomplete_count == 0
3 changes: 2 additions & 1 deletion user_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __len__(self):
return len(self._elements)

def __getitem__(self, key):
return self._dict['key']
return self._data[key]

def __setitem__(self, key, value):
if key not in self._elements:
Expand Down Expand Up @@ -381,6 +381,7 @@ def validate(self, value):
pass

def validate_data(self, _):
self.incomplete_count = 0
for element in self._elements:
try:
self._elements[element].validate_data(self._data)
Expand Down

0 comments on commit 239d712

Please sign in to comment.