def test___deepcopy__(self):
     a1 = NestedDict({"k": {"foo": "bar"}})
     a2 = copy.deepcopy(a1)
     self.assertIsNot(a1, a2)
     self.assertEqual(a1, a2)
     a1.data["k"]["foo"] = "baz"
     self.assertNotEqual(a1, a2)
    def test_collapse__function_arg__stop_at_root(self):
        a = NestedDict({"k1": {"k2": {"$foo": {"k3": {"k4": "v"}}}}})

        def detect_operator(key, *args):
            return key == "k1"

        b = a.collapse(func=detect_operator)
        self.assertEqual(b, {"k1": {("k2", "$foo", "k3", "k4"): "v"}})
    def test___eq____different_class__return_True(self):
        a1 = NestedDict({"k": {"foo": "bar"}})
        a2 = {"k": {"foo": "bar"}}
        self.assertTrue(a1 == a2)

        a3 = NestedDict()
        a4 = {}
        self.assertTrue(a3 == a4)
    def test_collapse__function_arg(self):
        a = NestedDict({"k1": {"k2": {"$foo": {"k3": {"k4": "v"}}}}})

        def detect_operator(key, *args):
            return key[0] == "$"

        b = a.collapse(func=detect_operator)
        self.assertEqual(b, {("k1", "k2"): {"$foo": {("k3", "k4"): "v"}}})
    def test_ref__func_arg(self):
        class CustomClass(NestedDict):
            def get(self):
                return {"k2": {"k3": "v"}}

        def ref_func(haystack, segment, path, i):
            if isinstance(haystack, CustomClass):
                return haystack.get()
            return haystack

        a = NestedDict({"k1": CustomClass()})

        self.assertEqual(a.ref(("k1", "k2", "k3"), func=ref_func), "v")
    def test_ref__func_arg__raises_StopIteration(self):
        class CustomClass(NestedDict):
            def get(self):
                return "foo"

        def ref_func(haystack, segment, path, i):
            if isinstance(haystack, CustomClass):
                e = StopIteration()
                e.haystack = haystack.get()
                raise e

            return haystack

        a = NestedDict({"k1": {"k2": CustomClass()}})

        self.assertEqual(a.ref(("k1", "k2", "k3"), func=ref_func), "foo")
 def test___bool____return_False(self):
     self.assertFalse(bool(NestedDict()))
 def test_collapse(self):
     a = NestedDict({"k1": {"k2": {"k3": "v"}}})
     b = a.collapse()
     self.assertEqual(b, {("k1", "k2", "k3"): "v"})
 def test___ne____return_False(self):
     a1 = NestedDict({"k": {"foo": "bar"}})
     a2 = NestedDict({"k": {"foo": "bar"}})
     self.assertFalse(a1 != a2)
Example #10
0
 def test___eq____return_False(self):
     a1 = NestedDict()
     a2 = None
     self.assertFalse(a1 == a2)
Example #11
0
 def test_pull__cleanup_True__removes_empty_containers(self):
     a = NestedDict({"k": ["v"]})
     a.pull("k", "v", cleanup=True)
     self.assertEqual(a, {})
Example #12
0
 def test___bool____return_True(self):
     self.assertTrue(bool(NestedDict({"k": {"k": "v"}})))
Example #13
0
 def test_unset__string_arg(self):
     a = NestedDict({"k": "v"})
     a.unset("k")
     self.assertEqual(a, {})
Example #14
0
 def test_pull__string_arg__raises_TypeError(self):
     a = NestedDict({"k": "v"})
     with self.assertRaises(AttributeError):
         a.pull("k", "v")
Example #15
0
 def test_pull__string_arg__raises_ValueError(self):
     a = NestedDict({"k": []})
     with self.assertRaises(ValueError):
         a.pull("k", "v")
Example #16
0
 def test___call____instance_of_self_arg(self):
     a = NestedDict()
     a(NestedDict({"k": "v"}))
     self.assertEqual(a, {"k": "v"})
Example #17
0
 def test_pull__string_arg__raises_KeyError(self):
     a = NestedDict()
     with self.assertRaises(KeyError):
         a.pull("k", "v")
Example #18
0
 def test_pull__path_arg(self):
     a = NestedDict({"k1": {"k2": {"k3": ["v"]}}})
     a.pull(("k1", "k2", "k3"), "v")
     self.assertEqual(a, {"k1": {"k2": {"k3": []}}})
Example #19
0
 def test_unset__cleanup_True__removes_empty_containers(self):
     a = NestedDict({"k1": {"k2": {"k3": "v"}}})
     a.unset(("k1", "k2", "k3"), cleanup=True)
     self.assertEqual(a, {"k1": {}})
Example #20
0
 def test___repr__(self):
     a = NestedDict({"k": {"k": "v"}})
     self.assertEqual(repr(a), "NestedDict({'k': NestedDict({'k': 'v'})})")
Example #21
0
 def test__expand(self):
     a = NestedDict({"k1": {"k2": {"k3": "v"}}})
     b = a._expand(a.collapse())
     self.assertEqual(b, a.get())
Example #22
0
 def test_unset__string_arg__raises_KeyError(self):
     a = NestedDict({"k": "v"})
     with self.assertRaises(KeyError):
         a.unset("foo")
Example #23
0
 def test___eq____return_True(self):
     a1 = NestedDict({"k": {"k": "v"}})
     a2 = NestedDict({"k": {"k": "v"}})
     self.assertTrue(a1 == a2)
Example #24
0
 def test_merge__nested_data__nested_data_arg(self):
     a = NestedDict({"k1": {"k2": "v"}})
     b = a.merge({"k1": {"k3": "v"}})
     self.assertEqual(b, {"k1": {"k2": "v", "k3": "v"}})
Example #25
0
 def test___eq____same_class__return_False(self):
     a1 = NestedDict({"k": {"foo": "bar"}})
     a2 = NestedDict({"k": {"bar": "baz"}})
     self.assertFalse(a1 == a2)
Example #26
0
 def test_merge__NestedDict_arg(self):
     a = NestedDict({"k1": "v"})
     b = a.merge(NestedDict({"k2": "v"}))
     self.assertEqual(b, {"k1": "v", "k2": "v"})
Example #27
0
 def test___ne____return_True(self):
     a1 = NestedDict({"k": {"foo": "bar"}})
     a2 = NestedDict({"k": {"bar": "baz"}})
     self.assertTrue(a1 != a2)
Example #28
0
 def test_merge__incompatible_types(self):
     a = NestedDict({"k1": "v"})
     b = a.merge("foo")
     self.assertEqual(b, {"k1": "v"})
Example #29
0
 def test___str__(self):
     a = NestedDict({"foo": "bar"})
     self.assertEqual(str(a), "{'foo': 'bar'}")
Example #30
0
 def test_update__NestedDict_arg(self):
     a = NestedDict({"k1": "v"})
     a.update(NestedDict({"k2": "v"}))
     self.assertEqual(a, {"k1": "v", "k2": "v"})