Пример #1
0
class TestRedisList(TestCase):
    """list tests"""

    def setUp(self):
        self.redis = MockRedis()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis[LIST1]))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen(LIST1))
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(2, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(1, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(0, self.redis.llen(LIST1))

    def test_lpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL1, self.redis.lpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL2, self.redis.lpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush(LIST1, VAL1)
        self.redis.lpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL2, VAL1], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.lpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL3, VAL1, VAL2, VAL1], self.redis.redis[LIST1])

    def test_rpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL2, self.redis.rpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL1, self.redis.rpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush(LIST1, VAL1)
        self.redis.rpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.rpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2, VAL1, VAL3], self.redis.redis[LIST1])

    def test_lrem(self):
        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL1, 0)
        self.assertEqual(2, count)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, 1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL1, 100)
        self.assertEqual(2, count)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL3, -1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, -1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL3, VAL4],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, -2)
        self.assertEqual(2, count)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4], self.redis.redis[LIST1])

        count = self.redis.lrem("NON_EXISTENT_LIST", VAL1, 0)
        self.assertEqual(0, count)

    def test_rpoplpush(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.redis.redis[LIST2] = [VAL3, VAL4]
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual(VAL2, transfer_item)
        self.assertEqual([VAL1], self.redis.redis[LIST1])
        self.assertEqual([VAL2, VAL3, VAL4], self.redis.redis[LIST2])

    def test_lrange_get_all(self):
        """Cases for returning entire list"""
        values = [VAL4, VAL3, VAL2, VAL1]

        self.assertEqual([], self.redis.lrange(LIST1, 0, 6))
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))
        self.redis.lpush(LIST1, *reversed(values))

        # Check with exact range
        self.assertEqual(values, self.redis.lrange(LIST1, 0, 3))
        # Check with negative index
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))
        # Check with range larger than length of list
        self.assertEqual(values, self.redis.lrange(LIST1, 0, 6))

    def test_lrange_get_sublist(self):
        """Cases for returning partial list"""
        values = [VAL4, VAL3, VAL2, VAL1]

        self.assertEqual([], self.redis.lrange(LIST1, 0, 6))
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))
        self.redis.lpush(LIST1, *reversed(values))

        # Check from left end of the list
        self.assertEqual(values[:2], self.redis.lrange(LIST1, 0, 1))
        # Check from right end of the list
        self.assertEqual(values[2:4], self.redis.lrange(LIST1, 2, 3))
        # Check from right end of the list with negative range
        self.assertEqual(values[-2:], self.redis.lrange(LIST1, -2, -1))
        # Check from middle of the list
        self.assertEqual(values[1:3], self.redis.lrange(LIST1, 1, 2))

    def test_ltrim_retain_all(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 0, -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, 0, len(values) - 1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, 0, len(values) + 1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, -1 * len(values), -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, -1 * (len(values) + 1), -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

    def test_ltrim_remove_all(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 2, 1)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -1, -2)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, 2, -3)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -1, 2)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

    def test_ltrim(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 1, 2)
        self.assertEqual(values[1:3], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -3, -1)
        self.assertEqual(values[-3:], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, 1, 5)
        self.assertEqual(values[1:5], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -100, 2)
        self.assertEqual(values[-100:3], self.redis.lrange(LIST1, 0, -1))

    def test_lset(self):
        with self.assertRaises(Exception):
            self.redis.lset(LIST1, 1, VAL1)

        self.redis.lpush(LIST1, VAL2)
        self.assertEqual([VAL2], self.redis.lrange(LIST1, 0, -1))

        with self.assertRaises(Exception):
            self.redis.lset(LIST1, 1, VAL1)

        self.redis.lset(LIST1, 0, VAL1)
        self.assertEqual([VAL1], self.redis.lrange(LIST1, 0, -1))

    def _reinitialize_list(self, key, *values):
        """
        Re-initialize the list
        """
        self.redis.delete(LIST1)
        self.redis.lpush(LIST1, *reversed(values))
