0

I am trying to test the below function using unit test mocks but i am not able to do it since the return type from request get/post is of type <class 'requests.models.Response'>. please let me know how to mock this and unit test this below function:

def response_check(base_url, headers, schedule_name, response):
    """
    Function to get response from dataform API and check the job status
    """
    run_url = base_url + "/" + response.json()["id"]
    query_response = requests.get(run_url, headers=headers)
    while query_response.json()["status"] == "RUNNING":
        time.sleep(10)
        print("Dataform job running")
        query_response = requests.get(run_url, headers=headers)
        if query_response.json()["status"] in ["FAILED", "CANCELLED", "TIMED_OUT"]:
            raise AirflowException(
                f'Dataform task {schedule_name} has been {response.json()["status"]} for reason {response.json()["runLogUrl"]}'
            )
        print(query_response.json())
    return "Dataform job finished"

I need to check for different response status but i am not able to mock the return type of it

3
  • did you try @patch? Commented Apr 29, 2022 at 12:49
  • I have used patch for request.get but the return type is of class requests.models.Response; so how to mock up the return type of this class. Commented Apr 29, 2022 at 13:00
  • Use side_effect to provide a list of canned responses; the calls to requests.get will use them as they are made. Commented Apr 29, 2022 at 13:09

1 Answer 1

0

Please test using http module and the http codes. For e.g. success is http.HTTPStatus.OK

Not found is http.HTTPStatus.NOT_FOUND Bad input is http.HTTPStatus.BAD_REQUEST etc.

Sign up to request clarification or add additional context in comments.

1 Comment

I have used responses package which solved my problem: stackoverflow.com/questions/9559963/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.