def test_slice(self): i1 = self.data_items[0] expect = self.data_items.copy() expect[5:6] = [i1] l = DataList(self.data_items) # Slice read self.assertListEqual(l[:], self.data_items[:]) self.assertListEqual(l[:1],self.data_items[:1]) self.assertListEqual(l[1:], self.data_items[1:]) self.assertListEqual(l[2:2], self.data_items[2:2]) self.assertListEqual(l[2:3], self.data_items[2:3]) self.assertListEqual(l[2:4], self.data_items[2:4]) self.assertListEqual(l[-1:], self.data_items[-1:]) self.assertListEqual(l[:-1], self.data_items[:-1]) # Slice set l[5:6] = [i1] self.assertListEqual(l, expect) # With type_spec l = DataList(self.data_items, Item) with self.assertRaises(TypeError): l[5:6] = [self.data_desc[0]] l[5:6] = [i1] self.assertListEqual(l, expect) # Slice remove l = DataList(self.data_items) del l[:] self.assertListEqual(l, []) # Frozen l = DataList(self.data_items) with self.assertRaises(TypeError): l.freeze() del l[:]
def test_freeze(self): l = DataList(self.data_items) # l.freeze() self.assertTrue(l.frozen) with self.assertRaises(TypeError): l[0] = self.data_items[0]
def test_insert(self): i1, i2, i3 = self.data_items[:3] l = DataList() # Simple l.insert(0, i1) self.assertListEqual(l, [i1], "Simple") l.insert(0, i2) self.assertListEqual(l, [i2, i1], "Simple") l.insert(1, i3) self.assertListEqual(l, [i2, i3, i1], "Simple") l.insert(5, i3) self.assertListEqual(l, [i2, i3, i1, i3], "Simple") # With type_spec l = DataList(type_spec=Item) l.insert(0, i1) with self.assertRaises(TypeError, msg="With type_spec"): l.insert(0, self.data_desc[0]) # With key expr l = DataList(key_expr=self.key_item) l.insert(0, i1) self.assertListEqual(l, [i1], "With key expr") # Frozen with self.assertRaises(TypeError): l.freeze() l.insert(0, i1)
def test_remove(self): i1, i2, i3 = self.data_items[:3] l = DataList(self.data_items[:3]) # l.remove(i2) self.assertListEqual(l, [i1, i3]) # Frozen with self.assertRaises(TypeError): l.freeze() l.remove(i1)
def test_list_delete(self): i1, i2, i3 = self.data_items[:3] l = DataList(self.data_items[:3]) # del l[1] self.assertListEqual(l, [i1, i3]) # Frozen with self.assertRaises(TypeError): l.freeze() del l[1]
def test_extend(self): l = DataList() # Simple l.extend(self.data_items) self.assertListEqual(l, self.data_items) # With type_spec l = DataList(type_spec=Item) l.extend(self.data_items) self.assertListEqual(l, self.data_items) with self.assertRaises(TypeError, msg="With type_spec"): l.extend(self.data_desc) # With key expr l = DataList(key_expr=self.key_item) l.extend(self.data_items) self.assertListEqual(l, self.data_items, "With key expr") # Frozen with self.assertRaises(TypeError): l.freeze() l.extend(self.data_items[0])
def test_extract(self): exp_return = [x for x in self.data_items if x.key > 5] exp_remains = [x for x in self.data_items if not x.key > 5] l = DataList(self.data_items) # result = l.extract(lambda x: x.key > 5) self.assertIsInstance(result, DataList) self.assertListEqual(result, exp_return) self.assertListEqual(l, exp_remains) self.assertEqual(len(result) + len(l), len(self.data_items)) # l = DataList(self.data_items) result = l.extract('item.key > 5') self.assertListEqual(result, exp_return) self.assertListEqual(l, exp_remains) self.assertEqual(len(result) + len(l), len(self.data_items)) # frozen with self.assertRaises(TypeError): l.freeze() l.extract('item.key > 5')
def test_list_update(self): i1 = self.data_items[0] l = DataList(self.data_items) # Simple l[3] = i1 self.assertEqual(l[3], i1) l = DataList(self.data_items, type_spec=Item) # With type_spec l[3] = i1 self.assertEqual(l[3], i1) with self.assertRaises(TypeError, msg="With type_spec"): l[3] = self.data_desc[0] # With key expr l = DataList(self.data_items, key_expr=self.key_item) l[3] = i1 self.assertEqual(l[3], i1) # Frozen with self.assertRaises(TypeError): l.freeze() l[3] = i1
def test_in(self): # Simple l = DataList(self.data_items) self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) # Frozen l.freeze() self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) # Typed l = DataList(self.data_items, Item) self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) # Frozen l.freeze() self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) # Keyed l = DataList(self.data_items, key_expr='item.key') self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) # Frozen l.freeze() self.assertTrue(self.data_items[0] in l) self.assertTrue(self.data_items[-1] in l) nil = Item(100, "NOT IN LISTS") i5 = self.data_items[4] # Simple l = DataList(self.data_items) self.assertIn(i5, l) self.assertNotIn(nil, l) # Frozen distincts l = DataList(self.data_items, type_spec=Item, frozen=True) self.assertIn(i5, l) self.assertNotIn(nil, l) # Frozen key_expr l = DataList(self.data_items, key_expr=self.key_item, frozen=True) self.assertIn(i5, l) self.assertNotIn(nil, l)