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.
 
 
 

117 lines
2.8 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. "testing"
  17. "cloud.google.com/go/internal/testutil"
  18. "github.com/google/go-cmp/cmp"
  19. bq "google.golang.org/api/bigquery/v2"
  20. )
  21. func defaultExtractJob() *bq.Job {
  22. return &bq.Job{
  23. JobReference: &bq.JobReference{JobId: "RANDOM", ProjectId: "client-project-id"},
  24. Configuration: &bq.JobConfiguration{
  25. Extract: &bq.JobConfigurationExtract{
  26. SourceTable: &bq.TableReference{
  27. ProjectId: "client-project-id",
  28. DatasetId: "dataset-id",
  29. TableId: "table-id",
  30. },
  31. DestinationUris: []string{"uri"},
  32. },
  33. },
  34. }
  35. }
  36. func defaultGCS() *GCSReference {
  37. return &GCSReference{
  38. URIs: []string{"uri"},
  39. }
  40. }
  41. func TestExtract(t *testing.T) {
  42. defer fixRandomID("RANDOM")()
  43. c := &Client{
  44. projectID: "client-project-id",
  45. }
  46. testCases := []struct {
  47. dst *GCSReference
  48. src *Table
  49. config ExtractConfig
  50. want *bq.Job
  51. }{
  52. {
  53. dst: defaultGCS(),
  54. src: c.Dataset("dataset-id").Table("table-id"),
  55. want: defaultExtractJob(),
  56. },
  57. {
  58. dst: defaultGCS(),
  59. src: c.Dataset("dataset-id").Table("table-id"),
  60. config: ExtractConfig{
  61. DisableHeader: true,
  62. Labels: map[string]string{"a": "b"},
  63. },
  64. want: func() *bq.Job {
  65. j := defaultExtractJob()
  66. j.Configuration.Labels = map[string]string{"a": "b"}
  67. f := false
  68. j.Configuration.Extract.PrintHeader = &f
  69. return j
  70. }(),
  71. },
  72. {
  73. dst: func() *GCSReference {
  74. g := NewGCSReference("uri")
  75. g.Compression = Gzip
  76. g.DestinationFormat = JSON
  77. g.FieldDelimiter = "\t"
  78. return g
  79. }(),
  80. src: c.Dataset("dataset-id").Table("table-id"),
  81. want: func() *bq.Job {
  82. j := defaultExtractJob()
  83. j.Configuration.Extract.Compression = "GZIP"
  84. j.Configuration.Extract.DestinationFormat = "NEWLINE_DELIMITED_JSON"
  85. j.Configuration.Extract.FieldDelimiter = "\t"
  86. return j
  87. }(),
  88. },
  89. }
  90. for i, tc := range testCases {
  91. ext := tc.src.ExtractorTo(tc.dst)
  92. tc.config.Src = ext.Src
  93. tc.config.Dst = ext.Dst
  94. ext.ExtractConfig = tc.config
  95. got := ext.newJob()
  96. checkJob(t, i, got, tc.want)
  97. jc, err := bqToJobConfig(got.Configuration, c)
  98. if err != nil {
  99. t.Fatalf("#%d: %v", i, err)
  100. }
  101. diff := testutil.Diff(jc, &ext.ExtractConfig,
  102. cmp.AllowUnexported(Table{}, Client{}))
  103. if diff != "" {
  104. t.Errorf("#%d: (got=-, want=+:\n%s", i, diff)
  105. }
  106. }
  107. }