def setUp(self): self.consumer = BusConsumer() self.consumer.channel = Mock() self.callback = Mock()
class TestBusConsumer(unittest.TestCase): def setUp(self): self.consumer = BusConsumer() self.consumer.channel = Mock() self.callback = Mock() @patch('pika.BlockingConnection') @patch('pika.ConnectionParameters') def test_when_connect_then_instantiate_connection_and_channel( self, parameters_mock, connection_mock): parameters = parameters_mock('localhost') self.consumer.connect() connection_mock.assert_called_once_with(parameters) def test_when_add_binding_then_channel_queue_bind(self): exchange = 'xivo-ami' key = '*' self.consumer.add_binding(self.callback, QUEUE_NAME, exchange, key) self.consumer.channel.queue_declare.assert_called_once_with( queue=QUEUE_NAME, exclusive=False, durable=True) self.consumer.channel.queue_bind.assert_called_once_with( queue=QUEUE_NAME, exchange=exchange, routing_key=key) def test_when_run_then_consume(self): self.consumer.callback = self.callback self.consumer.queue_name = QUEUE_NAME self.consumer.run() self.consumer.channel.basic_consume.assert_called_once_with( self.consumer.on_message, QUEUE_NAME) self.consumer.channel.start_consuming.assert_called_once_with() def test_rabbitmq_down_when_run_then_raise_busconnectionerror(self): self.consumer.channel.start_consuming.side_effect = [ AMQPConnectionError() ] self.consumer.callback = self.callback self.consumer.queue_name = QUEUE_NAME self.assertRaises(BusConnectionError, self.consumer.run) @patch('pika.BlockingConnection', Mock()) @patch('pika.ConnectionParameters', Mock()) def test_when_stop_then_stop_and_disconnect(self): self.consumer.connect() self.consumer.stop() self.consumer.channel.stop_consuming.assert_called_once_with() self.consumer.connection.close.assert_called_once_with() self.consumer.connection.close.assert_called_once_with() @patch('pika.BlockingConnection', Mock()) @patch('pika.ConnectionParameters', Mock()) def test_rabbitmq_down_when_stop_then_only_disconnect(self): self.consumer.connect() self.consumer.connection.is_open = False self.consumer.stop() assert_that(self.consumer.channel.stop_consuming.call_count, equal_to(0)) assert_that(self.consumer.connection.close.call_count, equal_to(0)) def test_when_on_message_then_unmarshal_callback_and_ack(self): channel = self.consumer.channel method = Mock(Basic.Deliver) method.delivery_tag = sentinel.tag linkedid = '1391789340.26' body = """ { "data": { "EventTime": "2014-02-07 11:09:03", "LinkedID": "%s", "UniqueID": "1391789340.26", "EventName": "LINKEDID_END" }, "name": "CEL" } """ % linkedid unmarshaled_body = { u'data': { u'EventName': u'LINKEDID_END', u'EventTime': u'2014-02-07 11:09:03', u'LinkedID': u'1391789340.26', u'UniqueID': u'1391789340.26' }, u'name': u'CEL' } self.consumer.callback = Mock() self.consumer.on_message(channel, method, sentinel.header, body) self.consumer.callback.assert_called_once_with(unmarshaled_body) channel.basic_ack.assert_called_once_with( delivery_tag=method.delivery_tag)
class TestBusConsumer(unittest.TestCase): def setUp(self): self.consumer = BusConsumer() self.consumer.channel = Mock() self.callback = Mock() @patch('pika.BlockingConnection') @patch('pika.ConnectionParameters') def test_when_connect_then_instantiate_connection_and_channel(self, parameters_mock, connection_mock): parameters = parameters_mock('localhost') self.consumer.connect() connection_mock.assert_called_once_with(parameters) def test_when_add_binding_then_channel_queue_bind(self): exchange = 'xivo-ami' key = '*' self.consumer.add_binding(self.callback, QUEUE_NAME, exchange, key) self.consumer.channel.queue_declare.assert_called_once_with(queue=QUEUE_NAME, exclusive=False, durable=True) self.consumer.channel.queue_bind.assert_called_once_with( queue=QUEUE_NAME, exchange=exchange, routing_key=key) def test_when_run_then_consume(self): self.consumer.callback = self.callback self.consumer.queue_name = QUEUE_NAME self.consumer.run() self.consumer.channel.basic_consume.assert_called_once_with( self.consumer.on_message, QUEUE_NAME) self.consumer.channel.start_consuming.assert_called_once_with() def test_rabbitmq_down_when_run_then_raise_busconnectionerror(self): self.consumer.channel.start_consuming.side_effect = [AMQPConnectionError()] self.consumer.callback = self.callback self.consumer.queue_name = QUEUE_NAME self.assertRaises(BusConnectionError, self.consumer.run) @patch('pika.BlockingConnection', Mock()) @patch('pika.ConnectionParameters', Mock()) def test_when_stop_then_stop_and_disconnect(self): self.consumer.connect() self.consumer.stop() self.consumer.channel.stop_consuming.assert_called_once_with() self.consumer.connection.close.assert_called_once_with() self.consumer.connection.close.assert_called_once_with() @patch('pika.BlockingConnection', Mock()) @patch('pika.ConnectionParameters', Mock()) def test_rabbitmq_down_when_stop_then_only_disconnect(self): self.consumer.connect() self.consumer.connection.is_open = False self.consumer.stop() assert_that(self.consumer.channel.stop_consuming.call_count, equal_to(0)) assert_that(self.consumer.connection.close.call_count, equal_to(0)) def test_when_on_message_then_unmarshal_callback_and_ack(self): channel = self.consumer.channel method = Mock(Basic.Deliver) method.delivery_tag = sentinel.tag linkedid = '1391789340.26' body = """ { "data": { "EventTime": "2014-02-07 11:09:03", "LinkedID": "%s", "UniqueID": "1391789340.26", "EventName": "LINKEDID_END" }, "name": "CEL" } """ % linkedid unmarshaled_body = {u'data': {u'EventName': u'LINKEDID_END', u'EventTime': u'2014-02-07 11:09:03', u'LinkedID': u'1391789340.26', u'UniqueID': u'1391789340.26'}, u'name': u'CEL'} self.consumer.callback = Mock() self.consumer.on_message(channel, method, sentinel.header, body) self.consumer.callback.assert_called_once_with(unmarshaled_body) channel.basic_ack.assert_called_once_with(delivery_tag=method.delivery_tag)