1
2
3
4
5
6
7
8
9
10
11
12
13
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 TestBQToModelMetadata(t *testing.T) {
27 aTime := time.Date(2019, 3, 14, 0, 0, 0, 0, time.Local)
28 aTimeMillis := aTime.UnixNano() / 1e6
29 for _, test := range []struct {
30 in *bq.Model
31 want *ModelMetadata
32 }{
33 {&bq.Model{}, &ModelMetadata{}},
34 {
35 &bq.Model{
36 CreationTime: aTimeMillis,
37 Description: "desc",
38 Etag: "etag",
39 ExpirationTime: aTimeMillis,
40 EncryptionConfiguration: &bq.EncryptionConfiguration{KmsKeyName: "keyName"},
41 FriendlyName: "fname",
42 LastModifiedTime: aTimeMillis,
43 Location: "loc",
44 Labels: map[string]string{"a": "b"},
45 },
46 &ModelMetadata{
47 CreationTime: aTime.Truncate(time.Millisecond),
48 Description: "desc",
49 ETag: "etag",
50 ExpirationTime: aTime.Truncate(time.Millisecond),
51 Name: "fname",
52 LastModifiedTime: aTime.Truncate(time.Millisecond),
53 EncryptionConfig: &EncryptionConfig{KMSKeyName: "keyName"},
54 Location: "loc",
55 Labels: map[string]string{"a": "b"},
56 },
57 },
58 } {
59 got, err := bqToModelMetadata(test.in)
60 if err != nil {
61 t.Fatal(err)
62 }
63 if diff := testutil.Diff(got, test.want, cmpopts.IgnoreUnexported(ModelMetadata{})); diff != "" {
64 t.Errorf("%+v:\n, -got, +want:\n%s", test.in, diff)
65 }
66 }
67 }
68
69 func TestModelMetadataUpdateToBQ(t *testing.T) {
70 aTime := time.Date(2019, 3, 14, 0, 0, 0, 0, time.Local)
71 aTimeMillis := aTime.UnixNano() / 1e6
72
73 for _, test := range []struct {
74 in ModelMetadataToUpdate
75 want *bq.Model
76 }{
77 {
78 ModelMetadataToUpdate{},
79 &bq.Model{},
80 },
81 {
82 ModelMetadataToUpdate{
83 Description: "d",
84 Name: "n",
85 },
86 &bq.Model{
87 Description: "d",
88 FriendlyName: "n",
89 ForceSendFields: []string{"Description", "FriendlyName"},
90 },
91 },
92 {
93 ModelMetadataToUpdate{
94 ExpirationTime: aTime,
95 },
96 &bq.Model{
97 ExpirationTime: aTimeMillis,
98 ForceSendFields: []string{"ExpirationTime"},
99 },
100 },
101 {
102 ModelMetadataToUpdate{
103 labelUpdater: labelUpdater{
104 setLabels: map[string]string{"L": "V"},
105 deleteLabels: map[string]bool{"D": true},
106 },
107 },
108 &bq.Model{
109 Labels: map[string]string{"L": "V"},
110 NullFields: []string{"Labels.D"},
111 },
112 },
113 } {
114 got, err := test.in.toBQ()
115 if err != nil {
116 t.Fatalf("%+v: %v", test.in, err)
117 }
118 if diff := testutil.Diff(got, test.want); diff != "" {
119 t.Errorf("%+v:\n-got, +want:\n%s", test.in, diff)
120 }
121 }
122 }
123
124 func TestModelIdentifiers(t *testing.T) {
125 testModel := &Model{
126 ProjectID: "p",
127 DatasetID: "d",
128 ModelID: "m",
129 c: nil,
130 }
131 for _, tc := range []struct {
132 description string
133 in *Model
134 format IdentifierFormat
135 want string
136 wantErr bool
137 }{
138 {
139 description: "empty format string",
140 in: testModel,
141 format: "",
142 wantErr: true,
143 },
144 {
145 description: "legacy",
146 in: testModel,
147 format: LegacySQLID,
148 want: "p:d.m",
149 },
150 {
151 description: "standard unquoted",
152 in: testModel,
153 format: StandardSQLID,
154 want: "p.d.m",
155 },
156 {
157 description: "standard w/dash",
158 in: &Model{ProjectID: "p-p", DatasetID: "d", ModelID: "m"},
159 format: StandardSQLID,
160 want: "`p-p.d.m`",
161 },
162 {
163 description: "api resource",
164 in: testModel,
165 format: StorageAPIResourceID,
166 wantErr: true,
167 },
168 } {
169 got, err := tc.in.Identifier(tc.format)
170 if tc.wantErr && err == nil {
171 t.Errorf("case %q: wanted err, was success", tc.description)
172 }
173 if !tc.wantErr {
174 if err != nil {
175 t.Errorf("case %q: wanted success, got err: %v", tc.description, err)
176 } else {
177 if got != tc.want {
178 t.Errorf("case %q: got %s, want %s", tc.description, got, tc.want)
179 }
180 }
181 }
182 }
183 }
184
View as plain text