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 15package bigquery 16 17import ( 18 "testing" 19 20 "cloud.google.com/go/internal/testutil" 21 bq "google.golang.org/api/bigquery/v2" 22) 23 24func 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 client := test.client 65 if client == nil { 66 client = cNoLoc 67 } 68 got := test.in.createJobRef(client) 69 test.want.ProjectId = "projectID" 70 if !testutil.Equal(got, test.want) { 71 t.Errorf("%+v: got %+v, want %+v", test.in, got, test.want) 72 } 73 } 74} 75 76func fixRandomID(s string) func() { 77 prev := randomIDFn 78 randomIDFn = func() string { return s } 79 return func() { randomIDFn = prev } 80} 81 82func checkJob(t *testing.T, i int, got, want *bq.Job) { 83 if got.JobReference == nil { 84 t.Errorf("#%d: empty job reference", i) 85 return 86 } 87 if got.JobReference.JobId == "" { 88 t.Errorf("#%d: empty job ID", i) 89 return 90 } 91 d := testutil.Diff(got, want) 92 if d != "" { 93 t.Errorf("#%d: (got=-, want=+) %s", i, d) 94 } 95} 96