Skip to content

Commit

Permalink
fix bug with value setting
Browse files Browse the repository at this point in the history
  • Loading branch information
edaniszewski committed Mar 8, 2018
1 parent d95236e commit cfb205b
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bison/__version__.py
Expand Up @@ -3,7 +3,7 @@
__title__ = 'bison'
__description__ = 'Python application configuration'
__url__ = 'https://github.com/edaniszewski/bison'
__version__ = '0.0.2'
__version__ = '0.0.3'
__author__ = 'Erick Daniszewski'
__author_email__ = 'edaniszewski@gmail.com'
__license__ = 'MIT'
Expand Down
10 changes: 8 additions & 2 deletions bison/utils.py
Expand Up @@ -62,8 +62,14 @@ def __setitem__(self, key, value):
# otherwise, traverse the key components to set the value
first, remainder = key.split('.', 1)
if first in self:
v = super(DotDict, self).__getitem__(first)
v.__setitem__(remainder, value)
elem = super(DotDict, self).__getitem__(first)
if isinstance(elem, dict):
dd = DotDict(elem)
dd.__setitem__(remainder, value)
elem.update(dd)
else:
k, v = build_dot_value(key, value)
super(DotDict, self).__setitem__(k, v)

else:
k, v = build_dot_value(key, value)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_bison.py
Expand Up @@ -84,6 +84,33 @@ def test_set(self, key, value):
assert len(b._override) == 1
assert b.get(key) == value

def test_set_multiple_nested(self):
"""Set overrides for multiple nested values"""
b = bison.Bison()
assert len(b._override) == 0
assert len(b.config) == 0

b.set('foo.bar.a', 'test')

assert b.config == {
'foo': {
'bar': {
'a': 'test'
}
}
}

b.set('foo.bar.b', 'test')

assert b.config == {
'foo': {
'bar': {
'a': 'test',
'b': 'test'
}
}
}

@pytest.mark.parametrize(
'paths', [
(),
Expand Down
162 changes: 162 additions & 0 deletions tests/test_utils.py
Expand Up @@ -202,6 +202,168 @@ def test_set(self, key, value):
dd[key] = value
assert dd.get(key) == value

def test_set_multiple_nested(self):
"""Set multiple nested items"""
dd = utils.DotDict()
assert dd == {}

dd['foo.bar.a'] = 'test'
assert dd == {
'foo': {
'bar': {
'a': 'test'
}
}
}

dd['foo.bar.b'] = 'test'
assert dd == {
'foo': {
'bar': {
'a': 'test',
'b': 'test'
}
}
}

def test_set_multiple_neste2(self):
"""Set multiple nested items"""
dd = utils.DotDict()
assert dd == {}

dd['foo'] = 'test'
assert dd == {
'foo': 'test'
}

dd['bar.baz'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test'
}
}

dd['bar.ball'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test'
}
}

dd['bar.bits.a'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test',
'bits': {
'a': 'test'
}
}
}

dd['bar.bits.b'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test',
'bits': {
'a': 'test',
'b': 'test'
}
}
}

dd['bar.new.a'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test',
'bits': {
'a': 'test',
'b': 'test'
},
'new': {
'a': 'test'
}
}
}

dd['bar.new.b.c.d'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test',
'bits': {
'a': 'test',
'b': 'test'
},
'new': {
'a': 'test',
'b': {
'c': {
'd': 'test'
}
}
}
}
}

dd['bar.new.b'] = 'test'
assert dd == {
'foo': 'test',
'bar': {
'baz': 'test',
'ball': 'test',
'bits': {
'a': 'test',
'b': 'test'
},
'new': {
'a': 'test',
'b': 'test'
}
}
}

dd['bar'] = 'test'
assert dd == {
'foo': 'test',
'bar': 'test'
}

dd['foo.bar.a'] = 'test'
assert dd == {
'foo': {
'bar': {
'a': 'test'
}
},
'bar': 'test'
}

dd['foo'] = ['a', 'b', 'c']
assert dd == {
'foo': ['a', 'b', 'c'],
'bar': 'test'
}

dd['foo.bar.b'] = ['a', 'b', 'c']
assert dd == {
'foo': {
'bar': {
'b': ['a', 'b', 'c']
}
},
'bar': 'test'
}

@pytest.mark.parametrize(
'key,expected', [
('a', True),
Expand Down

0 comments on commit cfb205b

Please sign in to comment.