2017-08-05 18:17:15 +00:00
|
|
|
// Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package bigquery
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-12-03 20:01:25 +00:00
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
|
|
|
|
|
|
|
"cloud.google.com/go/internal/testutil"
|
|
|
|
|
2017-08-05 18:17:15 +00:00
|
|
|
bq "google.golang.org/api/bigquery/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultCopyJob() *bq.Job {
|
|
|
|
return &bq.Job{
|
2017-10-01 08:13:39 +00:00
|
|
|
JobReference: &bq.JobReference{JobId: "RANDOM", ProjectId: "client-project-id"},
|
2017-08-05 18:17:15 +00:00
|
|
|
Configuration: &bq.JobConfiguration{
|
|
|
|
Copy: &bq.JobConfigurationTableCopy{
|
|
|
|
DestinationTable: &bq.TableReference{
|
|
|
|
ProjectId: "d-project-id",
|
|
|
|
DatasetId: "d-dataset-id",
|
|
|
|
TableId: "d-table-id",
|
|
|
|
},
|
|
|
|
SourceTables: []*bq.TableReference{
|
|
|
|
{
|
|
|
|
ProjectId: "s-project-id",
|
|
|
|
DatasetId: "s-dataset-id",
|
|
|
|
TableId: "s-table-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCopy(t *testing.T) {
|
2017-12-03 20:01:25 +00:00
|
|
|
defer fixRandomID("RANDOM")()
|
2017-08-05 18:17:15 +00:00
|
|
|
testCases := []struct {
|
2018-03-30 09:41:12 +00:00
|
|
|
dst *Table
|
|
|
|
srcs []*Table
|
|
|
|
jobID string
|
|
|
|
location string
|
|
|
|
config CopyConfig
|
|
|
|
want *bq.Job
|
2017-08-05 18:17:15 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
dst: &Table{
|
|
|
|
ProjectID: "d-project-id",
|
|
|
|
DatasetID: "d-dataset-id",
|
|
|
|
TableID: "d-table-id",
|
|
|
|
},
|
|
|
|
srcs: []*Table{
|
|
|
|
{
|
|
|
|
ProjectID: "s-project-id",
|
|
|
|
DatasetID: "s-dataset-id",
|
|
|
|
TableID: "s-table-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: defaultCopyJob(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dst: &Table{
|
|
|
|
ProjectID: "d-project-id",
|
|
|
|
DatasetID: "d-dataset-id",
|
|
|
|
TableID: "d-table-id",
|
|
|
|
},
|
|
|
|
srcs: []*Table{
|
|
|
|
{
|
|
|
|
ProjectID: "s-project-id",
|
|
|
|
DatasetID: "s-dataset-id",
|
|
|
|
TableID: "s-table-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
config: CopyConfig{
|
2018-03-30 09:41:12 +00:00
|
|
|
CreateDisposition: CreateNever,
|
|
|
|
WriteDisposition: WriteTruncate,
|
|
|
|
DestinationEncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
|
|
|
|
Labels: map[string]string{"a": "b"},
|
2017-08-05 18:17:15 +00:00
|
|
|
},
|
|
|
|
want: func() *bq.Job {
|
|
|
|
j := defaultCopyJob()
|
2017-12-03 20:01:25 +00:00
|
|
|
j.Configuration.Labels = map[string]string{"a": "b"}
|
2017-08-05 18:17:15 +00:00
|
|
|
j.Configuration.Copy.CreateDisposition = "CREATE_NEVER"
|
|
|
|
j.Configuration.Copy.WriteDisposition = "WRITE_TRUNCATE"
|
2018-03-30 09:41:12 +00:00
|
|
|
j.Configuration.Copy.DestinationEncryptionConfiguration = &bq.EncryptionConfiguration{KmsKeyName: "keyName"}
|
2017-08-05 18:17:15 +00:00
|
|
|
return j
|
|
|
|
}(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
dst: &Table{
|
|
|
|
ProjectID: "d-project-id",
|
|
|
|
DatasetID: "d-dataset-id",
|
|
|
|
TableID: "d-table-id",
|
|
|
|
},
|
|
|
|
srcs: []*Table{
|
|
|
|
{
|
|
|
|
ProjectID: "s-project-id",
|
|
|
|
DatasetID: "s-dataset-id",
|
|
|
|
TableID: "s-table-id",
|
|
|
|
},
|
|
|
|
},
|
2017-12-03 20:01:25 +00:00
|
|
|
jobID: "job-id",
|
2017-08-05 18:17:15 +00:00
|
|
|
want: func() *bq.Job {
|
|
|
|
j := defaultCopyJob()
|
2017-09-13 12:09:48 +00:00
|
|
|
j.JobReference.JobId = "job-id"
|
2017-08-05 18:17:15 +00:00
|
|
|
return j
|
|
|
|
}(),
|
|
|
|
},
|
2018-03-30 09:41:12 +00:00
|
|
|
{
|
|
|
|
dst: &Table{
|
|
|
|
ProjectID: "d-project-id",
|
|
|
|
DatasetID: "d-dataset-id",
|
|
|
|
TableID: "d-table-id",
|
|
|
|
},
|
|
|
|
srcs: []*Table{
|
|
|
|
{
|
|
|
|
ProjectID: "s-project-id",
|
|
|
|
DatasetID: "s-dataset-id",
|
|
|
|
TableID: "s-table-id",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
location: "asia-northeast1",
|
|
|
|
want: func() *bq.Job {
|
|
|
|
j := defaultCopyJob()
|
|
|
|
j.JobReference.Location = "asia-northeast1"
|
|
|
|
return j
|
|
|
|
}(),
|
|
|
|
},
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
2017-12-03 20:01:25 +00:00
|
|
|
c := &Client{projectID: "client-project-id"}
|
2017-09-13 12:09:48 +00:00
|
|
|
for i, tc := range testCases {
|
2017-08-05 18:17:15 +00:00
|
|
|
tc.dst.c = c
|
|
|
|
copier := tc.dst.CopierFrom(tc.srcs...)
|
2017-12-03 20:01:25 +00:00
|
|
|
copier.JobID = tc.jobID
|
2018-03-30 09:41:12 +00:00
|
|
|
copier.Location = tc.location
|
2017-08-05 18:17:15 +00:00
|
|
|
tc.config.Srcs = tc.srcs
|
|
|
|
tc.config.Dst = tc.dst
|
|
|
|
copier.CopyConfig = tc.config
|
2017-12-03 20:01:25 +00:00
|
|
|
got := copier.newJob()
|
|
|
|
checkJob(t, i, got, tc.want)
|
|
|
|
|
|
|
|
jc, err := bqToJobConfig(got.Configuration, c)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("#%d: %v", i, err)
|
|
|
|
}
|
|
|
|
diff := testutil.Diff(jc.(*CopyConfig), &copier.CopyConfig,
|
|
|
|
cmpopts.IgnoreUnexported(Table{}))
|
|
|
|
if diff != "" {
|
|
|
|
t.Errorf("#%d: (got=-, want=+:\n%s", i, diff)
|
2017-08-05 18:17:15 +00:00
|
|
|
}
|
2017-09-13 12:09:48 +00:00
|
|
|
}
|
|
|
|
}
|