...

Source file src/cloud.google.com/go/bigquery/copy_test.go

Documentation: cloud.google.com/go/bigquery

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

View as plain text