Permalink
Browse files

added tests for config.

  • Loading branch information...
1 parent 89925eb commit 903b9a93a61dfe1216f0076c91687ead1eff01e3 @schettino72 schettino72 committed Feb 22, 2014
Showing with 91 additions and 11 deletions.
  1. +17 −9 doitpy/config.py
  2. +1 −1 doitpy/coverage.py
  3. +1 −1 doitpy/pyflakes.py
  4. +72 −0 tests/test_config.py
View
26 doitpy/config.py
@@ -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):
@@ -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
View
2 doitpy/coverage.py
@@ -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):
View
2 doitpy/pyflakes.py
@@ -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"""
View
72 tests/test_config.py
@@ -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.