Example #1
0
    def test_glob_wildcard_keys(self):
        """should not scan object keys"""
        self.assertEqual(self.obj["foo"], [{"bar": 1}, {"bar": 2}, {"baz": 3}])

        self.assertEqual(gpath.place(self.obj, ["foo", 0, "*"], 17), 1)
        self.assertEqual(self.obj["foo"], [{
            "bar": 1,
            '*': 17
        }, {
            "bar": 2
        }, {
            "baz": 3
        }])

        with self.assertRaisesRegex(Exception, r'non-existent'):
            gpath.place(self.obj, ["*", 0, "bar"], 17)
Example #2
0
    def test_glob(self):
        """glob should scan arrays"""
        self.assertEqual(self.obj["foo"], [{"bar": 1}, {"bar": 2}, {"baz": 3}])

        self.assertEqual(gpath.place(self.obj, ["foo", "*", "bar"], 17), 3)
        self.assertEqual(self.obj["foo"], [{
            "bar": 17
        }, {
            "bar": 17
        }, {
            "baz": 3,
            "bar": 17
        }])

        with self.assertRaisesRegex(Exception,
                                    r'non-existent object at \/foo\/\*\/bad'):
            gpath.place(self.obj, ["foo", "*", "bad", "test"], 10)

        self.assertEqual(gpath.update(self.obj, ["foo", "*"], "hello"), 3)
        self.assertEqual(self.obj["foo"], ["hello", "hello", "hello"])
Example #3
0
    def test_set_strict(self):
        with self.assertRaisesRegex(Exception, r"non-existent"):
            gpath.place(self.obj, ["bar", 4], 17)

        with self.assertRaisesRegex(Exception, r"not a plain object"):
            gpath.place(self.obj, ["foo", 0], 17)
Example #4
0
    def test_set(self):
        gpath.place(self.obj, ["foo"], {"bar": 1, "baz": 2})
        self.assertEqual(self.obj["foo"], {"bar": 1, "baz": 2})
        gpath.place(self.obj, ["foo", "bar"], 17)
        self.assertEqual(self.obj["foo"], {"bar": 17, "baz": 2})
        gpath.place(self.obj, ["foo", "baz"], None)
        self.assertEqual(self.obj["foo"], {"bar": 17})

        self.assertEqual(self.obj["hello"], "world")
        gpath.place(self.obj, ["hello"], None)
        self.assertFalse("hello" in self.obj)
        gpath.place(self.obj, ["hello"],
                    None)  # OK to remove a non-existent property.
        self.assertFalse("hello" in self.obj)
        gpath.place(self.obj, ["hello"], "blah")
        self.assertEqual(self.obj["hello"], "blah")