def test_xpath_move(self): d = { "regular": [ {"name": "foo", "size": 2}, {"name": "bar", "size": 1}, {"name": "baz", "size": 42}, ], "regular-multi": [ {"name": "foo", "group": "xxx", "size": 2}, {"name": "bar", "group": "yyy", "size": 1}, {"name": "baz", "group": "xxx", "size": 42}, ], "ll": ["a", "b", "c", "d"], "ll-keyed": ly.KeyedList([10, 20, 30, 40]), } with self.assertRaises(KeyError): ly.xpath_move(d, "/not/found", "") with self.assertRaises(KeyError): ly.xpath_move(d, "/ll[.='x']", "") with self.assertRaises(KeyError): ly.xpath_move(d, "/ll[.='c']", "x") with self.assertRaises(ValueError): ly.xpath_move(d, "/ll", "") ly.xpath_move(d, "/ll[.='c']", "a") ly.xpath_move(d, "/ll[.='b']", "") ly.xpath_move(d, "/ll[.='a']", None) with self.assertRaises(ValueError): ly.xpath_move(d, "/ll-keyed[.='40']", "") with self.assertRaises(ValueError): ly.xpath_move(d, "/ll-keyed[.='10']", None) with self.assertRaises(ValueError): ly.xpath_move(d, "/ll-keyed[.='30']", "40") self.assertEqual( d, { "regular": [ {"name": "foo", "size": 2}, {"name": "bar", "size": 1}, {"name": "baz", "size": 42}, ], "regular-multi": [ {"group": "xxx", "name": "foo", "size": 2}, {"group": "yyy", "name": "bar", "size": 1}, {"group": "xxx", "name": "baz", "size": 42}, ], "ll": ["b", "c", "d", "a"], "ll-keyed": [10, 20, 30, 40], }, )
def test_keylist_leaflist(self): l = ly.KeyedList("abcdef") self.assertEqual(len(l), 6) self.assertIn("e", l) l.remove("e") self.assertNotIn("e", l) self.assertIn("c", l) del l["c"] self.assertNotIn("c", l) with self.assertRaises(KeyError): del l["x"] with self.assertRaises(KeyError): l.remove("x") self.assertEqual(l.pop("x", "not-found"), "not-found") self.assertEqual(l.pop("b"), "b") self.assertNotIn("b", l) l.clear() self.assertEqual(len(l), 0) self.assertEqual(l, [])
def test_keylist_base(self): l = ly.KeyedList(["x"]) with self.assertRaises(TypeError): l.index("x") with self.assertRaises(TypeError): l.insert(0, "x") with self.assertRaises(TypeError): l.reverse() with self.assertRaises(TypeError): l.sort() with self.assertRaises(TypeError): l["x"] = 2 with self.assertRaises(TypeError): l[0] # pylint: disable=pointless-statement with self.assertRaises(TypeError): del l[0] with self.assertRaises(TypeError): l.pop() with self.assertRaises(TypeError): l.pop(0)
def test_keylist_list_multiple_keys(self): l = ly.KeyedList( [ { "id": 5, "size": 10, "desc": "foo" }, { "id": 8, "size": 5, "desc": "bar" }, { "id": 2, "size": 14, "desc": "baz" }, ], key_name=("id", "size"), ) self.assertIn(("8", "5"), l) self.assertEqual(l["8", "5"], {"id": 8, "size": 5, "desc": "bar"}) self.assertEqual(l[("2", "14")], {"id": 2, "size": 14, "desc": "baz"}) del l["2", "14"] self.assertEqual( l, [{ "id": 5, "size": 10, "desc": "foo" }, { "id": 8, "size": 5, "desc": "bar" }], ) l.clear() self.assertEqual(len(l), 0) self.assertEqual(l, [])
def test_keylist_list_one_key(self): l = ly.KeyedList( [ { "mtu": 1500, "name": "eth0" }, { "mtu": 512, "name": "eth2" }, { "mtu": 1280, "name": "eth4" }, ], key_name="name", ) self.assertIn("eth2", l) self.assertEqual(l["eth4"], {"mtu": 1280, "name": "eth4"}) del l["eth0"] self.assertEqual(l, [{ "mtu": 512, "name": "eth2" }, { "mtu": 1280, "name": "eth4" }]) l.append({"name": "eth6", "mtu": 6}) l.append({"name": "eth5", "mtu": 5}) l.append({"name": "eth10", "mtu": 10}) self.assertEqual( l, # order does not matter [ { "mtu": 10, "name": "eth10" }, { "mtu": 512, "name": "eth2" }, { "mtu": 5, "name": "eth5" }, { "mtu": 6, "name": "eth6" }, { "mtu": 1280, "name": "eth4" }, ], ) self.assertEqual(l.pop("eth10000", {}), {}) self.assertEqual(l.pop("eth10"), {"mtu": 10, "name": "eth10"}) self.assertNotIn("eth10", l) l.clear() self.assertEqual(len(l), 0) self.assertEqual(l, [])
], }, "ipv6": { "address": [ {"ip": "3ffe::123:1"}, {"ip": "3ffe::c00:c00"}, ], }, }, { "name": "eth1", "ipv4": { "address": ly.KeyedList( [ {"ip": "10.0.0.2"}, {"ip": "10.0.0.6"}, ], key_name="ip", ), }, "ipv6": { "address": ly.KeyedList( [ {"ip": "3ffe::321:8", "prefixlen": 64, "tentative": False}, {"ip": "3ffe::ff12", "prefixlen": 96, "tentative": True}, ], key_name=("ip", "prefixlen"), ), }, }, ],