forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum_test.go
497 lines (474 loc) · 20 KB
/
enum_test.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2015 go-swagger maintainers
//
// 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
//
// http://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 generator
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/go-openapi/loads"
"github.com/stretchr/testify/assert"
)
func TestEnum_StringThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "StringThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("string_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var stringThingEnum []interface{}", res)
assertInCode(t, k+") validateStringThingEnum(path, location string, value StringThing)", res)
assertInCode(t, "m.validateStringThingEnum(\"\", \"body\", m)", res)
}
}
}
}
}
func TestEnum_ComposedThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "ComposedThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("composed_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "m.StringThing.Validate(formats)", res)
assertInCode(t, "var composedThingTypeNamePropEnum []interface{}", res)
assertInCode(t, "m.validateNameEnum(\"name\", \"body\", *m.Name)", res)
assertInCode(t, k+") validateNameEnum(path, location string, value string)", res)
}
}
}
}
}
func TestEnum_IntThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "IntThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("int_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var intThingEnum []interface{}", res)
assertInCode(t, k+") validateIntThingEnum(path, location string, value IntThing)", res)
assertInCode(t, "m.validateIntThingEnum(\"\", \"body\", m)", res)
}
}
}
}
}
func TestEnum_FloatThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "FloatThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("float_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var floatThingEnum []interface{}", res)
assertInCode(t, k+") validateFloatThingEnum(path, location string, value FloatThing)", res)
assertInCode(t, "m.validateFloatThingEnum(\"\", \"body\", m)", res)
}
}
}
}
}
func TestEnum_SliceThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "SliceThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("slice_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var sliceThingEnum []interface{}", res)
assertInCode(t, k+") validateSliceThingEnum(path, location string, value []string)", res)
assertInCode(t, "m.validateSliceThingEnum(\"\", \"body\", m)", res)
}
}
}
}
}
func TestEnum_SliceAndItemsThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "SliceAndItemsThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("slice_and_items_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var sliceAndItemsThingEnum []interface{}", res)
assertInCode(t, k+") validateSliceAndItemsThingEnum(path, location string, value []string)", res)
assertInCode(t, "m.validateSliceAndItemsThingEnum(\"\", \"body\", m)", res)
assertInCode(t, "var sliceAndItemsThingItemsEnum []interface{}", res)
assertInCode(t, k+") validateSliceAndItemsThingItemsEnum(path, location string, value string)", res)
assertInCode(t, "m.validateSliceAndItemsThingItemsEnum(strconv.Itoa(i), \"body\", m[i])", res)
}
}
}
}
}
func TestEnum_SliceAndAdditionalItemsThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "SliceAndAdditionalItemsThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("slice_and_additional_items_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var sliceAndAdditionalItemsThingEnum []interface{}", res)
assertInCode(t, k+") validateSliceAndAdditionalItemsThingEnum(path, location string, value *SliceAndAdditionalItemsThing)", res)
//assertInCode(t, "m.validateSliceAndAdditionalItemsThingEnum(\"\", \"body\", m)", res)
assertInCode(t, "var sliceAndAdditionalItemsThingTypeP0PropEnum []interface{}", res)
assertInCode(t, k+") validateP0Enum(path, location string, value string)", res)
assertInCode(t, "m.validateP0Enum(\"0\", \"body\", *m.P0)", res)
assertInCode(t, "var sliceAndAdditionalItemsThingItemsEnum []interface{}", res)
assertInCode(t, k+") validateSliceAndAdditionalItemsThingItemsEnum(path, location string, value float32)", res)
assertInCode(t, "m.validateSliceAndAdditionalItemsThingItemsEnum(strconv.Itoa(i+1), \"body\", m.SliceAndAdditionalItemsThingItems[i])", res)
}
}
}
}
}
func TestEnum_MapThing(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "MapThing"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("map_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var mapThingEnum []interface{}", res)
assertInCode(t, k+") validateMapThingEnum(path, location string, value MapThing)", res)
assertInCode(t, "m.validateMapThingEnum(\"\", \"body\", m)", res)
assertInCode(t, "var mapThingValueEnum []interface{}", res)
assertInCode(t, k+") validateMapThingValueEnum(path, location string, value string)", res)
assertInCode(t, "m.validateMapThingValueEnum(k, \"body\", m[k])", res)
} else {
fmt.Println(buf.String())
}
}
}
}
}
func TestEnum_ObjectThing(t *testing.T) {
// verify that additionalItems render the same from an expanded and a flattened spec
// known issue: there are some slight differences in generated code and variables for enum,
// depending on how the spec has been preprocessed
specs := []string{
"../fixtures/codegen/todolist.enums.yml",
"../fixtures/codegen/todolist.enums.flattened.json", // this one is the first one, after "swagger flatten"
}
k := "ObjectThing"
for _, fixture := range specs {
t.Logf("%s from spec: %s", k, fixture)
specDoc, err := loads.Spec(fixture)
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("object_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
// all these remain unaffected
assertInCode(t, "var objectThingTypeNamePropEnum []interface{}", res)
assertInCode(t, "var objectThingTypeFlowerPropEnum []interface{}", res)
assertInCode(t, "var objectThingTypeFlourPropEnum []interface{}", res)
assertInCode(t, "var objectThingTypeWolvesPropEnum []interface{}", res)
assertInCode(t, "var objectThingWolvesValueEnum []interface{}", res)
assertInCode(t, "var objectThingCatsItemsEnum []interface{}", res)
assertInCode(t, k+") validateNameEnum(path, location string, value string)", res)
assertInCode(t, k+") validateFlowerEnum(path, location string, value int32)", res)
assertInCode(t, k+") validateFlourEnum(path, location string, value float32)", res)
assertInCode(t, k+") validateWolvesEnum(path, location string, value map[string]string)", res)
assertInCode(t, k+") validateWolvesValueEnum(path, location string, value string)", res)
assertInCode(t, k+") validateCatsItemsEnum(path, location string, value string)", res)
assertInCode(t, k+") validateCats(", res)
assertInCode(t, "m.validateNameEnum(\"name\", \"body\", *m.Name)", res)
assertInCode(t, "m.validateFlowerEnum(\"flower\", \"body\", m.Flower)", res)
assertInCode(t, "m.validateFlourEnum(\"flour\", \"body\", m.Flour)", res)
assertInCode(t, "m.validateWolvesEnum(\"wolves\", \"body\", m.Wolves)", res)
assertInCode(t, "m.validateWolvesValueEnum(\"wolves\"+\".\"+k, \"body\", m.Wolves[k])", res)
assertInCode(t, "m.validateCatsItemsEnum(\"cats\"+\".\"+strconv.Itoa(i), \"body\", m.Cats[i])", res)
// small naming differences may be found between the expand and the flatten version of spec
namingDifference := "Tuple0"
pathDifference := "P"
if strings.Contains(fixture, "flattened") {
// when expanded, all defs are in the same template for AdditionalItems
schema := definitions["objectThingLions"]
genModel, err = makeGenDefinition("ObjectThingLions", "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf = bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("object_thing_lions.go", buf.Bytes())
if assert.NoError(t, err) {
res = string(ff)
}
}
}
namingDifference = ""
pathDifference = ""
}
// now common check resumes
assertInCode(t, "var objectThingLions"+namingDifference+"TypeP0PropEnum []interface{}", res)
assertInCode(t, "var objectThingLions"+namingDifference+"TypeP1PropEnum []interface{}", res)
assertInCode(t, "var objectThingLions"+namingDifference+"ItemsEnum []interface{}", res)
assertInCode(t, "m.validateP1Enum(\""+pathDifference+"1\", \"body\", *m.P1)", res)
assertInCode(t, "m.validateP0Enum(\""+pathDifference+"0\", \"body\", *m.P0)", res)
assertInCode(t, k+"Lions"+namingDifference+") validateObjectThingLions"+namingDifference+"ItemsEnum(path, location string, value float64)", res)
if namingDifference != "" {
assertInCode(t, "m.validateObjectThingLions"+namingDifference+"ItemsEnum(strconv.Itoa(i), \"body\", m.ObjectThingLions"+namingDifference+"Items[i])", res)
assertInCode(t, "var objectThingTypeLionsPropEnum []interface{}", res)
assertInCode(t, k+") validateLionsEnum(path, location string, value float64)", res)
} else {
assertInCode(t, "m.validateObjectThingLions"+namingDifference+"ItemsEnum(strconv.Itoa(i+2), \"body\", m.ObjectThingLions"+namingDifference+"Items[i])", res)
assertInCode(t, "var objectThingLionsItemsEnum []interface{}", res)
assertInCode(t, k+"Lions) validateObjectThingLionsItemsEnum(path, location string, value float64)", res)
}
}
}
}
}
}
}
func TestEnum_ComputeInstance(t *testing.T) {
// ensure that the enum validation for the anonymous object under the delegate property
// is rendered.
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "ComputeInstance"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("object_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "Region *string `json:\"region\"`", res)
assertInCode(t, "var computeInstanceTypeRegionPropEnum []interface{}", res)
assertInCode(t, "m.validateRegionEnum(\"region\", \"body\", *m.Region)", res)
}
}
}
}
}
func TestEnum_Cluster(t *testing.T) {
// ensure that the enum validation for the anonymous object under the delegate property
// is rendered.
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "Cluster"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("object_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "Data *ClusterData `json:\"data\"`", res)
assertInCode(t, `ClusterDataStatusScheduled string = "scheduled"`, res)
assertInCode(t, `ClusterDataStatusBuilding string = "building"`, res)
assertInCode(t, `ClusterDataStatusUp string = "up"`, res)
assertInCode(t, `ClusterDataStatusDeleting string = "deleting"`, res)
assertInCode(t, `ClusterDataStatusExited string = "exited"`, res)
assertInCode(t, `ClusterDataStatusError string = "error"`, res)
}
}
}
}
}
func TestEnum_NewPrototype(t *testing.T) {
// ensure that the enum validation for the anonymous object under the delegate property
// is rendered.
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "NewPrototype"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("object_thing.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "ActivatingUser *NewPrototypeActivatingUser `json:\"activating_user,omitempty\"`", res)
assertInCode(t, "Delegate *NewPrototypeDelegate `json:\"delegate\"`", res)
assertInCode(t, "Role *string `json:\"role\"`", res)
assertInCode(t, "var newPrototypeTypeRolePropEnum []interface{}", res)
assertInCode(t, "var newPrototypeDelegateTypeKindPropEnum []interface{}", res)
assertInCode(t, "m.validateDelegate(formats)", res)
assertInCode(t, "m.validateRole(formats)", res)
assertInCode(t, "m.validateActivatingUser(formats)", res)
assertInCode(t, "m.Delegate.Validate(formats)", res)
assertInCode(t, "m.ActivatingUser.Validate(formats)", res)
}
}
}
}
}
func TestEnum_Issue265(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/sodabooth.json")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "SodaBrand"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("soda_brand.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assert.Equal(t, 1, strings.Count(res, "m.validateSodaBrandEnum"))
}
}
}
}
}
func TestEnum_Issue325(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/sodabooths.json")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "SodaBrand"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err = templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, ferr := opts.LanguageOpts.FormatContent("soda_brand.go", buf.Bytes())
if assert.NoError(t, ferr) {
res := string(ff)
assertInCode(t, "var sodaBrandEnum []interface{}", res)
assertInCode(t, "err := validate.Enum(path, location, value, sodaBrandEnum)", res)
assert.Equal(t, 1, strings.Count(res, "m.validateSodaBrandEnum"))
}
}
}
k = "Soda"
schema = definitions[k]
genModel, err = makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("soda.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, "var sodaTypeBrandPropEnum []interface{}", res)
assertInCode(t, "err := validate.Enum(path, location, value, sodaTypeBrandPropEnum)", res)
assert.Equal(t, 1, strings.Count(res, "m.validateBrandEnum"))
}
}
}
}
}
func TestEnum_Issue352(t *testing.T) {
specDoc, err := loads.Spec("../fixtures/codegen/todolist.enums.yml")
if assert.NoError(t, err) {
definitions := specDoc.Spec().Definitions
k := "slp_action_enum"
schema := definitions[k]
opts := opts()
genModel, err := makeGenDefinition(k, "models", schema, specDoc, opts)
if assert.NoError(t, err) {
buf := bytes.NewBuffer(nil)
err := templates.MustGet("model").Execute(buf, genModel)
if assert.NoError(t, err) {
ff, err := opts.LanguageOpts.FormatContent("slp_action_enum.go", buf.Bytes())
if assert.NoError(t, err) {
res := string(ff)
assertInCode(t, ", value SlpActionEnum", res)
}
}
}
}
}