fix bug with value setting
This commit is contained in:
parent
d95236ec95
commit
cfb205b67e
|
@ -3,7 +3,7 @@
|
||||||
__title__ = 'bison'
|
__title__ = 'bison'
|
||||||
__description__ = 'Python application configuration'
|
__description__ = 'Python application configuration'
|
||||||
__url__ = 'https://github.com/edaniszewski/bison'
|
__url__ = 'https://github.com/edaniszewski/bison'
|
||||||
__version__ = '0.0.2'
|
__version__ = '0.0.3'
|
||||||
__author__ = 'Erick Daniszewski'
|
__author__ = 'Erick Daniszewski'
|
||||||
__author_email__ = 'edaniszewski@gmail.com'
|
__author_email__ = 'edaniszewski@gmail.com'
|
||||||
__license__ = 'MIT'
|
__license__ = 'MIT'
|
||||||
|
|
|
@ -62,8 +62,14 @@ class DotDict(dict):
|
||||||
# otherwise, traverse the key components to set the value
|
# otherwise, traverse the key components to set the value
|
||||||
first, remainder = key.split('.', 1)
|
first, remainder = key.split('.', 1)
|
||||||
if first in self:
|
if first in self:
|
||||||
v = super(DotDict, self).__getitem__(first)
|
elem = super(DotDict, self).__getitem__(first)
|
||||||
v.__setitem__(remainder, value)
|
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:
|
else:
|
||||||
k, v = build_dot_value(key, value)
|
k, v = build_dot_value(key, value)
|
||||||
|
|
|
@ -84,6 +84,33 @@ class TestBison:
|
||||||
assert len(b._override) == 1
|
assert len(b._override) == 1
|
||||||
assert b.get(key) == value
|
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(
|
@pytest.mark.parametrize(
|
||||||
'paths', [
|
'paths', [
|
||||||
(),
|
(),
|
||||||
|
|
|
@ -202,6 +202,168 @@ class TestDotDict:
|
||||||
dd[key] = value
|
dd[key] = value
|
||||||
assert dd.get(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(
|
@pytest.mark.parametrize(
|
||||||
'key,expected', [
|
'key,expected', [
|
||||||
('a', True),
|
('a', True),
|
||||||
|
|
Loading…
Reference in New Issue