python-benedict/tests/dicts/test_benedict_casting.py

174 lines
4.2 KiB
Python
Raw Normal View History

2020-09-19 12:42:02 +00:00
# -*- coding: utf-8 -*-
from benedict import benedict
import unittest
class benedict_casting_test_case(unittest.TestCase):
2022-02-13 10:35:43 +00:00
"""
This class describes a benedict casting test case.
"""
2020-09-19 12:42:02 +00:00
def test__getitem__(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
2022-02-13 10:35:43 +00:00
c = b["b.c"]
2020-09-19 12:42:02 +00:00
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
2022-02-13 10:35:43 +00:00
self.assertTrue(c == d["b"]["c"])
self.assertFalse(c is d["b"]["c"])
2020-09-19 12:42:02 +00:00
2020-09-22 13:06:47 +00:00
def test_cast_dict_to_benedict(self):
2020-09-19 12:42:02 +00:00
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
bb = benedict(b)
bbd = bb.dict()
self.assertTrue(isinstance(bbd, dict))
self.assertFalse(isinstance(bbd, benedict))
2020-09-22 13:06:47 +00:00
self.assertEqual(d, bbd)
2020-09-19 12:42:02 +00:00
self.assertTrue(d is bbd)
2020-09-22 13:06:47 +00:00
def test_cast_benedict_to_dict(self):
2022-02-13 10:35:43 +00:00
b = benedict(
{
"a": 1,
"b": {
"c": {
"d": 2,
},
2020-09-22 13:06:47 +00:00
},
2022-02-13 10:35:43 +00:00
}
)
2020-09-22 13:06:47 +00:00
# d1 = dict(**b)
# print(d1)
d = dict(b)
self.assertTrue(isinstance(d, dict))
self.assertEqual(type(d), dict)
self.assertEqual(b, d)
self.assertFalse(b is d)
d = dict(b)
self.assertTrue(isinstance(d, dict))
self.assertEqual(type(d), dict)
self.assertEqual(b, d)
self.assertFalse(b is d)
def test_cast_benedict_kwargs_to_dict(self):
2022-02-13 10:35:43 +00:00
b = benedict(
{
"a": 1,
"b": {
"c": {
"d": 2,
},
2020-09-22 13:06:47 +00:00
},
2022-02-13 10:35:43 +00:00
}
)
2020-09-22 13:06:47 +00:00
d = dict(**b)
self.assertTrue(isinstance(d, dict))
self.assertEqual(type(d), dict)
self.assertEqual(b, d)
self.assertFalse(b is d)
2020-09-19 12:42:02 +00:00
def test_dict(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
bd = b.dict()
self.assertTrue(isinstance(bd, dict))
self.assertFalse(isinstance(bd, benedict))
self.assertTrue(d == bd)
self.assertTrue(d is bd)
def test_get(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
2022-02-13 10:35:43 +00:00
c = b.get("b.c")
2020-09-19 12:42:02 +00:00
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
2022-02-13 10:35:43 +00:00
self.assertTrue(c == d["b"]["c"])
self.assertFalse(c is d["b"]["c"])
2020-09-19 12:42:02 +00:00
def test_get_dict(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
2022-02-13 10:35:43 +00:00
c = b.get_dict("b.c")
2020-09-19 12:42:02 +00:00
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
2022-02-13 10:35:43 +00:00
self.assertTrue(c == d["b"]["c"])
self.assertFalse(c is d["b"]["c"])
2020-09-19 12:42:02 +00:00
def test_get_list_item(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": [
{
"d": 2,
},
{
"e": 3,
},
{
"f": 4,
},
2020-09-19 12:42:02 +00:00
]
},
}
b = benedict(d)
2022-02-13 10:35:43 +00:00
c = b.get_list_item("b.c", 1)
2020-09-19 12:42:02 +00:00
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
2022-02-13 10:35:43 +00:00
self.assertTrue(c == d["b"]["c"][1])
self.assertFalse(c is d["b"]["c"][1])
2020-09-19 12:42:02 +00:00
def test_pop(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": {
"c": {
"d": 2,
2020-09-19 12:42:02 +00:00
},
},
}
b = benedict(d)
2022-02-13 10:35:43 +00:00
c = b.pop("b.c")
2020-09-19 12:42:02 +00:00
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
d["b"]["c"]