class RedisQueueTestCase(unittest.TestCase):
    def setUp(self):
        self.redis_inst = mock.Mock()
        self.registry = mock.Mock()
        self.get_serializer = self.registry.get_request_serializer
        self.serializer = self.get_serializer.return_value
        self.queue = RedisQueue("name1", self.redis_inst, self.registry)

    def test_append(self):
        request = object()
        dump = self.serializer.dumps.return_value
        self.queue.append(request)
        assert self.serializer.dumps.call_count == 1
        assert self.serializer.dumps.mock_calls[0][1] == (request,)
        assert self.redis_inst.rpush.call_count == 1
        assert self.redis_inst.rpush.mock_calls[0][1] == ("name1", dump)

    def test_pop(self):
        self.queue.pop()
        assert self.redis_inst.lpop.call_count == 1
        assert self.redis_inst.lpop.mock_calls[0][1] == ("name1",)

    def test_top(self):
        dump = object()
        obj = object()
        self.redis_inst.lrange.return_value = [dump]
        self.serializer.loads.return_value = obj
        retval = self.queue.top()
        assert retval == obj
        assert self.redis_inst.lrange.call_count == 1
        assert self.redis_inst.lrange.mock_calls[0][1] == ("name1", 0, 0)
        assert self.serializer.loads.call_count == 1
        assert self.serializer.loads.mock_calls[0][1] == (dump,)

    def test_nonzero(self):
        self.redis_inst.llen.return_value = 10
        assert bool(self.queue)
        assert self.redis_inst.llen.call_count == 1
        assert self.redis_inst.llen.mock_calls[0][1] == ("name1",)

        self.redis_inst.llen.return_value = 0
        assert not bool(self.queue)
        assert self.redis_inst.llen.call_count == 2
        assert self.redis_inst.llen.mock_calls[1][1] == ("name1",)
 def setUp(self):
     self.redis_inst = mock.Mock()
     self.registry = mock.Mock()
     self.get_serializer = self.registry.get_request_serializer
     self.serializer = self.get_serializer.return_value
     self.queue = RedisQueue("name1", self.redis_inst, self.registry)