def test_on_consume_ok(self): """ Verify that the correct actions follows a consume OK. """ # Prep on_message_callback_called = False def on_message_callback(msg): if not isinstance(msg, ConsumeOK): return nonlocal on_message_callback_called on_message_callback_called = True consume_params = ConsumeParams(on_message_callback) queue_params = QueueParams("queue") frame = Mock() self.consumer.consume(consume_params, queue_params=queue_params) # Run test self.consumer.on_consume_ok(frame, queue_params=queue_params) # Assertions consume_instance = self.consumer._consumes[ _gen_consume_key(queue=queue_params.queue) ] self.assertEqual(consume_instance.consumer_tag, frame.method.consumer_tag) self.assertTrue(on_message_callback_called) # Called with ConsumeOK
def test_consume_queue_exchange_and_routing_key(self): """ Verify possibility to provide both an exchange, routing_key and a queue to the consume method. """ # Prep def on_msg(): pass consume = ConsumeParams(on_msg) queue = QueueParams("queue") exchange = ExchangeParams("exchange") # Run test self.consumer.consume(consume, queue_params=queue, exchange_params=exchange, routing_key="routing_key") # Assertions consume_key = _gen_consume_key(queue=queue.queue, exchange=exchange.exchange, routing_key="routing_key") consume_instance = self.consumer._consumes[consume_key] self.assertEqual(consume_instance.consume_params, consume) self.assertEqual(consume_instance.queue_params, queue) self.assertEqual(consume_instance.exchange_params, exchange) self.assertEqual(consume_instance.routing_key, "routing_key") self.consumer.declare_queue.assert_called_with(queue, callback=ANY)
def test_consume_queue_only(self): """Verify possibility to consume from a queue.""" # Prep def on_msg(): pass consume = ConsumeParams(on_msg) queue = QueueParams("queue") # Run test self.consumer.consume(consume, queue_params=queue) # # Assertions consume_key = _gen_consume_key(queue=queue.queue) consume_instance = self.consumer._consumes[consume_key] self.assertEqual(consume_instance.consume_params, consume) self.assertEqual(consume_instance.queue_params, queue) self.assertEqual(consume_instance.exchange_params, None) self.consumer.declare_queue.assert_called_with(queue, callback=ANY)
def test_consume_exchange_only(self): """Verify possibility to consume from an exchange.""" # Prep def on_msg(): pass consume = ConsumeParams(on_msg) exchange = ExchangeParams("exchange") # Run test self.consumer.consume(consume, exchange_params=exchange) # # Assertions # Verify consume instance exists in consumer instance consume_key = _gen_consume_key(exchange=exchange.exchange) consume_instance = self.consumer._consumes[consume_key] self.assertEqual(consume_instance.consume_params, consume) self.assertEqual(consume_instance.exchange_params, exchange) # Always starts with queue declare, can't verify call params since # queue is autogenerated if not provided, and the other arg is a # callback constructed from functools.partial. self.consumer.declare_queue.assert_called()
def test_consume_key_gen(self): """Verify the consume key gen works as intended""" self.assertEqual( "queue|exchange|routing_key", _gen_consume_key( "queue", "exchange", "routing_key" ) ) self.assertEqual( "exchange|routing_key", _gen_consume_key( None, "exchange", "routing_key" ) ) self.assertEqual( "queue|routing_key", _gen_consume_key( "queue", None, "routing_key" ) ) self.assertEqual( "queue|exchange|complex.routing.key", _gen_consume_key( "queue", "exchange", "complex.routing.key" ) ) self.assertEqual( "exchange", _gen_consume_key( "", "exchange", None ) ) self.assertEqual( "queue|exchange", _gen_consume_key( "queue", "exchange", None ) )