...

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

Documentation: cloud.google.com/go/bigquery

     1  // Copyright 2017 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  
    20  	"cloud.google.com/go/internal/testutil"
    21  	bq "google.golang.org/api/bigquery/v2"
    22  )
    23  
    24  func TestCreateJobRef(t *testing.T) {
    25  	defer fixRandomID("RANDOM")()
    26  	cNoLoc := &Client{projectID: "projectID"}
    27  	cLoc := &Client{projectID: "projectID", Location: "defaultLoc"}
    28  	for _, test := range []struct {
    29  		in     JobIDConfig
    30  		client *Client
    31  		want   *bq.JobReference
    32  	}{
    33  		{
    34  			in:   JobIDConfig{JobID: "foo"},
    35  			want: &bq.JobReference{JobId: "foo"},
    36  		},
    37  		{
    38  			in:   JobIDConfig{},
    39  			want: &bq.JobReference{JobId: "RANDOM"},
    40  		},
    41  		{
    42  			in:   JobIDConfig{AddJobIDSuffix: true},
    43  			want: &bq.JobReference{JobId: "RANDOM"},
    44  		},
    45  		{
    46  			in:   JobIDConfig{JobID: "foo", AddJobIDSuffix: true},
    47  			want: &bq.JobReference{JobId: "foo-RANDOM"},
    48  		},
    49  		{
    50  			in:   JobIDConfig{JobID: "foo", Location: "loc"},
    51  			want: &bq.JobReference{JobId: "foo", Location: "loc"},
    52  		},
    53  		{
    54  			in:     JobIDConfig{JobID: "foo"},
    55  			client: cLoc,
    56  			want:   &bq.JobReference{JobId: "foo", Location: "defaultLoc"},
    57  		},
    58  		{
    59  			in:     JobIDConfig{JobID: "foo", Location: "loc"},
    60  			client: cLoc,
    61  			want:   &bq.JobReference{JobId: "foo", Location: "loc"},
    62  		},
    63  		{
    64  			in:   JobIDConfig{JobID: "foo", ProjectID: "anotherProj"},
    65  			want: &bq.JobReference{JobId: "foo", ProjectId: "anotherProj"},
    66  		},
    67  	} {
    68  		client := test.client
    69  		if client == nil {
    70  			client = cNoLoc
    71  		}
    72  		got := test.in.createJobRef(client)
    73  		if test.want.ProjectId == "" {
    74  			test.want.ProjectId = "projectID"
    75  		}
    76  		if !testutil.Equal(got, test.want) {
    77  			t.Errorf("%+v: got %+v, want %+v", test.in, got, test.want)
    78  		}
    79  	}
    80  }
    81  
    82  func fixRandomID(s string) func() {
    83  	prev := randomIDFn
    84  	randomIDFn = func() string { return s }
    85  	return func() { randomIDFn = prev }
    86  }
    87  
    88  func checkJob(t *testing.T, i int, got, want *bq.Job) {
    89  	if got.JobReference == nil {
    90  		t.Errorf("#%d: empty job  reference", i)
    91  		return
    92  	}
    93  	if got.JobReference.JobId == "" {
    94  		t.Errorf("#%d: empty job ID", i)
    95  		return
    96  	}
    97  	d := testutil.Diff(got, want)
    98  	if d != "" {
    99  		t.Errorf("#%d: (got=-, want=+) %s", i, d)
   100  	}
   101  }
   102  

View as plain text