...

Source file src/cloud.google.com/go/bigquery/error_test.go

Documentation: cloud.google.com/go/bigquery

     1  // Copyright 2015 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bigquery
    16  
    17  import (
    18  	"errors"
    19  	"strings"
    20  	"testing"
    21  
    22  	"cloud.google.com/go/internal/testutil"
    23  	bq "google.golang.org/api/bigquery/v2"
    24  )
    25  
    26  func rowInsertionError(msg string) RowInsertionError {
    27  	return RowInsertionError{Errors: []error{errors.New(msg)}}
    28  }
    29  
    30  func TestPutMultiErrorString(t *testing.T) {
    31  	testCases := []struct {
    32  		errs PutMultiError
    33  		want string
    34  	}{
    35  		{
    36  			errs: PutMultiError{},
    37  			want: "0 row insertions failed",
    38  		},
    39  		{
    40  			errs: PutMultiError{rowInsertionError("a")},
    41  			want: "1 row insertion failed (insertion of row [insertID: \"\"; insertIndex: 0] failed with error: a)",
    42  		},
    43  		{
    44  			errs: PutMultiError{
    45  				rowInsertionError("1"),
    46  				rowInsertionError("2"),
    47  				rowInsertionError("3"),
    48  				rowInsertionError("4"),
    49  			},
    50  			want: "4 row insertions failed (insertion of row [insertID: \"\"; insertIndex: 0] failed with error: 1, insertion of row [insertID: \"\"; insertIndex: 0] failed with error: 2, insertion of row [insertID: \"\"; insertIndex: 0] failed with error: 3, ...)",
    51  		},
    52  	}
    53  
    54  	for _, tc := range testCases {
    55  		if tc.errs.Error() != tc.want {
    56  			t.Errorf("PutMultiError string: got:\n%v\nwant:\n%v", tc.errs.Error(), tc.want)
    57  		}
    58  	}
    59  }
    60  
    61  func TestMultiErrorString(t *testing.T) {
    62  	testCases := []struct {
    63  		errs MultiError
    64  		want string
    65  	}{
    66  		{
    67  			errs: MultiError{},
    68  			want: "(0 errors)",
    69  		},
    70  		{
    71  			errs: MultiError{errors.New("a")},
    72  			want: "a",
    73  		},
    74  		{
    75  			errs: MultiError{errors.New("a"), errors.New("b")},
    76  			want: "a (and 1 other error)",
    77  		},
    78  		{
    79  			errs: MultiError{errors.New("a"), errors.New("b"), errors.New("c")},
    80  			want: "a (and 2 other errors)",
    81  		},
    82  	}
    83  
    84  	for _, tc := range testCases {
    85  		if tc.errs.Error() != tc.want {
    86  			t.Errorf("PutMultiError string: got:\n%v\nwant:\n%v", tc.errs.Error(), tc.want)
    87  		}
    88  	}
    89  }
    90  
    91  func TestErrorFromErrorProto(t *testing.T) {
    92  	for _, test := range []struct {
    93  		in   *bq.ErrorProto
    94  		want *Error
    95  	}{
    96  		{nil, nil},
    97  		{
    98  			in:   &bq.ErrorProto{Location: "L", Message: "M", Reason: "R"},
    99  			want: &Error{Location: "L", Message: "M", Reason: "R"},
   100  		},
   101  	} {
   102  		if got := bqToError(test.in); !testutil.Equal(got, test.want) {
   103  			t.Errorf("%v: got %v, want %v", test.in, got, test.want)
   104  		}
   105  	}
   106  }
   107  
   108  func TestErrorString(t *testing.T) {
   109  	e := &Error{Location: "<L>", Message: "<M>", Reason: "<R>"}
   110  	got := e.Error()
   111  	if !strings.Contains(got, "<L>") || !strings.Contains(got, "<M>") || !strings.Contains(got, "<R>") {
   112  		t.Errorf(`got %q, expected to see "<L>", "<M>" and "<R>"`, got)
   113  	}
   114  }
   115  

View as plain text