-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathmethod_test.go
More file actions
432 lines (416 loc) · 13.1 KB
/
method_test.go
File metadata and controls
432 lines (416 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/googleapis/librarian/internal/sidekick/api"
)
func TestIsCreate(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Name Prefix", &api.Method{Name: "CreateInstance"}, true},
{"Name Mismatch", &api.Method{Name: "GetInstance"}, false},
{"Verb Match", api.NewTestMethod("CreateInstance").WithVerb("POST"), true},
{"Verb Mismatch", api.NewTestMethod("CreateInstance").WithVerb("GET"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsCreate(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestIsGet(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Name Prefix", &api.Method{Name: "GetInstance"}, true},
{"Name Mismatch", &api.Method{Name: "CreateInstance"}, false},
{"Verb Match", api.NewTestMethod("GetInstance").WithVerb("GET"), true},
{"Verb Mismatch", api.NewTestMethod("GetInstance").WithVerb("POST"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsGet(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestIsList(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Name Prefix", &api.Method{Name: "ListInstances"}, true},
{"Name Mismatch", &api.Method{Name: "GetInstance"}, false},
{"Verb Match", api.NewTestMethod("ListInstances").WithVerb("GET"), true},
{"Verb Mismatch", api.NewTestMethod("ListInstances").WithVerb("POST"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsList(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestIsUpdate(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Name Prefix", &api.Method{Name: "UpdateInstance"}, true},
{"Name Mismatch", &api.Method{Name: "GetInstance"}, false},
{"Verb Match PATCH", api.NewTestMethod("UpdateInstance").WithVerb("PATCH"), true},
{"Verb Match PUT", api.NewTestMethod("UpdateInstance").WithVerb("PUT"), true},
{"Verb Mismatch", api.NewTestMethod("UpdateInstance").WithVerb("GET"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsUpdate(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestIsDelete(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Name Prefix", &api.Method{Name: "DeleteInstance"}, true},
{"Name Mismatch", &api.Method{Name: "GetInstance"}, false},
{"Verb Match", api.NewTestMethod("DeleteInstance").WithVerb("DELETE"), true},
{"Verb Mismatch", api.NewTestMethod("DeleteInstance").WithVerb("GET"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsDelete(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestGetCommandName(t *testing.T) {
v := "exportData"
for _, test := range []struct {
name string
method *api.Method
want string
}{
{"Standard Create", &api.Method{Name: "CreateInstance"}, "create"},
{"Standard List", &api.Method{Name: "ListInstances"}, "list"},
{"Standard Get", &api.Method{Name: "GetInstance"}, "describe"},
{"Custom Verb in Path", api.NewTestMethod("ExportData").WithPathTemplate(&api.PathTemplate{Verb: &v}), "export_data"},
{"Fallback to Name", &api.Method{Name: "ExportData"}, "export_data"},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got, err := GetCommandName(test.method)
if err != nil {
t.Fatal(err)
}
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}
func TestFindResourceMessage(t *testing.T) {
instanceMsg := &api.Message{
Name: "Instance",
}
for _, test := range []struct {
name string
outputType *api.Message
want *api.Message
}{
{
name: "Standard List Response",
outputType: &api.Message{
Fields: []*api.Field{
{Name: "next_page_token", Typez: api.STRING_TYPE},
{Name: "instances", Repeated: true, MessageType: instanceMsg},
},
},
want: instanceMsg,
},
{
name: "No Repeated Message",
outputType: &api.Message{
Fields: []*api.Field{
{Name: "status", Typez: api.STRING_TYPE},
},
},
want: nil,
},
{
name: "Nil Output Type",
outputType: nil,
want: nil,
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := FindResourceMessage(test.outputType)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestGetCommandName_Error(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
wantErr error
}{
{
name: "Nil Method",
method: nil,
wantErr: errors.New("method cannot be nil"),
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
_, gotErr := GetCommandName(test.method)
if test.wantErr != nil {
if gotErr == nil {
t.Fatalf("GetCommandName() returned nil error, want %v", test.wantErr)
}
if gotErr.Error() != test.wantErr.Error() {
t.Errorf("GetCommandName() error = %q, want %q", gotErr.Error(), test.wantErr.Error())
}
} else if gotErr != nil {
t.Errorf("GetCommandName() returned error %v, want nil", gotErr)
}
})
}
}
func TestIsResourceMethod(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Standard Get", api.NewTestMethod("GetInstance").WithVerb("GET"), true},
{"Standard List", api.NewTestMethod("ListInstances").WithVerb("GET"), false},
{"Custom Resource", api.NewTestMethod("CustomInstance").WithPathTemplate(
&api.PathTemplate{Segments: []api.PathSegment{*api.NewPathSegment().WithVariable(api.NewPathVariable("instance"))}},
), true},
{"Custom Collection", api.NewTestMethod("CustomCollection").WithPathTemplate(
&api.PathTemplate{Segments: []api.PathSegment{*api.NewPathSegment().WithLiteral("instances")}},
), false},
{"Nil PathInfo", &api.Method{Name: "CustomMethod", PathInfo: nil}, false},
{"Empty Bindings", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{}}}, false},
{"Nil PathTemplate", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{{PathTemplate: nil}}}}, false},
{"Empty Segments", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{{PathTemplate: &api.PathTemplate{Segments: []api.PathSegment{}}}}}}, false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := IsResourceMethod(test.method); got != test.want {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(test.want, got))
}
})
}
}
func TestIsCollectionMethod(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Standard Get", api.NewTestMethod("GetInstance").WithVerb("GET"), false},
{"Standard List", api.NewTestMethod("ListInstances").WithVerb("GET"), true},
{"Custom Resource", api.NewTestMethod("CustomInstance").WithPathTemplate(
&api.PathTemplate{Segments: []api.PathSegment{*api.NewPathSegment().WithVariable(api.NewPathVariable("instance"))}},
), false},
{"Custom Collection", api.NewTestMethod("CustomCollection").WithPathTemplate(
&api.PathTemplate{Segments: []api.PathSegment{*api.NewPathSegment().WithLiteral("instances")}},
), true},
{"Nil PathInfo", &api.Method{Name: "CustomMethod", PathInfo: nil}, false},
{"Empty Bindings", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{}}}, false},
{"Nil PathTemplate", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{{PathTemplate: nil}}}}, false},
{"Empty Segments", &api.Method{Name: "CustomMethod", PathInfo: &api.PathInfo{Bindings: []*api.PathBinding{{PathTemplate: &api.PathTemplate{Segments: []api.PathSegment{}}}}}}, false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := IsCollectionMethod(test.method); got != test.want {
t.Errorf("mismatch (-want +got):\n%s", cmp.Diff(test.want, got))
}
})
}
}
func TestIsStandardMethod(t *testing.T) {
for _, test := range []struct {
name string
method *api.Method
want bool
}{
{"Get", api.NewTestMethod("GetInstance").WithVerb("GET"), true},
{"List", api.NewTestMethod("ListInstances").WithVerb("GET"), true},
{"Create", api.NewTestMethod("CreateInstance").WithVerb("POST"), true},
{"Update", api.NewTestMethod("UpdateInstance").WithVerb("PATCH"), true},
{"Delete", api.NewTestMethod("DeleteInstance").WithVerb("DELETE"), true},
{"Custom", api.NewTestMethod("ExportInstance").WithVerb("POST"), false},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsStandardMethod(test.method)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestIsSingletonResourceMethod(t *testing.T) {
model := &api.API{
ResourceDefinitions: []*api.Resource{
{
Type: "test.googleapis.com/Singleton",
Patterns: []api.ResourcePattern{
[]api.PathSegment{
*api.NewPathSegment().WithLiteral("projects"),
*api.NewPathSegment().WithVariable(api.NewPathVariable("project")),
*api.NewPathSegment().WithLiteral("singletonConfig"),
},
},
},
{
Type: "test.googleapis.com/AdjacentLiterals",
Patterns: []api.ResourcePattern{
[]api.PathSegment{
*api.NewPathSegment().WithLiteral("projects"),
*api.NewPathSegment().WithVariable(api.NewPathVariable("project")),
*api.NewPathSegment().WithLiteral("literal1"),
*api.NewPathSegment().WithLiteral("literal2"),
*api.NewPathSegment().WithVariable(api.NewPathVariable("instance")),
},
},
},
{
Type: "test.googleapis.com/Standard",
Patterns: []api.ResourcePattern{
[]api.PathSegment{
*api.NewPathSegment().WithLiteral("projects"),
*api.NewPathSegment().WithVariable(api.NewPathVariable("project")),
*api.NewPathSegment().WithLiteral("instances"),
*api.NewPathSegment().WithVariable(api.NewPathVariable("instance")),
},
},
},
},
}
for _, test := range []struct {
name string
method *api.Method
model *api.API
want bool
}{
{
name: "Nil Method",
method: nil,
model: &api.API{},
want: false,
},
{
name: "Fallback: No Resource Found, Path Ends in Variable",
method: api.NewTestMethod("Test").WithPathTemplate(&api.PathTemplate{Segments: parseResourcePattern("projects/{project}/instances/{instance}")}),
model: &api.API{},
want: false,
},
{
name: "Fallback: No Resource Found, Path Ends in Literal",
method: api.NewTestMethod("Test").WithPathTemplate(&api.PathTemplate{Segments: parseResourcePattern("projects/{project}/singletonConfig")}),
model: &api.API{},
want: false, // Was true when fallback existed.
},
{
name: "Resource Found: Singleton Pattern",
method: func() *api.Method {
m := api.NewTestMethod("GetSingleton")
m.InputType = &api.Message{
Fields: []*api.Field{{
Name: "name",
ResourceReference: &api.ResourceReference{
Type: "test.googleapis.com/Singleton",
},
}},
}
return m
}(),
model: model,
want: true,
},
{
name: "Resource Found: Adjacent Literals Pattern",
method: func() *api.Method {
m := api.NewTestMethod("GetAdjacent")
m.InputType = &api.Message{
Fields: []*api.Field{{
Name: "name",
ResourceReference: &api.ResourceReference{
Type: "test.googleapis.com/AdjacentLiterals",
},
}},
}
return m
}(),
model: model,
want: true,
},
{
name: "Resource Found: Standard Pattern",
method: func() *api.Method {
m := api.NewTestMethod("GetStandard")
m.InputType = &api.Message{
Fields: []*api.Field{{
Name: "name",
ResourceReference: &api.ResourceReference{
Type: "test.googleapis.com/Standard",
},
}},
}
return m
}(),
model: model,
want: false,
},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
got := IsSingletonResourceMethod(test.method, test.model)
if diff := cmp.Diff(test.want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
})
}
}