Пример #2
0
class TestRedisList(TestCase):
    """list tests"""

    def setUp(self):
        self.redis = MockRedis()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis[LIST1]))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen(LIST1))
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(2, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(1, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(0, self.redis.llen(LIST1))

    def test_lpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL1, self.redis.lpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL2, self.redis.lpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush(LIST1, VAL1)
        self.redis.lpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL2, VAL1], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.lpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL3, VAL1, VAL2, VAL1], self.redis.redis[LIST1])

    def test_rpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL2, self.redis.rpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL1, self.redis.rpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush(LIST1, VAL1)
        self.redis.rpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.rpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2, VAL1, VAL3], self.redis.redis[LIST1])

    def test_lrem(self):
        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL1, 0)
        self.assertEqual(2, count)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, 1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL1, 100)
        self.assertEqual(2, count)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL3, -1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, -1)
        self.assertEqual(1, count)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL3, VAL4],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        count = self.redis.lrem(LIST1, VAL2, -2)
        self.assertEqual(2, count)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4], self.redis.redis[LIST1])

        count = self.redis.lrem("NON_EXISTENT_LIST", VAL1, 0)
        self.assertEqual(0, count)

    def test_rpoplpush(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.redis.redis[LIST2] = [VAL3, VAL4]
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual(VAL2, transfer_item)
        self.assertEqual([VAL1], self.redis.redis[LIST1])
        self.assertEqual([VAL2, VAL3, VAL4], self.redis.redis[LIST2])

    def test_rpoplpush_with_empty_source(self):
        # source list is empty
        self.redis.redis[LIST1] = []
        self.redis.redis[LIST2] = [VAL3, VAL4]
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual(None, transfer_item)
        self.assertEqual([], self.redis.redis[LIST1])
        # nothing has been added to the destination queue
        self.assertEqual([VAL3, VAL4], self.redis.redis[LIST2])

    def test_rpoplpush_source_with_empty_string(self):
        # source list contains empty string
        self.redis.redis[LIST1] = ['']
        self.redis.redis[LIST2] = [VAL3, VAL4]
        self.assertEqual(1, self.redis.llen(LIST1))
        self.assertEqual(2, self.redis.llen(LIST2))
        
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual('', transfer_item)
        self.assertEqual(0, self.redis.llen(LIST1))
        self.assertEqual(3, self.redis.llen(LIST2))
        self.assertEqual([], self.redis.redis[LIST1])
        # empty string is added to the destination queue
        self.assertEqual(['', VAL3, VAL4], self.redis.redis[LIST2])

    def test_lrange_get_all(self):
        """Cases for returning entire list"""
        values = [VAL4, VAL3, VAL2, VAL1]

        self.assertEqual([], self.redis.lrange(LIST1, 0, 6))
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))
        self.redis.lpush(LIST1, *reversed(values))

        # Check with exact range
        self.assertEqual(values, self.redis.lrange(LIST1, 0, 3))
        # Check with negative index
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))
        # Check with range larger than length of list
        self.assertEqual(values, self.redis.lrange(LIST1, 0, 6))

    def test_lrange_get_sublist(self):
        """Cases for returning partial list"""
        values = [VAL4, VAL3, VAL2, VAL1]

        self.assertEqual([], self.redis.lrange(LIST1, 0, 6))
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))
        self.redis.lpush(LIST1, *reversed(values))

        # Check from left end of the list
        self.assertEqual(values[:2], self.redis.lrange(LIST1, 0, 1))
        # Check from right end of the list
        self.assertEqual(values[2:4], self.redis.lrange(LIST1, 2, 3))
        # Check from right end of the list with negative range
        self.assertEqual(values[-2:], self.redis.lrange(LIST1, -2, -1))
        # Check from middle of the list
        self.assertEqual(values[1:3], self.redis.lrange(LIST1, 1, 2))

    def test_ltrim_retain_all(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 0, -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, 0, len(values) - 1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, 0, len(values) + 1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, -1 * len(values), -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

        self.redis.ltrim(LIST1, -1 * (len(values) + 1), -1)
        self.assertEqual(values, self.redis.lrange(LIST1, 0, -1))

    def test_ltrim_remove_all(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 2, 1)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -1, -2)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, 2, -3)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -1, 2)
        self.assertEqual([], self.redis.lrange(LIST1, 0, -1))

    def test_ltrim(self):
        values = [VAL4, VAL3, VAL2, VAL1]
        self._reinitialize_list(LIST1, *values)

        self.redis.ltrim(LIST1, 1, 2)
        self.assertEqual(values[1:3], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -3, -1)
        self.assertEqual(values[-3:], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, 1, 5)
        self.assertEqual(values[1:5], self.redis.lrange(LIST1, 0, -1))

        self._reinitialize_list(LIST1, *values)
        self.redis.ltrim(LIST1, -100, 2)
        self.assertEqual(values[-100:3], self.redis.lrange(LIST1, 0, -1))

    def test_lset(self):
        with self.assertRaises(Exception):
            self.redis.lset(LIST1, 1, VAL1)

        self.redis.lpush(LIST1, VAL2)
        self.assertEqual([VAL2], self.redis.lrange(LIST1, 0, -1))

        with self.assertRaises(Exception):
            self.redis.lset(LIST1, 1, VAL1)

        self.redis.lset(LIST1, 0, VAL1)
        self.assertEqual([VAL1], self.redis.lrange(LIST1, 0, -1))

    def _reinitialize_list(self, key, *values):
        """
        Re-initialize the list
        """
        self.redis.delete(LIST1)
        self.redis.lpush(LIST1, *reversed(values))
