def test_upload_csv_success(self): # connect s3 resource_s3 = s3_utils.resource_connect() check_bucket = common_utils.get_environ("S3_CHECK_BUCKET") # prepare data resource_s3.create_bucket(Bucket=check_bucket) with patch.object(PmLogAdapter, 'info', return_value=None) as mock_method_info: with patch.object(boto3, 'client') as mock_method_client: mock_method_client.return_value = client_s3 with patch.object(client_s3, 'put_object') as mock_method: # call function test FileUtils.upload_csv(trace_id, "S3_CHECK_BUCKET", data_file, s3_file_name) # check write log info mock_method_info.assert_any_call( "Upload file csv [%s] success on bucket [%s]", s3_file_name, check_bucket) # check connect client mock_method_client.assert_called_with('s3') # check param call function put_object mock_method.assert_any_call(Body=data_file, Bucket=check_bucket, Key=s3_file_name)
def test_upload_csv_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.upload_csv(trace_id, "S3_CHECK_BUCKET", data_file, 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_upload_csv_error_call_put_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, 'put_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.upload_csv(trace_id, "S3_CHECK_BUCKET", data_file, 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)