extract_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 defaultExtractJob() *bq.Job {
  22. return &bq.Job{
  23. Configuration: &bq.JobConfiguration{
  24. Extract: &bq.JobConfigurationExtract{
  25. SourceTable: &bq.TableReference{
  26. ProjectId: "project-id",
  27. DatasetId: "dataset-id",
  28. TableId: "table-id",
  29. },
  30. DestinationUris: []string{"uri"},
  31. },
  32. },
  33. }
  34. }
  35. func TestExtract(t *testing.T) {
  36. testCases := []struct {
  37. dst *GCSReference
  38. src *Table
  39. options []Option
  40. want *bq.Job
  41. }{
  42. {
  43. dst: defaultGCS,
  44. src: defaultTable,
  45. want: defaultExtractJob(),
  46. },
  47. {
  48. dst: defaultGCS,
  49. src: defaultTable,
  50. options: []Option{
  51. DisableHeader(),
  52. },
  53. want: func() *bq.Job {
  54. j := defaultExtractJob()
  55. f := false
  56. j.Configuration.Extract.PrintHeader = &f
  57. return j
  58. }(),
  59. },
  60. {
  61. dst: &GCSReference{
  62. uris: []string{"uri"},
  63. Compression: Gzip,
  64. DestinationFormat: JSON,
  65. FieldDelimiter: "\t",
  66. },
  67. src: defaultTable,
  68. want: func() *bq.Job {
  69. j := defaultExtractJob()
  70. j.Configuration.Extract.Compression = "GZIP"
  71. j.Configuration.Extract.DestinationFormat = "NEWLINE_DELIMITED_JSON"
  72. j.Configuration.Extract.FieldDelimiter = "\t"
  73. return j
  74. }(),
  75. },
  76. }
  77. for _, tc := range testCases {
  78. s := &testService{}
  79. c := &Client{
  80. service: s,
  81. }
  82. if _, err := c.Copy(context.Background(), tc.dst, tc.src, tc.options...); err != nil {
  83. t.Errorf("err calling extract: %v", err)
  84. continue
  85. }
  86. if !reflect.DeepEqual(s.Job, tc.want) {
  87. t.Errorf("extracting: got:\n%v\nwant:\n%v", s.Job, tc.want)
  88. }
  89. }
  90. }