1
2
3
4
5
6
7
8
9
10
11
12
13
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