def test_send_messages_wrong_size(sqs_stubber, queue_stub, count): """Test that sending batches of messages that are too big or too small raises exceptions.""" messages = [{ 'body': f'Another body {ind}', 'attributes': {} } for ind in range(0, count)] sqs_stubber.add_client_error( 'send_message_batch', expected_params={ 'QueueUrl': queue_stub.url, 'Entries': [{ 'Id': str(ind), 'MessageBody': msg['body'], 'MessageAttributes': msg['attributes'] } for ind, msg in enumerate(messages)] }, service_error_code='AWS.SimpleQueueService.EmptyBatchRequest' if count == 0 else 'AWS.SimpleQueueService.TooManyEntriesInBatchRequest') if count == 0: with pytest.raises(sqs_stubber.client.exceptions.EmptyBatchRequest): message_wrapper.send_messages(queue_stub, messages) elif count > 10: with pytest.raises( sqs_stubber.client.exceptions.TooManyEntriesInBatchRequest): message_wrapper.send_messages(queue_stub, messages)
def test_send_messages_wrong_size(make_stubber, make_queue, count): """Test that sending batches of messages that are too big or too small raises exceptions.""" sqs_stubber = make_stubber(message_wrapper.sqs.meta.client) queue = make_queue(sqs_stubber, message_wrapper.sqs) messages = [{ 'body': f'Another body {ind}', 'attributes': {} } for ind in range(0, count)] sqs_stubber.stub_send_message_batch( queue.url, messages, 'AWS.SimpleQueueService.EmptyBatchRequest' if count == 0 else 'AWS.SimpleQueueService.TooManyEntriesInBatchRequest') with pytest.raises(ClientError) as exc_info: message_wrapper.send_messages(queue, messages) if count == 0: assert exc_info.value.response['Error']['Code'] == \ 'AWS.SimpleQueueService.EmptyBatchRequest' else: assert exc_info.value.response['Error']['Code'] == \ 'AWS.SimpleQueueService.TooManyEntriesInBatchRequest'
def test_receive_messages(make_stubber, make_queue, send_count, receive_count, wait_time): """Test that receiving various numbers of messages returns the expected number of messages.""" sqs_stubber = make_stubber(message_wrapper.sqs.meta.client) queue = make_queue(sqs_stubber, message_wrapper.sqs) sent_messages = [{ 'body': f"I have several bodies. This is #{ind}.", 'attributes': {} } for ind in range(0, send_count)] if send_count > 0: sqs_stubber.stub_send_message_batch(queue.url, sent_messages) message_wrapper.send_messages(queue, sent_messages) sqs_stubber.stub_receive_messages(queue.url, sent_messages, receive_count) received_messages = message_wrapper.receive_messages( queue, receive_count, wait_time) if send_count > 0: assert received_messages assert len(received_messages) <= receive_count else: assert not received_messages
def test_delete_messages(make_stubber, make_queue, message_count): """Test that deleting a single message or a batch of messages returns the expected success response.""" sqs_stubber = make_stubber(message_wrapper.sqs.meta.client) queue = make_queue(sqs_stubber, message_wrapper.sqs) body = "I'm not long for this world." wait_time = 1 messages = [{'body': body, 'attributes': {}}] * message_count sqs_stubber.stub_send_message_batch(queue.url, messages) sqs_stubber.stub_receive_messages(queue.url, messages, message_count) message_wrapper.send_messages(queue, messages) messages = message_wrapper.receive_messages(queue, message_count, wait_time) if message_count == 1: sqs_stubber.stub_delete_message(queue.url, messages[0]) messages[0].delete() else: sqs_stubber.stub_delete_message_batch(queue.url, messages, len(messages), 0) message_wrapper.delete_messages(queue, messages)
def test_receive_messages(sqs_stubber, queue_stub, send_count, receive_count, wait_time): """Test that receiving various numbers of messages returns the expected number of messages.""" sent_messages = [{ 'body': f"I have several bodies. This is #{ind}.", 'attributes': {} } for ind in range(0, send_count)] if send_count > 0: sqs_stubber.add_response('send_message_batch', expected_params={ 'QueueUrl': queue_stub.url, 'Entries': [{ 'Id': str(ind), 'MessageBody': msg['body'], 'MessageAttributes': msg['attributes'] } for ind, msg in enumerate(sent_messages)] }, service_response={ 'Successful': [{ 'Id': str(ind), 'MessageId': f'{ind}-1234', 'MD5OfMessageBody': 'Test-MD5-Body', } for ind in range(0, len(sent_messages))], 'Failed': [] }) message_wrapper.send_messages(queue_stub, sent_messages) sqs_stubber.add_response('receive_message', expected_params={ 'QueueUrl': queue_stub.url, 'MessageAttributeNames': ['All'], 'MaxNumberOfMessages': receive_count, 'WaitTimeSeconds': wait_time }, service_response={ 'Messages': [{ 'MessageId': f'{ind}-1234', 'Body': msg['body'], 'MD5OfBody': 'Test-MD5-Body', 'ReceiptHandle': f'Receipt-{ind}' } for ind, msg in enumerate(sent_messages) if ind < receive_count] }) received_messages = message_wrapper.receive_messages( queue_stub, receive_count, wait_time) if send_count > 0: assert received_messages assert len(received_messages) <= receive_count else: assert not received_messages
def test_send_messages(make_stubber, make_queue, body_template, attributes, count): """Test that sending various batches of messages returns the expected list of successful sends.""" sqs_stubber = make_stubber(message_wrapper.sqs.meta.client) queue = make_queue(sqs_stubber, message_wrapper.sqs) messages = [{ 'body': body_template.format(ind), 'attributes': attributes } for ind in range(1, count+1)] sqs_stubber.stub_send_message_batch(queue.url, messages) response = message_wrapper.send_messages(queue, messages) assert len(response['Successful']) == count
def test_send_messages(sqs_stubber, queue_stub, body_template, attributes, count): """Test that sending various batches of messages returns the expected list of successful sends.""" messages = [{ 'body': body_template.format(ind), 'attributes': attributes } for ind in range(1, count + 1)] sqs_stubber.add_response('send_message_batch', expected_params={ 'QueueUrl': queue_stub.url, 'Entries': [{ 'Id': str(ind), 'MessageBody': msg['body'], 'MessageAttributes': msg['attributes'] } for ind, msg in enumerate(messages)] }, service_response={ 'Successful': [{ 'Id': str(ind), 'MessageId': f'{ind}-1234', 'MD5OfMessageBody': 'Test-MD5-Body', } for ind in range(0, len(messages))], 'Failed': [] }) response = message_wrapper.send_messages(queue_stub, messages) assert len(response['Successful']) == count
def test_delete_messages(sqs_stubber, queue_stub, message_count): """Test that deleting a single message or a batch of messages returns the expected success response.""" body = "I'm not long for this world." wait_time = 1 sqs_stubber.add_response('send_message_batch', expected_params={ 'QueueUrl': queue_stub.url, 'Entries': [{ 'Id': str(ind), 'MessageBody': body, 'MessageAttributes': {} } for ind in range(0, message_count)] }, service_response={ 'Successful': [{ 'Id': str(ind), 'MessageId': f'{ind}-1234', 'MD5OfMessageBody': 'Test-MD5-Body', } for ind in range(0, message_count)], 'Failed': [] }) sqs_stubber.add_response('receive_message', expected_params={ 'QueueUrl': queue_stub.url, 'MessageAttributeNames': ['All'], 'MaxNumberOfMessages': message_count, 'WaitTimeSeconds': wait_time }, service_response={ 'Messages': [{ 'MessageId': f'{ind}-1234', 'Body': body, 'MD5OfBody': 'Test-MD5-Body', 'ReceiptHandle': f'Receipt-{ind}' } for ind in range(0, message_count)] }) message_wrapper.send_messages(queue_stub, [{ 'body': body, 'attributes': {} }] * message_count) messages = message_wrapper.receive_messages(queue_stub, message_count, wait_time) if message_count == 1: sqs_stubber.add_response('delete_message', expected_params={ 'QueueUrl': queue_stub.url, 'ReceiptHandle': messages[0].receipt_handle }, service_response={}) messages[0].delete() else: sqs_stubber.add_response('delete_message_batch', expected_params={ 'QueueUrl': queue_stub.url, 'Entries': [{ 'Id': str(ind), 'ReceiptHandle': msg.receipt_handle } for ind, msg in enumerate(messages)] }, service_response={ 'Successful': [{ 'Id': str(ind) } for ind in range(0, message_count)], 'Failed': [] }) message_wrapper.delete_messages(queue_stub, messages)