-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication_test.go
More file actions
529 lines (457 loc) · 18.8 KB
/
application_test.go
File metadata and controls
529 lines (457 loc) · 18.8 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
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package codescan
import (
"flag"
"io"
"log"
"os"
"sort"
"testing"
"github.com/go-openapi/spec"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
var (
petstoreCtx *scanCtx //nolint:gochecknoglobals // test package cache shared across test functions
classificationCtx *scanCtx //nolint:gochecknoglobals // test package cache shared across test functions
)
var (
enableSpecOutput bool //nolint:gochecknoglobals // test flag registered in init
enableDebug bool //nolint:gochecknoglobals // test flag registered in init
)
func init() { //nolint:gochecknoinits // registers test flags before TestMain
flag.BoolVar(&enableSpecOutput, "enable-spec-output", false, "enable spec gen test to write output to a file")
flag.BoolVar(&enableDebug, "enable-debug", false, "enable debug output in tests")
}
func TestMain(m *testing.M) {
// initializations to run tests in this package
flag.Parse()
if !enableDebug {
log.SetOutput(io.Discard)
} else {
// enable full debug when test is run with -enable-debug arg
Debug = true
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetOutput(os.Stderr)
}
os.Exit(m.Run())
}
func TestApplication_LoadCode(t *testing.T) {
sctx := loadClassificationPkgsCtx(t)
require.NotNil(t, sctx)
require.Len(t, sctx.app.Models, 39)
require.Len(t, sctx.app.Meta, 1)
require.Len(t, sctx.app.Routes, 7)
require.Empty(t, sctx.app.Operations)
require.Len(t, sctx.app.Parameters, 10)
require.Len(t, sctx.app.Responses, 11)
}
func TestAppScanner_NewSpec(t *testing.T) {
doc, err := Run(&Options{
Packages: []string{"./goparsing/petstore/..."},
WorkDir: "fixtures",
})
require.NoError(t, err)
require.NotNil(t, doc)
verifyParsedPetStore(t, doc)
}
func TestAppScanner_Definitions(t *testing.T) {
doc, err := Run(&Options{
Packages: []string{"./goparsing/bookings/..."},
WorkDir: "fixtures",
ScanModels: true,
})
require.NoError(t, err)
if assert.NotNil(t, doc) {
_, ok := doc.Definitions["Booking"]
assert.TrueT(t, ok, "Should include cross repo structs")
_, ok = doc.Definitions["Customer"]
assert.TrueT(t, ok, "Should include package structs with swagger:model")
_, ok = doc.Definitions["DateRange"]
assert.TrueT(t, ok, "Should include package structs that are used in responses")
_, ok = doc.Definitions["BookingResponse"]
assert.FalseT(t, ok, "Should not include responses")
_, ok = doc.Definitions["IgnoreMe"]
assert.FalseT(t, ok, "Should not include un-annotated/un-referenced structs")
}
}
func loadPetstorePkgsCtx(t *testing.T) *scanCtx {
t.Helper()
if petstoreCtx != nil {
return petstoreCtx
}
sctx, err := newScanCtx(&Options{
Packages: []string{"./goparsing/petstore/..."},
WorkDir: "fixtures",
})
require.NoError(t, err)
petstoreCtx = sctx
return petstoreCtx
}
func loadClassificationPkgsCtx(t *testing.T) *scanCtx {
t.Helper()
if classificationCtx != nil {
return classificationCtx
}
sctx, err := newScanCtx(&Options{
Packages: []string{
"./goparsing/classification",
"./goparsing/classification/models",
"./goparsing/classification/operations",
},
WorkDir: "fixtures",
})
require.NoError(t, err)
classificationCtx = sctx
return classificationCtx
}
func verifyParsedPetStore(t *testing.T, doc *spec.Swagger) {
t.Helper()
verifyTop(t, doc)
verifyInfo(t, doc.Info)
verifyModels(t, doc.Definitions)
verifyCommonResponses(t, doc.Responses)
t.Run("with API paths", func(t *testing.T) {
require.NotNil(t, doc.Paths)
paths := doc.Paths.Paths
require.Len(t, paths, 5)
t.Run("with path /pets", func(t *testing.T) {
op, ok := paths["/pets"]
assert.TrueT(t, ok)
assert.NotNil(t, op)
t.Run("with GET: listPets", func(t *testing.T) {
require.NotNil(t, op.Get)
assert.EqualT(t, "Lists the pets known to the store.", op.Get.Summary)
assert.EqualT(t, "By default it will only lists pets that are available for sale.\nThis can be changed with the status flag.", op.Get.Description)
assert.EqualT(t, "listPets", op.Get.ID)
assert.Equal(t, []string{"pets"}, op.Get.Tags)
assert.TrueT(t, op.Get.Deprecated)
names := make(namedParams, 0, len(op.Get.Parameters))
for i, v := range op.Get.Parameters {
names = append(names, namedParam{Index: i, Name: v.Name})
}
sort.Sort(names)
sparam := op.Get.Parameters[names[1].Index]
assert.EqualT(t, "Status\navailable STATUS_AVAILABLE\npending STATUS_PENDING\nsold STATUS_SOLD", sparam.Description)
assert.EqualT(t, "query", sparam.In)
assert.EqualT(t, "string", sparam.Type)
assert.Empty(t, sparam.Format)
assert.Equal(t, []any{"available", "pending", "sold"}, sparam.Enum)
assert.FalseT(t, sparam.Required)
assert.Equal(t, "Status", sparam.Extensions["x-go-name"])
assert.EqualT(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
assert.Len(t, op.Get.Parameters, 2)
sparam1 := op.Get.Parameters[names[0].Index]
assert.EqualT(t, "Birthday", sparam1.Description)
assert.EqualT(t, "query", sparam1.In)
assert.EqualT(t, "string", sparam1.Type)
assert.EqualT(t, "date", sparam1.Format)
assert.FalseT(t, sparam1.Required)
assert.Equal(t, "Birthday", sparam1.Extensions["x-go-name"])
rs, ok := op.Get.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
require.NotNil(t, rs.Schema)
aprop := rs.Schema
assert.EqualT(t, "array", aprop.Type[0])
assert.NotNil(t, aprop.Items)
assert.NotNil(t, aprop.Items.Schema)
assert.EqualT(t, "#/definitions/pet", aprop.Items.Schema.Ref.String())
})
t.Run("with POST: createPet", func(t *testing.T) {
require.NotNil(t, op.Post)
assert.EqualT(t, "Creates a new pet in the store.", op.Post.Summary)
assert.Empty(t, op.Post.Description)
assert.EqualT(t, "createPet", op.Post.ID)
assert.Equal(t, []string{"pets"}, op.Post.Tags)
verifyRefParam(t, op.Post.Parameters[0], "The pet to submit.", "pet")
assert.EqualT(t, "#/responses/genericError", op.Post.Responses.Default.Ref.String())
rs, ok := op.Post.Responses.StatusCodeResponses[200]
assert.TrueT(t, ok)
assert.NotNil(t, rs.Schema)
aprop := rs.Schema
assert.EqualT(t, "#/definitions/pet", aprop.Ref.String())
})
})
t.Run("with path /pets/{id}", func(t *testing.T) {
op, ok := paths["/pets/{id}"]
require.TrueT(t, ok)
require.NotNil(t, op)
t.Run("with GET: getPetById", func(t *testing.T) {
require.NotNil(t, op.Get)
assert.EqualT(t, "Gets the details for a pet.", op.Get.Summary)
assert.Empty(t, op.Get.Description)
assert.EqualT(t, "getPetById", op.Get.ID)
assert.Equal(t, []string{"pets"}, op.Get.Tags)
verifyIDParam(t, op.Get.Parameters[0], "The ID of the pet")
assert.EqualT(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
rs, ok := op.Get.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
require.NotNil(t, rs.Schema)
aprop := rs.Schema
assert.EqualT(t, "#/definitions/pet", aprop.Ref.String())
})
t.Run("with PUT: updatePet", func(t *testing.T) {
require.NotNil(t, op.Put)
assert.EqualT(t, "Updates the details for a pet.", op.Put.Summary)
assert.Empty(t, op.Put.Description)
assert.EqualT(t, "updatePet", op.Put.ID)
assert.Equal(t, []string{"pets"}, op.Put.Tags)
verifyIDParam(t, op.Put.Parameters[0], "The ID of the pet")
verifyRefParam(t, op.Put.Parameters[1], "The pet to submit.", "pet")
assert.EqualT(t, "#/responses/genericError", op.Put.Responses.Default.Ref.String())
rs, ok := op.Put.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
require.NotNil(t, rs.Schema)
aprop := rs.Schema
assert.EqualT(t, "#/definitions/pet", aprop.Ref.String())
})
t.Run("with DELETE: deletePet", func(t *testing.T) {
require.NotNil(t, op.Delete)
assert.EqualT(t, "Deletes a pet from the store.", op.Delete.Summary)
assert.Empty(t, op.Delete.Description)
assert.EqualT(t, "deletePet", op.Delete.ID)
assert.Equal(t, []string{"pets"}, op.Delete.Tags)
verifyIDParam(t, op.Delete.Parameters[0], "The ID of the pet")
assert.EqualT(t, "#/responses/genericError", op.Delete.Responses.Default.Ref.String())
_, ok := op.Delete.Responses.StatusCodeResponses[204]
assert.TrueT(t, ok)
})
})
t.Run("with path /orders/{id}", func(t *testing.T) {
op, ok := paths["/orders/{id}"]
require.TrueT(t, ok)
require.NotNil(t, op)
t.Run("with GET: getOrderDetails", func(t *testing.T) {
require.NotNil(t, op.Get)
assert.EqualT(t, "Gets the details for an order.", op.Get.Summary)
assert.Empty(t, op.Get.Description)
assert.EqualT(t, "getOrderDetails", op.Get.ID)
assert.Equal(t, []string{"orders"}, op.Get.Tags)
verifyIDParam(t, op.Get.Parameters[0], "The ID of the order")
assert.EqualT(t, "#/responses/genericError", op.Get.Responses.Default.Ref.String())
rs, ok := op.Get.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
assert.EqualT(t, "#/responses/orderResponse", rs.Ref.String())
rsm := doc.Responses["orderResponse"]
assert.NotNil(t, rsm.Schema)
assert.EqualT(t, "#/definitions/order", rsm.Schema.Ref.String())
})
t.Run("with DELETE: cancelOrder", func(t *testing.T) {
require.NotNil(t, op.Delete)
assert.EqualT(t, "Deletes an order.", op.Delete.Summary)
assert.Empty(t, op.Delete.Description)
assert.EqualT(t, "cancelOrder", op.Delete.ID)
assert.Equal(t, []string{"orders"}, op.Delete.Tags)
verifyIDParam(t, op.Delete.Parameters[0], "The ID of the order")
assert.EqualT(t, "#/responses/genericError", op.Delete.Responses.Default.Ref.String())
_, ok := op.Delete.Responses.StatusCodeResponses[204]
assert.TrueT(t, ok)
})
t.Run("with PUT: updateOrder", func(t *testing.T) {
require.NotNil(t, op.Put)
assert.EqualT(t, "Updates an order.", op.Put.Summary)
assert.Empty(t, op.Put.Description)
assert.EqualT(t, "updateOrder", op.Put.ID)
assert.Equal(t, []string{"orders"}, op.Put.Tags)
verifyIDParam(t, op.Put.Parameters[0], "The ID of the order")
verifyRefParam(t, op.Put.Parameters[1], "The order to submit", "order")
assert.EqualT(t, "#/responses/genericError", op.Put.Responses.Default.Ref.String())
rs, ok := op.Put.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
require.NotNil(t, rs.Schema)
aprop := rs.Schema
assert.EqualT(t, "#/definitions/order", aprop.Ref.String())
})
})
t.Run("with path /orders", func(t *testing.T) {
op, ok := paths["/orders"]
require.TrueT(t, ok)
require.NotNil(t, op)
t.Run("with POST: createOrder", func(t *testing.T) {
require.NotNil(t, op.Post)
assert.EqualT(t, "Creates an order.", op.Post.Summary)
assert.Empty(t, op.Post.Description)
assert.EqualT(t, "createOrder", op.Post.ID)
assert.Equal(t, []string{"orders"}, op.Post.Tags)
verifyRefParam(t, op.Post.Parameters[0], "The order to submit", "order")
assert.EqualT(t, "#/responses/genericError", op.Post.Responses.Default.Ref.String())
rs, ok := op.Post.Responses.StatusCodeResponses[200]
require.TrueT(t, ok)
assert.EqualT(t, "#/responses/orderResponse", rs.Ref.String())
rsm := doc.Responses["orderResponse"]
require.NotNil(t, rsm.Schema)
assert.EqualT(t, "#/definitions/order", rsm.Schema.Ref.String())
})
})
})
}
func verifyTop(t *testing.T, doc *spec.Swagger) {
t.Helper()
t.Run("with top level specification", func(t *testing.T) {
t.Run("should consume and produce JSON", func(t *testing.T) {
assert.Equal(t, []string{"application/json"}, doc.Consumes)
assert.Equal(t, []string{"application/json"}, doc.Produces)
})
t.Run("schemes should be http and https", func(t *testing.T) {
assert.Equal(t, []string{"http", "https"}, doc.Schemes)
})
t.Run("API host should be localhost", func(t *testing.T) {
assert.EqualT(t, "localhost", doc.Host)
})
t.Run("check API base path", func(t *testing.T) {
assert.EqualT(t, "/v2", doc.BasePath)
})
})
}
func verifyCommonResponses(t *testing.T, responses map[string]spec.Response) {
t.Helper()
t.Run("with responses", func(t *testing.T) {
require.Len(t, responses, 4)
t.Run("should define genericError", func(t *testing.T) {
resp, ok := responses["genericError"]
require.TrueT(t, ok)
require.NotNil(t, resp.Schema)
assert.Len(t, resp.Schema.Properties, 2)
assertProperty(t, resp.Schema, "integer", "code", "int32", "Code")
assertProperty(t, resp.Schema, "string", "message", "", "Message")
})
t.Run("should define validationError", func(t *testing.T) {
resp, ok := responses["validationError"]
require.TrueT(t, ok)
require.NotNil(t, resp.Schema)
assert.Len(t, resp.Schema.Properties, 3)
assertProperty(t, resp.Schema, "integer", "code", "int32", "Code")
assertProperty(t, resp.Schema, "string", "message", "", "Message")
assertProperty(t, resp.Schema, "string", "field", "", "Field")
})
t.Run("should define MarkdownRender", func(t *testing.T) {
resp, ok := responses["MarkdownRender"]
require.TrueT(t, ok)
require.NotNil(t, resp.Schema)
assert.TrueT(t, resp.Schema.Type.Contains("string"))
})
})
}
func verifyModels(t *testing.T, definitions spec.Definitions) {
t.Helper()
t.Run("with models definitions", func(t *testing.T) {
keys := make([]string, 0, len(definitions))
for k := range definitions {
keys = append(keys, k)
}
require.Len(t, keys, 3)
mod, ok := definitions["tag"]
require.TrueT(t, ok)
assert.Equal(t, spec.StringOrArray([]string{"object"}), mod.Type)
assert.EqualT(t, "A Tag is an extra piece of data to provide more information about a pet.", mod.Title)
assert.EqualT(t, "It is used to describe the animals available in the store.", mod.Description)
assert.Len(t, mod.Required, 2)
assertProperty(t, &mod, "integer", "id", "int64", "ID")
prop, ok := mod.Properties["id"]
require.TrueT(t, ok, "should have had an 'id' property")
assert.EqualT(t, "The id of the tag.", prop.Description)
assertProperty(t, &mod, "string", "value", "", "Value")
prop, ok = mod.Properties["value"]
require.TrueT(t, ok)
assert.EqualT(t, "The value of the tag.", prop.Description)
mod, ok = definitions["pet"]
require.TrueT(t, ok)
assert.Equal(t, spec.StringOrArray([]string{"object"}), mod.Type)
assert.EqualT(t, "A Pet is the main product in the store.", mod.Title)
assert.EqualT(t, "It is used to describe the animals available in the store.", mod.Description)
assert.Len(t, mod.Required, 2)
assertProperty(t, &mod, "integer", "id", "int64", "ID")
prop, ok = mod.Properties["id"]
require.TrueT(t, ok, "should have had an 'id' property")
assert.EqualT(t, "The id of the pet.", prop.Description)
assertProperty(t, &mod, "string", "name", "", "Name")
prop, ok = mod.Properties["name"]
require.TrueT(t, ok)
assert.EqualT(t, "The name of the pet.", prop.Description)
assert.EqualValues(t, 3, *prop.MinLength)
assert.EqualValues(t, 50, *prop.MaxLength)
assert.EqualT(t, "\\w[\\w-]+", prop.Pattern)
assertArrayProperty(t, &mod, "string", "photoUrls", "", "PhotoURLs")
prop, ok = mod.Properties["photoUrls"]
require.TrueT(t, ok)
assert.EqualT(t, "The photo urls for the pet.\nThis only accepts jpeg or png images.", prop.Description)
if assert.NotNil(t, prop.Items) && assert.NotNil(t, prop.Items.Schema) {
assert.EqualT(t, "\\.(jpe?g|png)$", prop.Items.Schema.Pattern)
}
assertProperty(t, &mod, "string", "status", "", "Status")
prop, ok = mod.Properties["status"]
assert.TrueT(t, ok)
assert.EqualT(t, "The current status of the pet in the store.\navailable STATUS_AVAILABLE\npending STATUS_PENDING\nsold STATUS_SOLD", prop.Description)
assert.Equal(t, []any{"available", "pending", "sold"}, prop.Enum)
assertProperty(t, &mod, "string", "birthday", "date", "Birthday")
prop, ok = mod.Properties["birthday"]
assert.TrueT(t, ok)
assert.EqualT(t, "The pet's birthday", prop.Description)
assertArrayRef(t, &mod, "tags", "Tags", "#/definitions/tag")
prop, ok = mod.Properties["tags"]
assert.TrueT(t, ok)
assert.EqualT(t, "Extra bits of information attached to this pet.", prop.Description)
mod, ok = definitions["order"]
assert.TrueT(t, ok)
assert.Len(t, mod.Properties, 4)
assert.Len(t, mod.Required, 3)
assertProperty(t, &mod, "integer", "id", "int64", "ID")
prop, ok = mod.Properties["id"]
assert.TrueT(t, ok, "should have had an 'id' property")
assert.EqualT(t, "the ID of the order", prop.Description)
assertProperty(t, &mod, "integer", "userId", "int64", "UserID")
prop, ok = mod.Properties["userId"]
assert.TrueT(t, ok, "should have had an 'userId' property")
assert.EqualT(t, "the id of the user who placed the order.", prop.Description)
assertProperty(t, &mod, "string", "orderedAt", "date-time", "OrderedAt")
prop, ok = mod.Properties["orderedAt"]
assert.EqualT(t, "the time at which this order was made.", prop.Description)
assert.TrueT(t, ok, "should have an 'orderedAt' property")
assertArrayProperty(t, &mod, "object", "items", "", "Items")
prop, ok = mod.Properties["items"]
assert.TrueT(t, ok, "should have an 'items' slice")
assert.NotNil(t, prop.Items, "items should have had an items property")
assert.NotNil(t, prop.Items.Schema, "items.items should have had a schema property")
itprop := prop.Items.Schema
assert.Len(t, itprop.Properties, 2)
assert.Len(t, itprop.Required, 2)
assertProperty(t, itprop, "integer", "petId", "int64", "PetID")
iprop, ok := itprop.Properties["petId"]
assert.TrueT(t, ok, "should have had a 'petId' property")
assert.EqualT(t, "the id of the pet to order", iprop.Description)
assertProperty(t, itprop, "integer", "qty", "int32", "Quantity")
iprop, ok = itprop.Properties["qty"]
assert.TrueT(t, ok, "should have had a 'qty' property")
assert.EqualT(t, "the quantity of this pet to order", iprop.Description)
require.NotNil(t, iprop.Minimum)
assert.InDeltaT(t, 1.00, *iprop.Minimum, epsilon)
})
}
func verifyIDParam(t *testing.T, param spec.Parameter, description string) {
t.Helper()
assert.EqualT(t, description, param.Description)
assert.EqualT(t, "path", param.In)
assert.EqualT(t, "integer", param.Type)
assert.EqualT(t, "int64", param.Format)
assert.TrueT(t, param.Required)
assert.Equal(t, "ID", param.Extensions["x-go-name"])
}
func verifyRefParam(t *testing.T, param spec.Parameter, description, refed string) {
t.Helper()
assert.EqualT(t, description, param.Description)
assert.EqualT(t, "body", param.In)
// TODO: this may fail sometimes (seen on go1.12 windows test): require pointer to be valid and avoid panicking
require.NotNil(t, param)
require.NotNil(t, param.Schema)
assert.EqualT(t, "#/definitions/"+refed, param.Schema.Ref.String())
assert.TrueT(t, param.Required)
}
type namedParam struct {
Index int
Name string
}
type namedParams []namedParam
func (g namedParams) Len() int { return len(g) }
func (g namedParams) Swap(i, j int) { g[i], g[j] = g[j], g[i] }
func (g namedParams) Less(i, j int) bool { return g[i].Name < g[j].Name }