-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_aiptest.pb.go
400 lines (362 loc) · 11.6 KB
/
schema_aiptest.pb.go
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
// Code generated by protoc-gen-go-aip-test. DO NOT EDIT.
package pubsubpb
import (
context "context"
cmpopts "github.com/google/go-cmp/cmp/cmpopts"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
protocmp "google.golang.org/protobuf/testing/protocmp"
assert "gotest.tools/v3/assert"
strings "strings"
testing "testing"
)
type SchemaServiceTestSuite struct {
T *testing.T
// Server to test.
Server SchemaServiceServer
}
func (fx SchemaServiceTestSuite) TestSchema(ctx context.Context, options SchemaServiceSchemaTestSuiteConfig) {
fx.T.Run("Schema", func(t *testing.T) {
options.ctx = ctx
options.service = fx.Server
options.test(t)
})
}
type SchemaServiceSchemaTestSuiteConfig struct {
ctx context.Context
service SchemaServiceServer
currParent int
// The parents to use when creating resources.
// At least one parent needs to be set. Depending on methods available on the resource,
// more may be required. If insufficient number of parents are
// provided the test will fail.
Parents []string
// Create should return a resource which is valid to create, i.e.
// all required fields set.
Create func(parent string) *Schema
// Patterns of tests to skip.
// For example if a service has a Get method:
// Skip: ["Get"] will skip all tests for Get.
// Skip: ["Get/persisted"] will only skip the subtest called "persisted" of Get.
Skip []string
}
func (fx *SchemaServiceSchemaTestSuiteConfig) test(t *testing.T) {
t.Run("Create", fx.testCreate)
t.Run("Get", fx.testGet)
t.Run("List", fx.testList)
t.Run("Delete", fx.testDelete)
}
func (fx *SchemaServiceSchemaTestSuiteConfig) testCreate(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no parent is provided.
t.Run("missing parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateSchema(fx.ctx, &CreateSchemaRequest{
Parent: "",
Schema: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.CreateSchema(fx.ctx, &CreateSchemaRequest{
Parent: "invalid resource name",
Schema: fx.Create(fx.nextParent(t, false)),
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// The created resource should be persisted and reachable with Get.
t.Run("persisted", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg, err := fx.service.CreateSchema(fx.ctx, &CreateSchemaRequest{
Parent: parent,
Schema: fx.Create(parent),
})
assert.NilError(t, err)
persisted, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: msg.Name,
})
assert.NilError(t, err)
assert.DeepEqual(t, msg, persisted, protocmp.Transform())
})
// The method should fail with InvalidArgument if the resource has any
// required fields and they are not provided.
t.Run("required fields", func(t *testing.T) {
fx.maybeSkip(t)
t.Run(".name", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
msg := fx.Create(parent)
container := msg
if container == nil {
t.Skip("not reachable")
}
fd := container.ProtoReflect().Descriptor().Fields().ByName("name")
container.ProtoReflect().Clear(fd)
_, err := fx.service.CreateSchema(fx.ctx, &CreateSchemaRequest{
Parent: parent,
Schema: msg,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
})
}
func (fx *SchemaServiceSchemaTestSuiteConfig) testGet(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Resource should be returned without errors if it exists.
t.Run("exists", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
msg, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: created.Name,
})
assert.NilError(t, err)
assert.DeepEqual(t, msg, created, protocmp.Transform())
})
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: created.Name + "notfound",
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name only contains wildcards ('-')
t.Run("only wildcards", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.GetSchema(fx.ctx, &GetSchemaRequest{
Name: "projects/-/schemas/-",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *SchemaServiceSchemaTestSuiteConfig) testList(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if provided parent is invalid.
t.Run("invalid parent", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page token is not valid.
t.Run("invalid page token", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageToken: "invalid page token",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument is provided page size is negative.
t.Run("negative page size", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
_, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: -10,
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
const resourcesCount = 15
parent := fx.nextParent(t, true)
parentMsgs := make([]*Schema, resourcesCount)
for i := 0; i < resourcesCount; i++ {
parentMsgs[i] = fx.create(t, parent)
}
// If parent is provided the method must only return resources
// under that parent.
t.Run("isolation", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: 999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs,
response.Schemas,
cmpopts.SortSlices(func(a, b *Schema) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// If there are no more resources, next_page_token should not be set.
t.Run("last page", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: resourcesCount,
})
assert.NilError(t, err)
assert.Equal(t, "", response.NextPageToken)
})
// If there are more resources, next_page_token should be set.
t.Run("more pages", func(t *testing.T) {
fx.maybeSkip(t)
response, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: resourcesCount - 1,
})
assert.NilError(t, err)
assert.Check(t, response.NextPageToken != "")
})
// Listing resource one by one should eventually return all resources.
t.Run("one by one", func(t *testing.T) {
fx.maybeSkip(t)
msgs := make([]*Schema, 0, resourcesCount)
var nextPageToken string
for {
response, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: 1,
PageToken: nextPageToken,
})
assert.NilError(t, err)
assert.Equal(t, 1, len(response.Schemas))
msgs = append(msgs, response.Schemas...)
nextPageToken = response.NextPageToken
if nextPageToken == "" {
break
}
}
assert.DeepEqual(
t,
parentMsgs,
msgs,
cmpopts.SortSlices(func(a, b *Schema) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
// Method should not return deleted resources.
t.Run("deleted", func(t *testing.T) {
fx.maybeSkip(t)
const deleteCount = 5
for i := 0; i < deleteCount; i++ {
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: parentMsgs[i].Name,
})
assert.NilError(t, err)
}
response, err := fx.service.ListSchemas(fx.ctx, &ListSchemasRequest{
Parent: parent,
PageSize: 9999,
})
assert.NilError(t, err)
assert.DeepEqual(
t,
parentMsgs[deleteCount:],
response.Schemas,
cmpopts.SortSlices(func(a, b *Schema) bool {
return a.Name < b.Name
}),
protocmp.Transform(),
)
})
}
func (fx *SchemaServiceSchemaTestSuiteConfig) testDelete(t *testing.T) {
fx.maybeSkip(t)
// Method should fail with InvalidArgument if no name is provided.
t.Run("missing name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: "",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name is not valid.
t.Run("invalid name", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: "invalid resource name",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
// Resource should be deleted without errors if it exists.
t.Run("exists", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: created.Name,
})
assert.NilError(t, err)
})
// Method should fail with NotFound if the resource does not exist.
t.Run("not found", func(t *testing.T) {
fx.maybeSkip(t)
parent := fx.nextParent(t, false)
created := fx.create(t, parent)
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: created.Name + "notfound",
})
assert.Equal(t, codes.NotFound, status.Code(err), err)
})
// Method should fail with InvalidArgument if the provided name only contains wildcards ('-')
t.Run("only wildcards", func(t *testing.T) {
fx.maybeSkip(t)
_, err := fx.service.DeleteSchemaRevision(fx.ctx, &DeleteSchemaRevisionRequest{
Name: "projects/-/schemas/-",
})
assert.Equal(t, codes.InvalidArgument, status.Code(err), err)
})
}
func (fx *SchemaServiceSchemaTestSuiteConfig) nextParent(t *testing.T, pristine bool) string {
if pristine {
fx.currParent++
}
if fx.currParent >= len(fx.Parents) {
t.Fatal("need at least", fx.currParent+1, "parents")
}
return fx.Parents[fx.currParent]
}
func (fx *SchemaServiceSchemaTestSuiteConfig) peekNextParent(t *testing.T) string {
next := fx.currParent + 1
if next >= len(fx.Parents) {
t.Fatal("need at least", next+1, "parents")
}
return fx.Parents[next]
}
func (fx *SchemaServiceSchemaTestSuiteConfig) maybeSkip(t *testing.T) {
for _, skip := range fx.Skip {
if strings.Contains(t.Name(), skip) {
t.Skip("skipped because of .Skip")
}
}
}
func (fx *SchemaServiceSchemaTestSuiteConfig) create(t *testing.T, parent string) *Schema {
t.Helper()
created, err := fx.service.CreateSchema(fx.ctx, &CreateSchemaRequest{
Parent: parent,
Schema: fx.Create(parent),
})
assert.NilError(t, err)
return created
}