def test_update_strict(self): """update should be strict""" with self.assertRaisesRegex(Exception, r'non-existent'): gpath.update(self.obj, ["bar", 4], 17) with self.assertRaisesRegex(Exception, r'not an array'): gpath.update(self.obj, ["foo"], 17) with self.assertRaisesRegex(Exception, r'invalid.*index'): gpath.update(self.obj, ["foo", -1], 17) with self.assertRaisesRegex(Exception, r'invalid.*index'): gpath.update(self.obj, ["foo", None], 17)
def test_glob_nested(self): """should scan nested arrays""" self.obj = [{"a": [1, 2, 3]}, {"a": [4, 5, 6]}, {"a": [7, 8, 9]}] self.assertEqual(gpath.update(self.obj, ["*", "a", "*"], 5), 9) self.assertEqual(self.obj, [{ "a": [5, 5, 5] }, { "a": [5, 5, 5] }, { "a": [5, 5, 5] }])
def test_update(self): """update should update array items""" self.assertEqual(self.obj["foo"], [{"bar": 1}, {"bar": 2}, {"baz": 3}]) gpath.update(self.obj, ["foo", 0], "asdf") self.assertEqual(self.obj["foo"], ["asdf", {"bar": 2}, {"baz": 3}]) gpath.update(self.obj, ["foo", 2], "hello") self.assertEqual(self.obj["foo"], ["asdf", {"bar": 2}, "hello"]) gpath.update(self.obj, ["foo", 1], None) self.assertEqual(self.obj["foo"], ["asdf", None, "hello"])
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"])