def test_receive_messages_bad_params(make_stubber, make_queue, receive_count):
    """Test that trying to receive a number of messages that is too large or too small
    raises an exception."""
    sqs_stubber = make_stubber(message_wrapper.sqs.meta.client)
    queue = make_queue(sqs_stubber, message_wrapper.sqs)

    sqs_stubber.stub_receive_messages(
        queue.url, [], receive_count, 'InvalidParameterValue')

    with pytest.raises(ClientError):
        message_wrapper.receive_messages(queue, receive_count, 1)
def test_receive_messages_bad_params(sqs_stubber, queue_stub, receive_count):
    """Test that trying to receive a number of messages that is too large or too small
    raises an exception."""
    sqs_stubber.add_client_error('receive_message',
                                 expected_params={
                                     'QueueUrl': queue_stub.url,
                                     'MessageAttributeNames': ['All'],
                                     'MaxNumberOfMessages': receive_count,
                                     'WaitTimeSeconds': 1
                                 },
                                 service_error_code='InvalidParameterValue')

    with pytest.raises(ClientError):
        message_wrapper.receive_messages(queue_stub, receive_count, 1)
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_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)