Skip to content

Commit

Permalink
added tests for config.
Browse files Browse the repository at this point in the history
  • Loading branch information
schettino72 committed Feb 22, 2014
1 parent 89925eb commit 903b9a9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
26 changes: 17 additions & 9 deletions doitpy/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ class Config(dict):
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)

def __setitem__(self, key, value):
"""make sure new items are not added after initialization"""
if key not in self:
msg = 'New items can not be added to Config, invalid key:{}'
raise KeyError(msg.format(key))
super(Config, self).__setitem__(key, value)

# http://stackoverflow.com/questions/2060972
# subclassing-python-dictionary-to-override-setitem
def update(self, *args, **kwargs):
Expand All @@ -22,19 +29,20 @@ def setdefault(self, key, value=None):
return self[key]
# end - redefinition of methods to make sure __setitem__ is always called

def __setitem__(self, key, value):
assert key in self
super(Config, self).__setitem__(key, value)

def copy(self):
"""copy that returns a Config object instead of plain dict"""
return self.__class__(dict.copy(self))
return self.__class__(self)


# non-dict methods
def make(self, *args, **kwargs):
"""return new Config, updating with given values
# extra methods
def push(self, other):
Also accepts None as single argument, in this case just return a copy
of self.
"""
result = self.copy()
if other is not None:
result.update(other)
if not(args and args[0] is None):
result.update(*args, **kwargs)
return result

2 changes: 1 addition & 1 deletion doitpy/coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Coverage(object):
omit=[])

def __init__(self, pkgs, config=None):
self.config = self.config.push(config)
self.config = self.config.make(config)
self.pkgs = []
for pkg in pkgs:
if isinstance(pkg, PythonPackage):
Expand Down
2 changes: 1 addition & 1 deletion doitpy/pyflakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, **kwargs):
"""
@param exclude_patterns: (list - str) pathlib patterns to be excluded
"""
self.config = self.config.push(kwargs)
self.config = self.config.make(kwargs)

def __call__(self, py_file):
"""return task metadata to run pyflakes on a single module"""
Expand Down
72 changes: 72 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from doitpy.config import Config



def test_init():
config = Config({'foo': 'bar'})
assert config['foo'] == 'bar'
assert isinstance(config, dict)


class TestConfigSetItem(object):
def test_setitem(self):
config = Config({'foo': 'bar'})
config['foo'] = 'baz'
assert config['foo'] == 'baz'

def test_cant_setitem_with_new_key(self):
config = Config({'foo': 'bar'})
with pytest.raises(KeyError):
config['foo2'] = 'baz'


class TestConfigUpdate(object):
def test_update_with_dict(self):
config = Config({'foo': 'bar'})
config.update({'foo': 'baz'})
assert config['foo'] == 'baz'

def test_update_error_two_args(self):
config = Config({'foo': 'bar'})
pytest.raises(TypeError, config.update, {'foo': 'baz'}, {'foo': 'baz2'})

def test_update_keyword(self):
config = Config({'foo': 'bar'})
config.update(foo='baz')
assert config['foo'] == 'baz'

def test_update_fail_new_item(self):
config = Config({'foo': 'bar'})
pytest.raises(KeyError, config.update, foo2='baz')


class TestConfigSetDefault(object):
def test_setdefault_ok(self):
config = Config({'foo': 'bar'})
assert config.setdefault('foo', 'baz') == 'bar'

def test_setdefault_fail_new_item(self):
config = Config({'foo': 'bar'})
pytest.raises(KeyError, config.setdefault, 'foo2', 'baz')


def test_copy():
config = Config({'foo': 'bar'})
assert isinstance(config.copy(), Config)




class TestConfigMake(object):
def test_make_new_value(self):
config = Config({'foo': 'bar'})
c2 = config.make({'foo': 'baz'})
assert config['foo'] == 'bar'
assert c2['foo'] == 'baz'

def test_make_None(self):
config = Config({'foo': 'bar'})
c2 = config.make(None)
assert c2['foo'] == 'bar'

0 comments on commit 903b9a9

Please sign in to comment.