python-benedict/tests/dicts/base/test_base_dict.py

524 lines
12 KiB
Python
Raw Normal View History

2020-09-19 12:42:02 +00:00
# -*- coding: utf-8 -*-
from benedict.dicts.base import BaseDict
try:
from collections.abc import Iterable
except ImportError:
from collections import Iterable
import copy
import sys
2020-09-19 12:42:02 +00:00
import unittest
class base_dict_test_case(unittest.TestCase):
2022-02-13 10:35:43 +00:00
"""
This class describes a BaseDict test case.
"""
2020-09-19 12:42:02 +00:00
def test__bool__(self):
b = BaseDict()
self.assertFalse(b)
self.assertFalse(bool(b))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
2020-09-19 12:42:02 +00:00
self.assertTrue(b)
self.assertTrue(bool(b))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-30 15:08:08 +00:00
def test__bool__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {"a": 1}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
self.assertTrue(b)
self.assertTrue(bool(b))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-30 15:08:08 +00:00
b = BaseDict({})
self.assertFalse(b)
self.assertFalse(bool(b))
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__contains__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
self.assertTrue("a" in b)
self.assertFalse("b" in b)
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test__contains__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {"a": 1}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
self.assertTrue("a" in b)
self.assertFalse("b" in b)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2022-02-13 10:35:43 +00:00
del d["a"]
self.assertFalse("a" in b)
2020-09-19 12:42:02 +00:00
def test__deepcopy__(self):
b1 = BaseDict()
2022-02-13 10:35:43 +00:00
b1["a"] = {}
b1["a"]["b"] = {}
b1["a"]["b"]["c"] = True
b2 = copy.deepcopy(b1)
self.assertEqual(b1, b2)
self.assertEqual(type(b1), type(b2))
self.assertFalse(b1 is b2)
def test__deepcopy__with_pointer(self):
d = {}
2022-02-13 10:35:43 +00:00
d["a"] = {}
d["a"]["b"] = {}
d["a"]["b"]["c"] = True
b1 = BaseDict(d)
b2 = copy.deepcopy(b1)
self.assertEqual(b1, b2)
self.assertEqual(type(b1), type(b2))
self.assertFalse(b1 is b2)
2020-09-19 12:42:02 +00:00
def test__delitem__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
del b["a"]
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test__delitem__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
self.assertTrue("a" in b)
del b["a"]
self.assertFalse("a" in b)
2020-09-19 12:42:02 +00:00
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
del b["a"]
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__equal__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
o1 = {}
2022-02-13 10:35:43 +00:00
o2 = {
"a": 2,
}
2020-09-30 15:08:08 +00:00
self.assertTrue(b == o1)
self.assertFalse(b == o2)
self.assertEqual(b, b.dict())
def test__equal__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
o1 = {
"a": 1,
}
o2 = {
"a": 2,
}
2020-09-19 12:42:02 +00:00
self.assertTrue(b == o1)
self.assertFalse(b == o2)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__getitem__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2020-09-19 12:42:02 +00:00
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
b["a"]
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test__getitem__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
self.assertEqual(b["a"], 1)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__iter__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
i = iter(b)
self.assertTrue(isinstance(i, Iterable))
self.assertEqual(b, b.dict())
def test__iter__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
i = iter(b)
self.assertTrue(isinstance(i, Iterable))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__len__(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
self.assertEqual(len(b), 0)
2022-02-13 10:35:43 +00:00
b["a"] = 1
2020-09-30 15:08:08 +00:00
self.assertEqual(len(b), 1)
self.assertEqual(b, b.dict())
def test__len__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
self.assertEqual(len(b), 3)
2020-09-30 15:08:08 +00:00
self.assertEqual(len(d), 3)
2022-02-13 10:35:43 +00:00
b["d"] = 4
2020-09-30 15:08:08 +00:00
self.assertEqual(len(b), 4)
self.assertEqual(len(d), 4)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__repr__(self):
2020-09-30 15:08:08 +00:00
d = {}
b = BaseDict()
self.assertEqual(repr(d), repr(b))
self.assertEqual(b, b.dict())
def test__repr__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
self.assertEqual(repr(d), repr(b))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__setitem__(self):
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
self.assertEqual(b["a"], 1)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
2020-09-30 15:08:08 +00:00
def test__setitem__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
b["a"] = 2
self.assertEqual(b["a"], 2)
self.assertEqual(d["a"], 2)
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test__str__(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
b = BaseDict(
{
"a": 1,
"b": 2,
"c": 3,
}
)
2020-09-19 12:42:02 +00:00
self.assertEqual(str(d), str(b))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
2020-09-30 15:08:08 +00:00
def test__str__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
self.assertEqual(str(d), str(b))
self.assertEqual(b, b.dict())
2022-02-13 10:35:43 +00:00
@unittest.skipIf(sys.version_info[0] > 2, "No unicode in Python 3")
def test__unicode__(self):
d = BaseDict()
2022-02-13 10:35:43 +00:00
d["name"] = "pythòn-bènèdìçt"
print(unicode(d))
2022-02-13 10:35:43 +00:00
@unittest.skipIf(sys.version_info[0] > 2, "No unicode in Python > 2")
def test__unicode__with_pointer(self):
2022-02-13 10:35:43 +00:00
d = BaseDict({"name": "pythòn-bènèdìçt"})
print(unicode(d))
2020-09-19 12:42:02 +00:00
def test_clear(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
2020-09-30 15:08:08 +00:00
self.assertTrue(b == d)
b.clear()
self.assertTrue(b == {})
self.assertTrue(d != {})
def test_clear_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-19 12:42:02 +00:00
b = BaseDict(d)
b.clear()
self.assertTrue(b == {})
self.assertTrue(d == {})
self.assertTrue(b == d)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_copy(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
2020-09-30 15:08:08 +00:00
c = b.copy()
2022-02-13 10:35:43 +00:00
c["a"] = -1
c["b"] = -2
c["c"] = -3
2020-09-30 15:08:08 +00:00
self.assertFalse(b == c)
# self.assertTrue(type(b) == type(c))
self.assertEqual(b, b.dict())
def test_copy_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-19 12:42:02 +00:00
b = BaseDict(d)
c = b.copy()
2022-02-13 10:35:43 +00:00
c["a"] = -1
c["b"] = -2
c["c"] = -3
2020-09-19 12:42:02 +00:00
self.assertTrue(b == d)
self.assertFalse(b == c)
# self.assertTrue(type(b) == type(c))
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_dict(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-19 12:42:02 +00:00
b = BaseDict(d)
self.assertFalse(b is d)
self.assertTrue(b.dict() is d)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_dict_pointer(self):
d = {
2022-02-13 10:35:43 +00:00
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": {
"f": 4,
},
},
2020-09-19 12:42:02 +00:00
}
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
b["a"] = -1
b["b"] = -2
b["c"]["d"] = -3
b["c"]["e"]["f"] = -4
2020-09-19 12:42:02 +00:00
self.assertEqual(d, b)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_get(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
self.assertEqual(b.get("a"), 1)
self.assertEqual(b.get("b"), None)
self.assertEqual(b.get("b", 2), 2)
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test_get_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
self.assertEqual(b.get("a"), 1)
d["a"] = 2
self.assertEqual(b.get("a"), 2)
self.assertEqual(b.get("b"), None)
self.assertEqual(b.get("b", 2), 2)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_items(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
2020-09-30 15:08:08 +00:00
i = list(b.items())
i.sort()
2022-02-13 10:35:43 +00:00
self.assertTrue(i, [("a", 1), ("b", 2), ("c", 3)])
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test_items_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {"a": 1, "b": 2, "c": 3}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
i = list(b.items())
i.sort()
2022-02-13 10:35:43 +00:00
self.assertTrue(i, [("a", 1), ("b", 2), ("c", 3)])
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_keys(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
2020-09-30 15:08:08 +00:00
k = list(b.keys())
k.sort()
2022-02-13 10:35:43 +00:00
self.assertTrue(k, ["a", "b", "c"])
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test_keys_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2020-09-19 12:42:02 +00:00
k = list(b.keys())
k.sort()
2022-02-13 10:35:43 +00:00
self.assertTrue(k, ["a", "b", "c"])
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_pop(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
v = b.pop("c")
2020-09-30 15:08:08 +00:00
self.assertEqual(v, 3)
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
v = b.pop("d")
v = b.pop("e", 5)
2020-09-30 15:08:08 +00:00
self.assertEqual(v, 5)
self.assertEqual(b, b.dict())
def test_pop_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-19 12:42:02 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
v = b.pop("c")
2020-09-19 12:42:02 +00:00
self.assertEqual(v, 3)
with self.assertRaises(KeyError):
2022-02-13 10:35:43 +00:00
v = b.pop("d")
v = b.pop("e", 5)
2020-09-19 12:42:02 +00:00
self.assertEqual(v, 5)
2022-02-13 10:35:43 +00:00
self.assertEqual(
d,
{
"a": 1,
"b": 2,
},
)
2020-09-19 12:42:02 +00:00
self.assertTrue(b == d)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_setdefault(self):
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
v = b.setdefault("c", 4)
2020-09-19 12:42:02 +00:00
self.assertEqual(v, 3)
2022-02-13 10:35:43 +00:00
v = b.setdefault("d", 4)
2020-09-19 12:42:02 +00:00
self.assertEqual(v, 4)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
2020-09-30 15:08:08 +00:00
def test_setdefault_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
v = b.setdefault("c", 4)
2020-09-30 15:08:08 +00:00
self.assertEqual(v, 3)
2022-02-13 10:35:43 +00:00
v = b.setdefault("d", 4)
2020-09-30 15:08:08 +00:00
self.assertEqual(v, 4)
2022-02-13 10:35:43 +00:00
self.assertEqual(
d,
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
},
)
d["d"] = 4
2020-09-30 15:08:08 +00:00
self.assertTrue(b == d)
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_update(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
b.update(
{
"d": 4,
"e": 5,
}
)
self.assertEqual(b, {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5})
2020-09-30 15:08:08 +00:00
self.assertEqual(b, b.dict())
def test_update_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-19 12:42:02 +00:00
b = BaseDict(d)
2022-02-13 10:35:43 +00:00
b.update(
{
"d": 4,
"e": 5,
}
)
self.assertEqual(
d,
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
},
)
d["a"] = -1
2020-09-19 12:42:02 +00:00
self.assertTrue(b == d)
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-19 12:42:02 +00:00
def test_values(self):
2020-09-30 15:08:08 +00:00
b = BaseDict()
2022-02-13 10:35:43 +00:00
b["a"] = 1
b["b"] = 2
b["c"] = 3
2020-09-19 12:42:02 +00:00
v = list(b.values())
v.sort()
self.assertTrue(v, [1, 2, 3])
2020-09-22 13:07:07 +00:00
self.assertEqual(b, b.dict())
2020-09-30 15:08:08 +00:00
def test_values_with_pointer(self):
2022-02-13 10:35:43 +00:00
d = {
"a": 1,
"b": 2,
"c": 3,
}
2020-09-30 15:08:08 +00:00
b = BaseDict(d)
v = list(b.values())
v.sort()
self.assertTrue(v, [1, 2, 3])
self.assertEqual(b, b.dict())