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.
 
 
 

211 lines
5.4 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. "strconv"
  18. "testing"
  19. "cloud.google.com/go/internal/pretty"
  20. "cloud.google.com/go/internal/testutil"
  21. "github.com/google/go-cmp/cmp"
  22. bq "google.golang.org/api/bigquery/v2"
  23. )
  24. type testSaver struct {
  25. row map[string]Value
  26. insertID string
  27. err error
  28. }
  29. func (ts testSaver) Save() (map[string]Value, string, error) {
  30. return ts.row, ts.insertID, ts.err
  31. }
  32. func TestNewInsertRequest(t *testing.T) {
  33. prev := randomIDFn
  34. n := 0
  35. randomIDFn = func() string { n++; return strconv.Itoa(n) }
  36. defer func() { randomIDFn = prev }()
  37. tests := []struct {
  38. ul *Uploader
  39. savers []ValueSaver
  40. req *bq.TableDataInsertAllRequest
  41. }{
  42. {
  43. ul: &Uploader{},
  44. savers: nil,
  45. req: nil,
  46. },
  47. {
  48. ul: &Uploader{},
  49. savers: []ValueSaver{
  50. testSaver{row: map[string]Value{"one": 1}},
  51. testSaver{row: map[string]Value{"two": 2}},
  52. },
  53. req: &bq.TableDataInsertAllRequest{
  54. Rows: []*bq.TableDataInsertAllRequestRows{
  55. {InsertId: "1", Json: map[string]bq.JsonValue{"one": 1}},
  56. {InsertId: "2", Json: map[string]bq.JsonValue{"two": 2}},
  57. },
  58. },
  59. },
  60. {
  61. ul: &Uploader{
  62. TableTemplateSuffix: "suffix",
  63. IgnoreUnknownValues: true,
  64. SkipInvalidRows: true,
  65. },
  66. savers: []ValueSaver{
  67. testSaver{insertID: "a", row: map[string]Value{"one": 1}},
  68. testSaver{insertID: "", row: map[string]Value{"two": 2}},
  69. },
  70. req: &bq.TableDataInsertAllRequest{
  71. Rows: []*bq.TableDataInsertAllRequestRows{
  72. {InsertId: "a", Json: map[string]bq.JsonValue{"one": 1}},
  73. {InsertId: "3", Json: map[string]bq.JsonValue{"two": 2}},
  74. },
  75. TemplateSuffix: "suffix",
  76. SkipInvalidRows: true,
  77. IgnoreUnknownValues: true,
  78. },
  79. },
  80. }
  81. for i, tc := range tests {
  82. got, err := tc.ul.newInsertRequest(tc.savers)
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. want := tc.req
  87. if !testutil.Equal(got, want) {
  88. t.Errorf("%d: %#v: got %#v, want %#v", i, tc.ul, got, want)
  89. }
  90. }
  91. }
  92. func TestNewInsertRequestErrors(t *testing.T) {
  93. var u Uploader
  94. _, err := u.newInsertRequest([]ValueSaver{testSaver{err: errors.New("bang")}})
  95. if err == nil {
  96. t.Error("got nil, want error")
  97. }
  98. }
  99. func TestHandleInsertErrors(t *testing.T) {
  100. rows := []*bq.TableDataInsertAllRequestRows{
  101. {InsertId: "a"},
  102. {InsertId: "b"},
  103. }
  104. for _, test := range []struct {
  105. in []*bq.TableDataInsertAllResponseInsertErrors
  106. want error
  107. }{
  108. {
  109. in: nil,
  110. want: nil,
  111. },
  112. {
  113. in: []*bq.TableDataInsertAllResponseInsertErrors{{Index: 1}},
  114. want: PutMultiError{RowInsertionError{InsertID: "b", RowIndex: 1}},
  115. },
  116. {
  117. in: []*bq.TableDataInsertAllResponseInsertErrors{{Index: 1}},
  118. want: PutMultiError{RowInsertionError{InsertID: "b", RowIndex: 1}},
  119. },
  120. {
  121. in: []*bq.TableDataInsertAllResponseInsertErrors{
  122. {Errors: []*bq.ErrorProto{{Message: "m0"}}, Index: 0},
  123. {Errors: []*bq.ErrorProto{{Message: "m1"}}, Index: 1},
  124. },
  125. want: PutMultiError{
  126. RowInsertionError{InsertID: "a", RowIndex: 0, Errors: []error{&Error{Message: "m0"}}},
  127. RowInsertionError{InsertID: "b", RowIndex: 1, Errors: []error{&Error{Message: "m1"}}},
  128. },
  129. },
  130. } {
  131. got := handleInsertErrors(test.in, rows)
  132. if !testutil.Equal(got, test.want) {
  133. t.Errorf("%#v:\ngot\n%#v\nwant\n%#v", test.in, got, test.want)
  134. }
  135. }
  136. }
  137. func TestValueSavers(t *testing.T) {
  138. ts := &testSaver{}
  139. type T struct{ I int }
  140. schema, err := InferSchema(T{})
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. for _, test := range []struct {
  145. in interface{}
  146. want []ValueSaver
  147. }{
  148. {[]interface{}(nil), nil},
  149. {[]interface{}{}, nil},
  150. {ts, []ValueSaver{ts}},
  151. {T{I: 1}, []ValueSaver{&StructSaver{Schema: schema, Struct: T{I: 1}}}},
  152. {[]ValueSaver{ts, ts}, []ValueSaver{ts, ts}},
  153. {[]interface{}{ts, ts}, []ValueSaver{ts, ts}},
  154. {[]T{{I: 1}, {I: 2}}, []ValueSaver{
  155. &StructSaver{Schema: schema, Struct: T{I: 1}},
  156. &StructSaver{Schema: schema, Struct: T{I: 2}},
  157. }},
  158. {[]interface{}{T{I: 1}, &T{I: 2}}, []ValueSaver{
  159. &StructSaver{Schema: schema, Struct: T{I: 1}},
  160. &StructSaver{Schema: schema, Struct: &T{I: 2}},
  161. }},
  162. {&StructSaver{Struct: T{I: 3}, InsertID: "foo"},
  163. []ValueSaver{
  164. &StructSaver{Schema: schema, Struct: T{I: 3}, InsertID: "foo"},
  165. }},
  166. } {
  167. got, err := valueSavers(test.in)
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. if !testutil.Equal(got, test.want, cmp.AllowUnexported(testSaver{})) {
  172. t.Errorf("%+v: got %v, want %v", test.in, pretty.Value(got), pretty.Value(test.want))
  173. }
  174. // Make sure Save is successful.
  175. for i, vs := range got {
  176. _, _, err := vs.Save()
  177. if err != nil {
  178. t.Fatalf("%+v, #%d: got error %v, want nil", test.in, i, err)
  179. }
  180. }
  181. }
  182. }
  183. func TestValueSaversErrors(t *testing.T) {
  184. inputs := []interface{}{
  185. nil,
  186. 1,
  187. []int{1, 2},
  188. []interface{}{
  189. testSaver{row: map[string]Value{"one": 1}, insertID: "a"},
  190. 1,
  191. },
  192. StructSaver{},
  193. }
  194. for _, in := range inputs {
  195. if _, err := valueSavers(in); err == nil {
  196. t.Errorf("%#v: got nil, want error", in)
  197. }
  198. }
  199. }