load_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  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. "reflect"
  17. "testing"
  18. "golang.org/x/net/context"
  19. bq "google.golang.org/api/bigquery/v2"
  20. )
  21. func defaultLoadJob() *bq.Job {
  22. return &bq.Job{
  23. Configuration: &bq.JobConfiguration{
  24. Load: &bq.JobConfigurationLoad{
  25. DestinationTable: &bq.TableReference{
  26. ProjectId: "project-id",
  27. DatasetId: "dataset-id",
  28. TableId: "table-id",
  29. },
  30. SourceUris: []string{"uri"},
  31. },
  32. },
  33. }
  34. }
  35. func stringFieldSchema() *FieldSchema {
  36. return &FieldSchema{Name: "fieldname", Type: StringFieldType}
  37. }
  38. func nestedFieldSchema() *FieldSchema {
  39. return &FieldSchema{
  40. Name: "nested",
  41. Type: RecordFieldType,
  42. Schema: Schema{stringFieldSchema()},
  43. }
  44. }
  45. func bqStringFieldSchema() *bq.TableFieldSchema {
  46. return &bq.TableFieldSchema{
  47. Name: "fieldname",
  48. Type: "STRING",
  49. }
  50. }
  51. func bqNestedFieldSchema() *bq.TableFieldSchema {
  52. return &bq.TableFieldSchema{
  53. Name: "nested",
  54. Type: "RECORD",
  55. Fields: []*bq.TableFieldSchema{bqStringFieldSchema()},
  56. }
  57. }
  58. func TestLoad(t *testing.T) {
  59. testCases := []struct {
  60. dst *Table
  61. src *GCSReference
  62. options []Option
  63. want *bq.Job
  64. }{
  65. {
  66. dst: defaultTable,
  67. src: defaultGCS,
  68. want: defaultLoadJob(),
  69. },
  70. {
  71. dst: defaultTable,
  72. src: defaultGCS,
  73. options: []Option{
  74. MaxBadRecords(1),
  75. AllowJaggedRows(),
  76. AllowQuotedNewlines(),
  77. IgnoreUnknownValues(),
  78. },
  79. want: func() *bq.Job {
  80. j := defaultLoadJob()
  81. j.Configuration.Load.MaxBadRecords = 1
  82. j.Configuration.Load.AllowJaggedRows = true
  83. j.Configuration.Load.AllowQuotedNewlines = true
  84. j.Configuration.Load.IgnoreUnknownValues = true
  85. return j
  86. }(),
  87. },
  88. {
  89. dst: &Table{
  90. ProjectID: "project-id",
  91. DatasetID: "dataset-id",
  92. TableID: "table-id",
  93. },
  94. options: []Option{CreateNever, WriteTruncate},
  95. src: defaultGCS,
  96. want: func() *bq.Job {
  97. j := defaultLoadJob()
  98. j.Configuration.Load.CreateDisposition = "CREATE_NEVER"
  99. j.Configuration.Load.WriteDisposition = "WRITE_TRUNCATE"
  100. return j
  101. }(),
  102. },
  103. {
  104. dst: &Table{
  105. ProjectID: "project-id",
  106. DatasetID: "dataset-id",
  107. TableID: "table-id",
  108. },
  109. src: defaultGCS,
  110. options: []Option{
  111. DestinationSchema(Schema{
  112. stringFieldSchema(),
  113. nestedFieldSchema(),
  114. }),
  115. },
  116. want: func() *bq.Job {
  117. j := defaultLoadJob()
  118. j.Configuration.Load.Schema = &bq.TableSchema{
  119. Fields: []*bq.TableFieldSchema{
  120. bqStringFieldSchema(),
  121. bqNestedFieldSchema(),
  122. }}
  123. return j
  124. }(),
  125. },
  126. {
  127. dst: defaultTable,
  128. src: &GCSReference{
  129. uris: []string{"uri"},
  130. SkipLeadingRows: 1,
  131. SourceFormat: JSON,
  132. Encoding: UTF_8,
  133. FieldDelimiter: "\t",
  134. Quote: "-",
  135. },
  136. want: func() *bq.Job {
  137. j := defaultLoadJob()
  138. j.Configuration.Load.SkipLeadingRows = 1
  139. j.Configuration.Load.SourceFormat = "NEWLINE_DELIMITED_JSON"
  140. j.Configuration.Load.Encoding = "UTF-8"
  141. j.Configuration.Load.FieldDelimiter = "\t"
  142. hyphen := "-"
  143. j.Configuration.Load.Quote = &hyphen
  144. return j
  145. }(),
  146. },
  147. {
  148. dst: defaultTable,
  149. src: &GCSReference{
  150. uris: []string{"uri"},
  151. Quote: "",
  152. },
  153. want: func() *bq.Job {
  154. j := defaultLoadJob()
  155. j.Configuration.Load.Quote = nil
  156. return j
  157. }(),
  158. },
  159. {
  160. dst: defaultTable,
  161. src: &GCSReference{
  162. uris: []string{"uri"},
  163. Quote: "",
  164. ForceZeroQuote: true,
  165. },
  166. want: func() *bq.Job {
  167. j := defaultLoadJob()
  168. empty := ""
  169. j.Configuration.Load.Quote = &empty
  170. return j
  171. }(),
  172. },
  173. }
  174. for _, tc := range testCases {
  175. s := &testService{}
  176. c := &Client{
  177. service: s,
  178. }
  179. if _, err := c.Copy(context.Background(), tc.dst, tc.src, tc.options...); err != nil {
  180. t.Errorf("err calling load: %v", err)
  181. continue
  182. }
  183. if !reflect.DeepEqual(s.Job, tc.want) {
  184. t.Errorf("loading: got:\n%v\nwant:\n%v", s.Job, tc.want)
  185. }
  186. }
  187. }