Пример #3
0
class TestRedisList(TestCase):
    """list tests"""
    def setUp(self):
        self.redis = MockRedis()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis[LIST1]))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen(LIST1))
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(2, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(1, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(0, self.redis.llen(LIST1))

    def test_lpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL1, self.redis.lpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL2, self.redis.lpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush(LIST1, VAL1)
        self.redis.lpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL2, VAL1], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.lpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL3, VAL1, VAL2, VAL1], self.redis.redis[LIST1])

    def test_rpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL2, self.redis.rpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL1, self.redis.rpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush(LIST1, VAL1)
        self.redis.rpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.rpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2, VAL1, VAL3], self.redis.redis[LIST1])

    def test_lrem(self):
        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 0, VAL1)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 1, VAL2)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 100, VAL1)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -1, VAL3)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -1, VAL2)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL3, VAL4],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -2, VAL2)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4], self.redis.redis[LIST1])

    def test_rpoplpush(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.redis.redis[LIST2] = [VAL3, VAL4]
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual(VAL2, transfer_item)
        self.assertEqual([VAL1], self.redis.redis[LIST1])
        self.assertEqual([VAL2, VAL3, VAL4], self.redis.redis[LIST2])
Пример #4
0
class TestRedisList(TestCase):
    """list tests"""

    def setUp(self):
        self.redis = MockRedis()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis[LIST1]))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen(LIST1))
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(2, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(1, self.redis.llen(LIST1))
        self.redis.redis[LIST1].pop(0)
        self.assertEquals(0, self.redis.llen(LIST1))

    def test_lpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL1, self.redis.lpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL2, self.redis.lpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush(LIST1, VAL1)
        self.redis.lpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL2, VAL1], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.lpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL3, VAL1, VAL2, VAL1], self.redis.redis[LIST1])

    def test_rpop(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.assertEquals(VAL2, self.redis.rpop(LIST1))
        self.assertEquals(1, len(self.redis.redis[LIST1]))
        self.assertEquals(VAL1, self.redis.rpop(LIST1))
        self.assertEquals(0, len(self.redis.redis[LIST1]))
        self.assertIsNone(self.redis.lpop(LIST1))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush(LIST1, VAL1)
        self.redis.rpush(LIST1, VAL2)

        # validate insertion
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2], self.redis.redis[LIST1])

        # insert two more values with one repeated
        self.redis.rpush(LIST1, VAL1, VAL3)

        # validate the update
        self.assertEquals("list", self.redis.type(LIST1))
        self.assertEquals([VAL1, VAL2, VAL1, VAL3], self.redis.redis[LIST1])

    def test_lrem(self):
        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 0, VAL1)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 1, VAL2)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, 100, VAL1)
        self.assertListEqual([VAL2, VAL3, VAL4, VAL2], self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -1, VAL3)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL4, VAL2],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -1, VAL2)
        self.assertListEqual([VAL1, VAL2, VAL1, VAL3, VAL4],
                             self.redis.redis[LIST1])

        self.redis.redis[LIST1] = [VAL1, VAL2, VAL1, VAL3, VAL4, VAL2]
        self.redis.lrem(LIST1, -2, VAL2)
        self.assertListEqual([VAL1, VAL1, VAL3, VAL4], self.redis.redis[LIST1])

    def test_rpoplpush(self):
        self.redis.redis[LIST1] = [VAL1, VAL2]
        self.redis.redis[LIST2] = [VAL3, VAL4]
        transfer_item = self.redis.rpoplpush(LIST1, LIST2)
        self.assertEqual(VAL2, transfer_item)
        self.assertEqual([VAL1], self.redis.redis[LIST1])
        self.assertEqual([VAL2, VAL3, VAL4], self.redis.redis[LIST2])
