def test_yield_messages_from_queue_no_messages(self): """Assert that yield_messages_from_queue breaks when no messages.""" queue_url = _faker.url() with patch.object(sqs, "boto3") as mock_boto3: mock_resource = mock_boto3.resource.return_value mock_queue = mock_resource.Queue.return_value mock_queue.receive_messages.side_effect = [None] yielded_messages = [] for message in sqs.yield_messages_from_queue(queue_url): yielded_messages.append(message) self.assertEqual(yielded_messages, [])
def test_yield_messages_from_queue_not_exists(self): """Assert yield_messages_from_queue handles a nonexistent queue.""" queue_url = _faker.url() error_response = {"Error": {"Code": ".NonExistentQueue"}} exception = ClientError(error_response, Mock()) with self.assertLogs( "util.aws.sqs", level="WARNING" ) as logging_watcher, patch.object(sqs, "boto3") as mock_boto3: mock_resource = mock_boto3.resource.return_value mock_queue = mock_resource.Queue.return_value mock_queue.receive_messages.side_effect = exception yielded_messages = list(sqs.yield_messages_from_queue(queue_url)) self.assertEqual(len(yielded_messages), 0) self.assertIn("Queue does not yet exist at", logging_watcher.output[0])
def test_yield_messages_from_queue(self): """Assert that yield_messages_from_queue yields messages.""" queue_url = _faker.url() available_messages = [Mock(), Mock(), Mock()] with patch.object(sqs, "boto3") as mock_boto3: mock_resource = mock_boto3.resource.return_value mock_queue = mock_resource.Queue.return_value mock_queue.receive_messages.side_effect = [ [available_messages[0]], [available_messages[1]], [available_messages[2]], ] yielded_messages = [] for message in sqs.yield_messages_from_queue(queue_url): yielded_messages.append(message) self.assertEqual(yielded_messages, available_messages)
def test_yield_messages_from_queue_other_client_error(self): """Assert yield_messages_from_queue logs and raises other ClientError.""" queue_url = _faker.url() error_response = {"Error": {"Code": ".Potatoes"}} exception = ClientError(error_response, Mock()) with self.assertLogs( "util.aws.sqs", level="ERROR" ) as logging_watcher, patch.object(sqs, "boto3") as mock_boto3: mock_resource = mock_boto3.resource.return_value mock_queue = mock_resource.Queue.return_value mock_queue.receive_messages.side_effect = exception with self.assertRaises(ClientError) as raised_exception: list(sqs.yield_messages_from_queue(queue_url)) self.assertEqual(raised_exception.exception, exception) self.assertIn( "Unexpected ClientError when receiving message from SQS", logging_watcher.output[0], )
def test_yield_messages_from_queue_raises_unhandled_exception(self): """Assert yield_messages_from_queue logs and raises other exceptions.""" queue_url = _faker.url() class UnexpectedException(Exception): pass exception = UnexpectedException() with self.assertLogs( "util.aws.sqs", level="ERROR" ) as logging_watcher, patch.object(sqs, "boto3") as mock_boto3: mock_resource = mock_boto3.resource.return_value mock_queue = mock_resource.Queue.return_value mock_queue.receive_messages.side_effect = exception with self.assertRaises(UnexpectedException) as raised_exception: list(sqs.yield_messages_from_queue(queue_url)) self.assertEqual(raised_exception.exception, exception) self.assertIn( "Unexpected not-ClientError exception when receiving message from SQS", logging_watcher.output[0], )