def test_read_object_success(self): data_file = copy.deepcopy(DataYaml.DATA_FILE_YAML) # prepare data check_bucket = common_utils.get_environ("S3_CHECK_BUCKET") client_s3.create_bucket(Bucket=check_bucket) client_s3.put_object(Body=data_file, Bucket=check_bucket, Key=s3_file_name) object_file = client_s3.get_object(Bucket=check_bucket, Key=s3_file_name) object_file_copy = copy.deepcopy(object_file) with patch.object(boto3, 'client') as mock_method_client: mock_method_client.return_value = client_s3 with patch.object(client_s3, 'get_object') as mock_method: mock_method.return_value = object_file # call function test actual_response = FileUtils.read_object( trace_id, 'S3_CHECK_BUCKET', s3_file_name) # check result expected_result = object_file_copy['Body'].read() self.assertEqual(expected_result, actual_response) # check connect client mock_method_client.assert_called_with('s3') # check param call function get_object mock_method.assert_any_call(Bucket=check_bucket, Key=s3_file_name)
def test_read_object_error_client(self): # create mock error client s3 self.create_mock_boto3_client_error() expected_error_response = copy.deepcopy(DataCommon.ERROR_RESPONSE) expected_operation_name = copy.deepcopy(DataCommon.OPERATION_NAME) FileUtils.global_s3_client = None with self.assertRaises(PmError) as exception: # call function test FileUtils.read_object(trace_id, 'S3_CHECK_BUCKET', s3_file_name) # check error actual_cause_error = exception.exception.cause_error self.assertEqual(expected_error_response['Error'], actual_cause_error.response['Error']) self.assertEqual(expected_operation_name, actual_cause_error.operation_name)
def test_read_object_error_call_get_object(self): # create mock throw error when called function put_object expected_error_response = copy.deepcopy(DataCommon.ERROR_RESPONSE) expected_operation_name = copy.deepcopy(DataCommon.OPERATION_NAME) with patch.object(client_s3, 'get_object') as mock_method: mock_method.side_effect = ClientError( error_response=expected_error_response, operation_name=expected_operation_name) with self.assertRaises(PmError) as exception: # call function test FileUtils.read_object(trace_id, 'S3_CHECK_BUCKET', s3_file_name) # check error actual_cause_error = exception.exception.cause_error self.assertEqual(expected_error_response['Error'], actual_cause_error.response['Error']) self.assertEqual(expected_operation_name, actual_cause_error.operation_name)