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