Пример #5
0
class TestList(TestCase):
    """
    Tests for MockRedis list operations
    """

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis['test_list']))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen('test_list'))
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals(2, self.redis.llen('test_list'))
        self.redis.redis['test_list'].pop(0)
        self.assertEquals(1, self.redis.llen('test_list'))
        self.redis.redis['test_list'].pop(0)
        self.assertEquals(0, self.redis.llen('test_list'))

    def test_lpop(self):
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals('val1', self.redis.lpop('test_list'))
        self.assertEquals(1, len(self.redis.redis['test_list']))
        self.assertEquals('val2', self.redis.lpop('test_list'))
        self.assertEquals(0, len(self.redis.redis['test_list']))
        self.assertIsNone(self.redis.lpop('test_list'))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush('test_list', 'val1')
        self.redis.lpush('test_list', 'val2')

        # validate insertion
        self.assertEquals(2, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val2', self.redis.redis['test_list'][0])
        self.assertEquals('val1', self.redis.redis['test_list'][1])

        # insert two more values with one repeated
        self.redis.lpush('test_list', 'val1', 'val3')

        # validate the update
        self.assertEquals(4, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val3', self.redis.redis['test_list'][0])
        self.assertEquals('val1', self.redis.redis['test_list'][1])
        self.assertEquals('val2', self.redis.redis['test_list'][2])
        self.assertEquals('val1', self.redis.redis['test_list'][3])

    def test_rpop(self):
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals('val2', self.redis.rpop('test_list'))
        self.assertEquals(1, len(self.redis.redis['test_list']))
        self.assertEquals('val1', self.redis.rpop('test_list'))
        self.assertEquals(0, len(self.redis.redis['test_list']))
        self.assertIsNone(self.redis.lpop('test_list'))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush('test_list', 'val1')
        self.redis.rpush('test_list', 'val2')

        # validate insertion
        self.assertEquals(2, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val1', self.redis.redis['test_list'][0])
        self.assertEquals('val2', self.redis.redis['test_list'][1])

        # insert two more values with one repeated
        self.redis.rpush('test_list', 'val1', 'val3')

        # validate the update
        self.assertEquals(4, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val1', self.redis.redis['test_list'][0])
        self.assertEquals('val2', self.redis.redis['test_list'][1])
        self.assertEquals('val1', self.redis.redis['test_list'][2])
        self.assertEquals('val3', self.redis.redis['test_list'][3])

    def test_lrem(self):
        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', 0, 'val1')
        self.assertListEqual(['val2', 'val3', 'val4', 'val2'], self.redis.redis['test_list'])

        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', 1, 'val2')
        self.assertListEqual(['val1', 'val1', 'val3', 'val4', 'val2'],
                             self.redis.redis['test_list'])

        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', 100, 'val1')
        self.assertListEqual(['val2', 'val3', 'val4', 'val2'], self.redis.redis['test_list'])

        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', -1, 'val3')
        self.assertListEqual(['val1', 'val2', 'val1', 'val4', 'val2'],
                             self.redis.redis['test_list'])

        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', -1, 'val2')
        self.assertListEqual(['val1', 'val2', 'val1', 'val3', 'val4'],
                             self.redis.redis['test_list'])

        self.redis.redis['test_list'] = ['val1', 'val2', 'val1', 'val3', 'val4', 'val2']
        self.redis.lrem('test_list', -2, 'val2')
        self.assertListEqual(['val1', 'val1', 'val3', 'val4'], self.redis.redis['test_list'])
Пример #6
0
class TestList(TestCase):
    """
    Tests for MockRedis list operations
    """

    def setUp(self):
        self.redis = MockRedis()
        self.redis.flushdb()

    def test_initially_empty(self):
        """
        List is created empty.
        """
        self.assertEqual(0, len(self.redis.redis['test_list']))

    def test_llen(self):
        self.assertEquals(0, self.redis.llen('test_list'))
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals(2, self.redis.llen('test_list'))
        self.redis.redis['test_list'].pop(0)
        self.assertEquals(1, self.redis.llen('test_list'))
        self.redis.redis['test_list'].pop(0)
        self.assertEquals(0, self.redis.llen('test_list'))

    def test_lpop(self):
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals('val1', self.redis.lpop('test_list'))
        self.assertEquals(1, len(self.redis.redis['test_list']))
        self.assertEquals('val2', self.redis.lpop('test_list'))
        self.assertEquals(0, len(self.redis.redis['test_list']))
        self.assertIsNone(self.redis.lpop('test_list'))

    def test_lpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.lpush('test_list', 'val1')
        self.redis.lpush('test_list', 'val2')

        # validate insertion
        self.assertEquals(2, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val2', self.redis.redis['test_list'][0])
        self.assertEquals('val1', self.redis.redis['test_list'][1])

        # insert two more values with one repeated
        self.redis.lpush('test_list', 'val1', 'val3')

        # validate the update
        self.assertEquals(4, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val3', self.redis.redis['test_list'][0])
        self.assertEquals('val1', self.redis.redis['test_list'][1])
        self.assertEquals('val2', self.redis.redis['test_list'][2])
        self.assertEquals('val1', self.redis.redis['test_list'][3])

    def test_rpop(self):
        self.redis.redis['test_list'] = ['val1', 'val2']
        self.assertEquals('val2', self.redis.rpop('test_list'))
        self.assertEquals(1, len(self.redis.redis['test_list']))
        self.assertEquals('val1', self.redis.rpop('test_list'))
        self.assertEquals(0, len(self.redis.redis['test_list']))
        self.assertIsNone(self.redis.lpop('test_list'))

    def test_rpush(self):
        """
        Insertion maintains order but not uniqueness.
        """
        # lpush two values
        self.redis.rpush('test_list', 'val1')
        self.redis.rpush('test_list', 'val2')

        # validate insertion
        self.assertEquals(2, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val1', self.redis.redis['test_list'][0])
        self.assertEquals('val2', self.redis.redis['test_list'][1])

        # insert two more values with one repeated
        self.redis.rpush('test_list', 'val1', 'val3')

        # validate the update
        self.assertEquals(4, len(self.redis.redis['test_list']))
        self.assertEquals('list', self.redis.type('test_list'))
        self.assertEquals('val1', self.redis.redis['test_list'][0])
        self.assertEquals('val2', self.redis.redis['test_list'][1])
        self.assertEquals('val1', self.redis.redis['test_list'][2])
        self.assertEquals('val3', self.redis.redis['test_list'][3])