示例#1
0
 def test_get_api_data_handles_error(self, mock_dal):
     print(
         "======================== Test get_api_data function handles error =================="
     )
     mock_dal.return_value.status_code = 500
     self.result = get_api_data(None, None, None)
     assert self.result["statusCode"] == 500
示例#2
0
 def test_get_api_data_handles_non_200(self, mock_dal):
     print(
         "======================== Test get_api_data function handles 401/non 200 responses =================="
     )
     mock_dal.return_value.status_code = 401
     self.result = get_api_data(None, None, None)
     assert self.result["statusCode"] == 401
示例#3
0
 def test_get_api_logging_on_error(self, mock_dal, mock_logger):
     print(
         "======================== Test get_api_data function logs errors =================="
     )
     mock_dal.return_value.status_code = 500
     self.result = get_api_data(None, None, None)
     mock_logger.error.assert_called_with(
         "Error calling %s, response code was: %s", None, 500)
示例#4
0
 def test_get_api_data(self, mock_dal):
     print(
         "======================== Test get_api_data function =================="
     )
     mock_dal.return_value.status_code = 200
     self.result = get_api_data(DAL_URL["base_url"],
                                TEST_DATA["shifter_api_params"],
                                DAL_URL["token"])
     assert self.result["statusCode"] == 200
示例#5
0
def sqs(body):
    """ This function used to call dal api and save response into mongo db according
    to request params.

    Notes: 'This function use helper function to save shift and shifter data

    Args:
    body (dict): required for call dal api.

    Returns:
    dict: The return value. """

    # initialize return value.
    return_value = {"statusCode": None, "body": None}

    try:
        # Initialize request body to call dal api.
        request_body = {"ID": body['Id'], "Type": body['nType']}
        # To parse as string and log the request body comming from sqs.
        request_body_string = str(body)
        logger.debug("Request body from sqs %s", request_body_string)
        # To call dal api and store response in result variable.
        result = get_api_data(DAL_URL['base_url'], request_body,
                              DAL_URL["token"])
        # To check if dal api call success.
        if result["statusCode"] == 200:
            logger.info("DAL api called success for type %s", body['nType'])
            data = result["body"].json()
            if data['data']:
                data = data['data'][0]
                logger.info("Get data from dal api succeeded")
                # To switch according to type.
                if body['nType'] == 'shifter':
                    # Call shifter function to save shifter data.
                    response = save_shifter(data)
                    return_value = response
                elif body['nType'] == 'shift':
                    # Call shift function to save shift data.
                    response = save_shift(data)
                    return_value = response
            else:
                logger.error("Response %s %s.", data["status"],
                             data["message"])
                return_value["statusCode"] = data['status']
                return_value["body"] = data['message']
        else:
            logger.error("Error during call dal api!")
            return_value = result
    except BaseException as exceptions:
        message = str(exceptions)
        logger.error("Error ! %s", message)
        return_value['statusCode'] = 500
        return_value['body'] = message
    return return_value
示例#6
0
 def test_get_api_logging_on_success(self, mock_dal, mock_logger):
     print(
         "======================== Test get_api_data function logs info =================="
     )
     mock_dal.return_value.status_code = 200
     self.result = get_api_data(DAL_URL["base_url"],
                                TEST_DATA["shifter_api_params"],
                                DAL_URL["token"])
     mock_logger.info.assert_called_with(
         "Successfully called %s with response code of %s",
         DAL_URL["base_url"], 200)
def get_shifter_volatile_data(shifters, vacated_params):
    """ This function is used to get shifters voatile data from dal api.

    Args:
    shifters (List): List of filtered shifter data.

    Returns:
    dict: The return value. """
    # Initialize return value.
    return_value = {"statusCode": None, "message": None, "body": {}}
    try:
        # Initialize request body for call dal api type: "qualify".
        shifter_ids_list = []
        for shifter in shifters:
            shifter_ids_list.append(shifter["shifterid"])
        shifter_ids = ", ".join(shifter_ids_list)
        request_body = {
            "ID": shifter_ids,
            "Type": "qualify",
            "VacatedShiftDate": vacated_params["startTime"]
        }
        result = get_api_data(DAL_URL['base_url'], request_body,
                              DAL_URL["token"])
        # To check if dal api call success.
        if result["statusCode"] == 200:
            logger.info("DAL api called successfully for type qualify.")
            data = result["body"].json()
            # To check data found.
            if data["data"]:
                logger.info("Shifters volatile data get success.")
                return_value["statusCode"] = 200
                return_value["message"] = "Success"
                return_value["body"] = data["data"]
            else:
                logger.error("Shifters volatile data not found!")
                return_value["statusCode"] = data['status']
                return_value["message"] = "Shifters volatile data not found!"
                return_value["body"] = []
        else:
            logger.error("Error calling DAL http response was: %s",
                         result["statusCode"])
            return_value["statusCode"] = result["statusCode"]
            return_value["message"] = "Internal server error!"
            return_value["body"] = []
    except BaseException as exceptions:
        message = str(exceptions)
        logger.error("Error while gatting shifters volatile data %s", message)
        return_value["statusCode"] = 500
        return_value["message"] = "Internal server error!"
        return_value["body"] = []
    return return_value
示例#8
0
 def test_get_api_logs_exception(self, mock_logger):
     print(
         "======================== Test get_api_data function logs exception =================="
     )
     self.result = get_api_data(None, None, None)
     mock_logger.error.assert_called()
示例#9
0
 def test_get_api_handles_exception(self):
     print(
         "======================== Test get_api_data function handles exception =================="
     )
     self.result = get_api_data(None, None, None)
     assert self.result["statusCode"] == 500