def test_setitem(self): l = rom.List(connection=self.conn, key="l") l.value = ["one", "two"] l[0] = "three" l[1] = "four" self.assertEqual(["three", "four"], l.value) self.assertRaises(IndexError, l.__setitem__, 2, "five")
def test_len(self): l = rom.List(connection=self.conn, key="l") self.assertEqual(0, len(l)) l.value = ["x", "y"] self.assertEqual(2, len(l)) l.append("z") self.assertEqual(3, len(l))
def test_append(self): l = rom.List(connection=self.conn, key="l") size = l.append("a") self.assertEqual(["a"], l.value) self.assertEqual(1, size) size = l.append("b") self.assertEqual(["a", "b"], l.value) self.assertEqual(2, size)
def test_getitem(self): l = rom.List(connection=self.conn, key="l") self.assertRaises(IndexError, l.__getitem__, 0) self.assertRaises(TypeError, l.__getitem__, "cat") l.append("x") self.assertEqual("x", l[0]) l.extend(["y", "z"]) self.assertEqual("y", l[1]) self.assertEqual("z", l[2]) self.assertRaises(IndexError, l.__getitem__, 3)
def test_value(self): l = rom.List(connection=self.conn, key="l") self.assertEqual([], l.value) native_list = ["one", "two"] l.value = native_list self.assertEqual(native_list, l.value) # make sure that reassignment clears the old values native_list = ["three", "four"] l.value = native_list self.assertEqual(native_list, l.value) self.assertEqual(str(native_list), str(l))
def test_value_encode_decode(self): l = rom.List(connection=self.conn, key='l', value_encoder=rom.json_enc, value_decoder=rom.json_dec) e0 = {'arbitrary': 3} e1 = ['data', 7.2] l.extend([e0, e1]) r0 = l[0] self.assertEqual(e0, r0) r1 = l[1] self.assertEqual(e1, r1)
def test_iterator(self): l = rom.List(connection=self.conn, key="l") l.value = ["a", "b", "c"] self.assertEqual(["a", "b", "c"], l.value)
def test_extend(self): l = rom.List(connection=self.conn, key="l") l.extend(["a", "b"]) self.assertEqual(["a", "b"], l.value) l.extend(["c", "d"]) self.assertEqual(["a", "b", "c", "d"], l.value)