lundi 22 juillet 2019

Python Mock function instead a class

I have the following class

class SqsClient:
    def __init__(self):
        self._sqs_client = boto3.client('sqs', region_name='us-west-2')
        self._sqs_resource = boto3.resource('sqs', region_name='us-west-2')

    def push_to_sqs(self, queue_url, payload):
        response = self._sqs_client.send_message(
            QueueUrl=queue_url,
            DelaySeconds=1,
            MessageBody=json.dumps(payload)
        )
        logger.info("SQS MESSAGE: {}".format(response))

This class is a field in my handler_client

The function I wish to test is

def notify_sqs(self, successful):
    self.construct_sqs_payload()
    queue_url = None
    payload = self._pay_load
    if successful:
        queue_url = "test_url"
    else:
        queue_url = "second_test_url"
    self._sqs_client.push_to_sqs(queue_url, payload)

I want to mock the call to push_to_sqs and just assert that it is called once, and called with the correct payload and url.

Here is my unit test

@pytest.mark.usefixtures("test_handler_client", "expected_sqs_payload", "mock_handler_client_env")
def test_notify_sqs(test_handler_client, expected_sqs_payload, mock_handler_client_env):
    with mock.patch("src.clients.sqs_client.SqsClient.push_to_sqs")as mocked_sqs:
        test_handler_client.notify_sqs(successful=True)
        mocked_sqs.assert_called_once()

The error is the class has no attribute. How do I properly mock this function so that I can perform assert_called_once_with?

Aucun commentaire:

Enregistrer un commentaire