2020-02-03 17:25:54 +00:00
|
|
|
import unittest
|
|
|
|
|
2022-10-14 14:53:06 +00:00
|
|
|
from benedict.core import rename as _rename
|
|
|
|
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
class rename_test_case(unittest.TestCase):
|
2022-02-13 10:35:43 +00:00
|
|
|
"""
|
|
|
|
This class describes a rename test case.
|
|
|
|
"""
|
2020-02-03 17:25:54 +00:00
|
|
|
|
|
|
|
def test_rename(self):
|
|
|
|
d = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": {
|
|
|
|
"x": 1,
|
|
|
|
"y": 1,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"b": {
|
|
|
|
"x": 2,
|
|
|
|
"y": 2,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"c": {
|
|
|
|
"x": 3,
|
|
|
|
"y": 3,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
|
|
|
}
|
2022-02-13 10:35:43 +00:00
|
|
|
_rename(d, "a", "d")
|
2020-02-03 17:25:54 +00:00
|
|
|
r = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"b": {
|
|
|
|
"x": 2,
|
|
|
|
"y": 2,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"c": {
|
|
|
|
"x": 3,
|
|
|
|
"y": 3,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
2022-02-13 10:35:43 +00:00
|
|
|
"d": {
|
|
|
|
"x": 1,
|
|
|
|
"y": 1,
|
2020-02-03 17:25:54 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
self.assertEqual(d, r)
|
|
|
|
|
|
|
|
def test_rename_with_same_key(self):
|
|
|
|
d = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
2020-02-03 17:25:54 +00:00
|
|
|
}
|
2022-02-13 10:35:43 +00:00
|
|
|
_rename(d, "a", "a")
|
2020-02-03 17:25:54 +00:00
|
|
|
r = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
2020-02-03 17:25:54 +00:00
|
|
|
}
|
|
|
|
self.assertEqual(d, r)
|
|
|
|
|
|
|
|
def test_rename_to_existing_name(self):
|
|
|
|
d = {
|
2022-02-13 10:35:43 +00:00
|
|
|
"a": 1,
|
|
|
|
"b": 2,
|
|
|
|
"c": 3,
|
2020-02-03 17:25:54 +00:00
|
|
|
}
|
|
|
|
with self.assertRaises(KeyError):
|
2022-02-13 10:35:43 +00:00
|
|
|
_rename(d, "a", "c")
|