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.
 
 
 

164 lines
4.0 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/cmpopts"
  19. bq "google.golang.org/api/bigquery/v2"
  20. )
  21. func defaultCopyJob() *bq.Job {
  22. return &bq.Job{
  23. JobReference: &bq.JobReference{JobId: "RANDOM", ProjectId: "client-project-id"},
  24. Configuration: &bq.JobConfiguration{
  25. Copy: &bq.JobConfigurationTableCopy{
  26. DestinationTable: &bq.TableReference{
  27. ProjectId: "d-project-id",
  28. DatasetId: "d-dataset-id",
  29. TableId: "d-table-id",
  30. },
  31. SourceTables: []*bq.TableReference{
  32. {
  33. ProjectId: "s-project-id",
  34. DatasetId: "s-dataset-id",
  35. TableId: "s-table-id",
  36. },
  37. },
  38. },
  39. },
  40. }
  41. }
  42. func TestCopy(t *testing.T) {
  43. defer fixRandomID("RANDOM")()
  44. testCases := []struct {
  45. dst *Table
  46. srcs []*Table
  47. jobID string
  48. location string
  49. config CopyConfig
  50. want *bq.Job
  51. }{
  52. {
  53. dst: &Table{
  54. ProjectID: "d-project-id",
  55. DatasetID: "d-dataset-id",
  56. TableID: "d-table-id",
  57. },
  58. srcs: []*Table{
  59. {
  60. ProjectID: "s-project-id",
  61. DatasetID: "s-dataset-id",
  62. TableID: "s-table-id",
  63. },
  64. },
  65. want: defaultCopyJob(),
  66. },
  67. {
  68. dst: &Table{
  69. ProjectID: "d-project-id",
  70. DatasetID: "d-dataset-id",
  71. TableID: "d-table-id",
  72. },
  73. srcs: []*Table{
  74. {
  75. ProjectID: "s-project-id",
  76. DatasetID: "s-dataset-id",
  77. TableID: "s-table-id",
  78. },
  79. },
  80. config: CopyConfig{
  81. CreateDisposition: CreateNever,
  82. WriteDisposition: WriteTruncate,
  83. DestinationEncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
  84. Labels: map[string]string{"a": "b"},
  85. },
  86. want: func() *bq.Job {
  87. j := defaultCopyJob()
  88. j.Configuration.Labels = map[string]string{"a": "b"}
  89. j.Configuration.Copy.CreateDisposition = "CREATE_NEVER"
  90. j.Configuration.Copy.WriteDisposition = "WRITE_TRUNCATE"
  91. j.Configuration.Copy.DestinationEncryptionConfiguration = &bq.EncryptionConfiguration{KmsKeyName: "keyName"}
  92. return j
  93. }(),
  94. },
  95. {
  96. dst: &Table{
  97. ProjectID: "d-project-id",
  98. DatasetID: "d-dataset-id",
  99. TableID: "d-table-id",
  100. },
  101. srcs: []*Table{
  102. {
  103. ProjectID: "s-project-id",
  104. DatasetID: "s-dataset-id",
  105. TableID: "s-table-id",
  106. },
  107. },
  108. jobID: "job-id",
  109. want: func() *bq.Job {
  110. j := defaultCopyJob()
  111. j.JobReference.JobId = "job-id"
  112. return j
  113. }(),
  114. },
  115. {
  116. dst: &Table{
  117. ProjectID: "d-project-id",
  118. DatasetID: "d-dataset-id",
  119. TableID: "d-table-id",
  120. },
  121. srcs: []*Table{
  122. {
  123. ProjectID: "s-project-id",
  124. DatasetID: "s-dataset-id",
  125. TableID: "s-table-id",
  126. },
  127. },
  128. location: "asia-northeast1",
  129. want: func() *bq.Job {
  130. j := defaultCopyJob()
  131. j.JobReference.Location = "asia-northeast1"
  132. return j
  133. }(),
  134. },
  135. }
  136. c := &Client{projectID: "client-project-id"}
  137. for i, tc := range testCases {
  138. tc.dst.c = c
  139. copier := tc.dst.CopierFrom(tc.srcs...)
  140. copier.JobID = tc.jobID
  141. copier.Location = tc.location
  142. tc.config.Srcs = tc.srcs
  143. tc.config.Dst = tc.dst
  144. copier.CopyConfig = tc.config
  145. got := copier.newJob()
  146. checkJob(t, i, got, tc.want)
  147. jc, err := bqToJobConfig(got.Configuration, c)
  148. if err != nil {
  149. t.Fatalf("#%d: %v", i, err)
  150. }
  151. diff := testutil.Diff(jc.(*CopyConfig), &copier.CopyConfig,
  152. cmpopts.IgnoreUnexported(Table{}))
  153. if diff != "" {
  154. t.Errorf("#%d: (got=-, want=+:\n%s", i, diff)
  155. }
  156. }
  157. }