Improved benedict casting support.

This commit is contained in:
Fabio Caccamo 2020-09-19 14:42:02 +02:00
parent 0baa5bb7f0
commit 2d519d76cb
4 changed files with 296 additions and 0 deletions

View File

@ -35,6 +35,9 @@ class benedict(KeypathDict, IODict, ParseDict):
""" """
Constructs a new instance. Constructs a new instance.
""" """
if len(args) == 1 and isinstance(args[0], benedict):
super(benedict, self).__init__(args[0].dict())
return
super(benedict, self).__init__(*args, **kwargs) super(benedict, self).__init__(*args, **kwargs)
def __getitem__(self, key): def __getitem__(self, key):

View File

View File

@ -0,0 +1,170 @@
# -*- coding: utf-8 -*-
from benedict.dicts.base import BaseDict
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
import unittest
class base_dict_test_case(unittest.TestCase):
def test__bool__(self):
b = BaseDict()
self.assertFalse(b)
self.assertFalse(bool(b))
b = BaseDict()
b['a'] = 1
self.assertTrue(b)
self.assertTrue(bool(b))
b = BaseDict({ 'a':1 })
self.assertTrue(b)
self.assertTrue(bool(b))
def test__contains__(self):
b = BaseDict({ 'a':1 })
self.assertTrue('a' in b)
self.assertFalse('b' in b)
def test__delitem__(self):
b = BaseDict({ 'a':1 })
del b['a']
self.assertFalse('a' in b)
with self.assertRaises(KeyError):
del b['b']
def test__equal__(self):
b = BaseDict({ 'a':1 })
o1 = { 'a':1 }
o2 = { 'a':2 }
self.assertTrue(b == o1)
self.assertFalse(b == o2)
def test__getitem__(self):
b = BaseDict({ 'a':1 })
self.assertEqual(b['a'], 1)
with self.assertRaises(KeyError):
b['b']
def test__iter__(self):
b = BaseDict({ 'a':1, 'b':2 })
i = iter(b)
self.assertTrue(isinstance(i, Iterable))
def test__len__(self):
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
self.assertEqual(len(b), 3)
def test__repr__(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
self.assertEqual(repr(d), repr(b))
def test__setitem__(self):
b = BaseDict()
b['a'] = 1
self.assertEqual(b['a'], 1)
def test__str__(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
self.assertEqual(str(d), str(b))
def test_clear(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
b.clear()
self.assertTrue(b == {})
self.assertTrue(d == {})
self.assertTrue(b == d)
def test_copy(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
c = b.copy()
c['a'] = -1
c['b'] = -2
c['c'] = -3
self.assertTrue(b == d)
self.assertFalse(b == c)
# self.assertTrue(type(b) == type(c))
def test_dict(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
self.assertFalse(b is d)
self.assertTrue(b.dict() is d)
def test_dict_pointer(self):
d = {
'a': 1,
'b': 2,
'c': {
'd': 3,
'e': {
'f': 4,
}
}
}
b = BaseDict(d)
b['a'] = -1
b['b'] = -2
b['c']['d'] = -3
b['c']['e']['f'] = -4
self.assertEqual(d, b)
def test_get(self):
b = BaseDict({ 'a':1 })
self.assertEqual(b.get('a'), 1)
self.assertEqual(b.get('b'), None)
self.assertEqual(b.get('b', 2), 2)
def test_items(self):
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
i = list(b.items())
i.sort()
self.assertTrue(i, [('a', 1, ), ('b', 2, ), ('c', 3, )])
def test_keys(self):
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
k = list(b.keys())
k.sort()
self.assertTrue(k, ['a', 'b', 'c'])
def test_pop(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
v = b.pop('c')
self.assertEqual(v, 3)
with self.assertRaises(KeyError):
v = b.pop('d')
v = b.pop('e', 5)
self.assertEqual(v, 5)
self.assertEqual(d, { 'a':1, 'b':2 })
self.assertTrue(b == d)
def test_setdefault(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
v = b.setdefault('c', 4)
self.assertEqual(v, 3)
v = b.setdefault('d', 4)
self.assertEqual(v, 4)
self.assertEqual(d, { 'a':1, 'b':2, 'c':3, 'd':4 })
self.assertTrue(b == d)
def test_update(self):
d = { 'a':1, 'b':2, 'c':3 }
b = BaseDict(d)
b.update({ 'd':4, 'e':5 })
self.assertEqual(d, { 'a':1, 'b':2, 'c':3, 'd':4, 'e':5 })
self.assertTrue(b == d)
def test_values(self):
b = BaseDict({ 'a':1, 'b':2, 'c':3 })
v = list(b.values())
v.sort()
self.assertTrue(v, [1, 2, 3])

View File

@ -0,0 +1,123 @@
# -*- coding: utf-8 -*-
from benedict import benedict
import unittest
class benedict_casting_test_case(unittest.TestCase):
def test__getitem__(self):
d = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
b = benedict(d)
c = b['b.c']
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
self.assertTrue(c == d['b']['c'])
self.assertFalse(c is d['b']['c'])
def test_cast_benedict_instance(self):
d = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
b = benedict(d)
bb = benedict(b)
bbd = bb.dict()
self.assertTrue(isinstance(bbd, dict))
self.assertFalse(isinstance(bbd, benedict))
self.assertTrue(d == bbd)
self.assertTrue(d is bbd)
def test_dict(self):
d = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
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 = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
b = benedict(d)
c = b.get('b.c')
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
self.assertTrue(c == d['b']['c'])
self.assertFalse(c is d['b']['c'])
def test_get_dict(self):
d = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
b = benedict(d)
c = b.get_dict('b.c')
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
self.assertTrue(c == d['b']['c'])
self.assertFalse(c is d['b']['c'])
def test_get_list_item(self):
d = {
'a': 1,
'b': {
'c': [
{ 'd': 2, },
{ 'e': 3, },
{ 'f': 4, },
]
},
}
b = benedict(d)
c = b.get_list_item('b.c', 1)
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
self.assertTrue(c == d['b']['c'][1])
self.assertFalse(c is d['b']['c'][1])
def test_pop(self):
d = {
'a': 1,
'b': {
'c': {
'd': 2,
},
},
}
b = benedict(d)
c = b.pop('b.c')
self.assertTrue(isinstance(c, benedict))
self.assertEqual(type(c), benedict)
with self.assertRaises(KeyError):
d['b']['c']