python-benedict/tests/core/test_unflatten.py

80 lines
1.6 KiB
Python
Raw Normal View History

import unittest
from benedict.core import unflatten as _unflatten
class unflatten_test_case(unittest.TestCase):
2022-02-13 10:35:43 +00:00
"""
This class describes an unflatten test case.
"""
def test_unflatten(self):
2020-02-20 10:41:14 +00:00
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b_c": 2,
"d_e": 3,
2020-02-20 10:41:14 +00:00
}
u = _unflatten(d)
r = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": 2,
2020-02-20 10:41:14 +00:00
},
2022-02-13 10:35:43 +00:00
"d": {
"e": 3,
2020-02-20 10:41:14 +00:00
},
}
self.assertEqual(u, r)
def test_unflatten_with_custom_separator(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b|c": 2,
"d|e": 3,
2020-02-20 10:41:14 +00:00
}
2022-02-13 10:35:43 +00:00
u = _unflatten(d, separator="#")
2020-02-20 10:41:14 +00:00
self.assertEqual(u, d)
2022-02-13 10:35:43 +00:00
u = _unflatten(d, separator="|")
2020-02-20 10:41:14 +00:00
r = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": 2,
2020-02-20 10:41:14 +00:00
},
2022-02-13 10:35:43 +00:00
"d": {
"e": 3,
2020-02-20 10:41:14 +00:00
},
}
self.assertEqual(u, r)
def test_unflatten_with_nested_dict(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b_c": {
"u_v": 2,
2020-02-20 10:41:14 +00:00
},
2022-02-13 10:35:43 +00:00
"d_e": {
"x_y_z": 3,
2020-02-20 10:41:14 +00:00
},
}
u = _unflatten(d)
r = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"u": {
"v": 2,
2020-02-20 10:41:14 +00:00
},
},
},
2022-02-13 10:35:43 +00:00
"d": {
"e": {
"x": {
"y": {
"z": 3,
2020-02-20 10:41:14 +00:00
},
},
},
},
}
self.assertEqual(u, r)