You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

110 lines
2.6 KiB

  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. package bigquery
  15. import (
  16. "errors"
  17. "strings"
  18. "testing"
  19. "cloud.google.com/go/internal/testutil"
  20. bq "google.golang.org/api/bigquery/v2"
  21. )
  22. func rowInsertionError(msg string) RowInsertionError {
  23. return RowInsertionError{Errors: []error{errors.New(msg)}}
  24. }
  25. func TestPutMultiErrorString(t *testing.T) {
  26. testCases := []struct {
  27. errs PutMultiError
  28. want string
  29. }{
  30. {
  31. errs: PutMultiError{},
  32. want: "0 row insertions failed",
  33. },
  34. {
  35. errs: PutMultiError{rowInsertionError("a")},
  36. want: "1 row insertion failed",
  37. },
  38. {
  39. errs: PutMultiError{rowInsertionError("a"), rowInsertionError("b")},
  40. want: "2 row insertions failed",
  41. },
  42. }
  43. for _, tc := range testCases {
  44. if tc.errs.Error() != tc.want {
  45. t.Errorf("PutMultiError string: got:\n%v\nwant:\n%v", tc.errs.Error(), tc.want)
  46. }
  47. }
  48. }
  49. func TestMultiErrorString(t *testing.T) {
  50. testCases := []struct {
  51. errs MultiError
  52. want string
  53. }{
  54. {
  55. errs: MultiError{},
  56. want: "(0 errors)",
  57. },
  58. {
  59. errs: MultiError{errors.New("a")},
  60. want: "a",
  61. },
  62. {
  63. errs: MultiError{errors.New("a"), errors.New("b")},
  64. want: "a (and 1 other error)",
  65. },
  66. {
  67. errs: MultiError{errors.New("a"), errors.New("b"), errors.New("c")},
  68. want: "a (and 2 other errors)",
  69. },
  70. }
  71. for _, tc := range testCases {
  72. if tc.errs.Error() != tc.want {
  73. t.Errorf("PutMultiError string: got:\n%v\nwant:\n%v", tc.errs.Error(), tc.want)
  74. }
  75. }
  76. }
  77. func TestErrorFromErrorProto(t *testing.T) {
  78. for _, test := range []struct {
  79. in *bq.ErrorProto
  80. want *Error
  81. }{
  82. {nil, nil},
  83. {
  84. in: &bq.ErrorProto{Location: "L", Message: "M", Reason: "R"},
  85. want: &Error{Location: "L", Message: "M", Reason: "R"},
  86. },
  87. } {
  88. if got := bqToError(test.in); !testutil.Equal(got, test.want) {
  89. t.Errorf("%v: got %v, want %v", test.in, got, test.want)
  90. }
  91. }
  92. }
  93. func TestErrorString(t *testing.T) {
  94. e := &Error{Location: "<L>", Message: "<M>", Reason: "<R>"}
  95. got := e.Error()
  96. if !strings.Contains(got, "<L>") || !strings.Contains(got, "<M>") || !strings.Contains(got, "<R>") {
  97. t.Errorf(`got %q, expected to see "<L>", "<M>" and "<R>"`, got)
  98. }
  99. }