Fixed `keypath_separator` support when using `from_{format}` methods. #12

This commit is contained in:
Fabio Caccamo 2020-03-13 13:53:49 +01:00
parent 7c0f11ea83
commit 86a23c7c11
2 changed files with 37 additions and 1 deletions

View File

@ -28,7 +28,7 @@ from benedict.dicts.keypath import KeypathDict
from benedict.dicts.parse import ParseDict from benedict.dicts.parse import ParseDict
class benedict(IODict, KeypathDict, ParseDict): class benedict(KeypathDict, IODict, ParseDict):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
""" """

View File

@ -607,6 +607,42 @@ class benedict_test_case(unittest.TestCase):
self.assertTrue(isinstance(d, benedict)) self.assertTrue(isinstance(d, benedict))
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },}) self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
def test_from_yaml_with_keypath_separator_in_keys(self):
# fix: https://github.com/fabiocaccamo/python-benedict/issues/12
j = """
192.168.0.1:
test: value
test2: value2
value:
value_with_period: 12.34.45
"""
with self.assertRaises(ValueError):
# static method
d = benedict.from_yaml(j)
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
# constructor
d = benedict(j, format='yaml')
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, { 'a':1, 'b':{ 'c':3, 'd':4 },})
r = {
'192.168.0.1': {
'test':'value',
'test2':'value2',
},
'value': {
'value_with_period':'12.34.45',
},
}
# static method
d = benedict.from_yaml(j, keypath_separator=None)
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, r)
# constructor
d = benedict(j, format='yaml', keypath_separator=None)
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, r)
def test_from_xml(self): def test_from_xml(self):
j = """ j = """
<?xml version="1.0" ?> <?xml version="1.0" ?>