Skip to content

Commit 7e52966

Browse files
authored
test: added tests (fixed bug: multipleOf) (#6)
Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent af7ed75 commit 7e52966

File tree

7 files changed

+71
-4
lines changed

7 files changed

+71
-4
lines changed

application_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ func TestApplication_LoadCode(t *testing.T) {
5959
require.Len(t, sctx.app.Responses, 11)
6060
}
6161

62+
func TestApplication_DebugLogging(t *testing.T) {
63+
// Exercises the debugLogf code path with Debug: true.
64+
_, err := Run(&Options{
65+
Packages: []string{"./goparsing/petstore/..."},
66+
WorkDir: "fixtures",
67+
ScanModels: true,
68+
Debug: true,
69+
})
70+
require.NoError(t, err)
71+
}
72+
73+
func TestRun_InvalidWorkDir(t *testing.T) {
74+
// Exercises the Run() error path when package loading fails.
75+
_, err := Run(&Options{
76+
Packages: []string{"./..."},
77+
WorkDir: "/nonexistent/directory",
78+
})
79+
require.Error(t, err)
80+
}
81+
6282
func TestAppScanner_NewSpec(t *testing.T) {
6383
doc, err := Run(&Options{
6484
Packages: []string{"./goparsing/petstore/..."},

fixtures/goparsing/classification/operations/noparams.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,18 @@ type NoParams struct {
226226
// in: query
227227
BarSlice [][][]string `json:"bar_slice"`
228228

229+
// a NumSlice has numeric items with item-level validation
230+
//
231+
// min items: 1
232+
// max items: 20
233+
// items.minimum: 5
234+
// items.maximum: 100
235+
// items.multiple of: 5
236+
// items.unique: true
237+
// items.collection format: csv
238+
// in: query
239+
NumSlice []int32 `json:"num_slice"`
240+
229241
// the items for this order
230242
//
231243
// in: body

fixtures/goparsing/go123/special/special_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ type SpecialTypes struct {
6363
PtrFormatUUID *strfmt.UUID
6464
Err error
6565
Map map[string]*GoStruct
66+
NamedArray GoArray
6667

6768
// and what not
6869
WhatNot struct {

parameters_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func assertSomeOperationParams(t *testing.T, operations map[string]*spec.Operati
181181
t.Helper()
182182
op, okParam := operations["someOperation"]
183183
assert.TrueT(t, okParam)
184-
assert.Len(t, op.Parameters, 12)
184+
assert.Len(t, op.Parameters, 13)
185185

186186
for _, param := range op.Parameters {
187187
switch param.Name {
@@ -215,6 +215,8 @@ func assertSomeOperationParams(t *testing.T, operations map[string]*spec.Operati
215215
assert.FalseT(t, param.ExclusiveMinimum)
216216
assert.EqualValues(t, 2, param.Default, "%s default value is incorrect", param.Name)
217217
assert.EqualValues(t, 27, param.Example)
218+
require.NotNil(t, param.MultipleOf)
219+
assert.InDeltaT(t, 3.00, *param.MultipleOf, epsilon)
218220

219221
case paramHdrName:
220222
assert.EqualT(t, "Name of this no model instance", param.Description)
@@ -368,6 +370,26 @@ func assertSomeOperationParams(t *testing.T, operations map[string]*spec.Operati
368370
assert.EqualT(t, int64(10), *itprop3.MaxLength, "'bar_slice.items.items.items.maxLength' should have been 10")
369371
assert.EqualT(t, "\\w+", itprop3.Pattern, "'bar_slice.items.items.items.pattern' should have \\w+")
370372

373+
case "num_slice":
374+
assert.EqualT(t, "a NumSlice has numeric items with item-level validation", param.Description)
375+
assert.EqualT(t, "query", param.In)
376+
assert.EqualT(t, "array", param.Type)
377+
require.NotNil(t, param.MinItems)
378+
assert.EqualT(t, int64(1), *param.MinItems)
379+
require.NotNil(t, param.MaxItems)
380+
assert.EqualT(t, int64(20), *param.MaxItems)
381+
382+
itprop := param.Items
383+
require.NotNil(t, itprop)
384+
require.NotNil(t, itprop.Minimum)
385+
assert.InDeltaT(t, 5.00, *itprop.Minimum, epsilon, "'num_slice.items.minimum' should have been 5")
386+
require.NotNil(t, itprop.Maximum)
387+
assert.InDeltaT(t, 100.00, *itprop.Maximum, epsilon, "'num_slice.items.maximum' should have been 100")
388+
require.NotNil(t, itprop.MultipleOf)
389+
assert.InDeltaT(t, 5.00, *itprop.MultipleOf, epsilon, "'num_slice.items.multipleOf' should have been 5")
390+
assert.TrueT(t, itprop.UniqueItems, "'num_slice.items' should have been unique")
391+
assert.EqualT(t, "csv", itprop.CollectionFormat, "'num_slice.items.collectionFormat' should have been csv")
392+
371393
default:
372394
assert.Fail(t, "unknown property: "+param.Name)
373395
}
@@ -379,7 +401,7 @@ func assertAnotherOperationParamOrder(t *testing.T, operations map[string]*spec.
379401

380402
order, ok := operations["anotherOperation"]
381403
assert.TrueT(t, ok)
382-
assert.Len(t, order.Parameters, 12)
404+
assert.Len(t, order.Parameters, 13)
383405

384406
for index, param := range order.Parameters {
385407
switch param.Name {
@@ -405,8 +427,10 @@ func assertAnotherOperationParamOrder(t *testing.T, operations map[string]*spec.
405427
assert.EqualT(t, 9, index, "%s index incorrect", param.Name)
406428
case paramBarSlice:
407429
assert.EqualT(t, 10, index, "%s index incorrect", param.Name)
408-
case "items":
430+
case "num_slice":
409431
assert.EqualT(t, 11, index, "%s index incorrect", param.Name)
432+
case "items":
433+
assert.EqualT(t, 12, index, "%s index incorrect", param.Name)
410434
default:
411435
assert.Fail(t, "unknown property: "+param.Name)
412436
}

parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ func (sm *setMultipleOf) Parse(lines []string) error {
721721
return nil
722722
}
723723
matches := sm.rx.FindStringSubmatch(lines[0])
724-
if len(matches) > 2 && len(matches[1]) > 0 {
724+
if len(matches) > 1 && len(matches[1]) > 0 {
725725
multipleOf, err := strconv.ParseFloat(matches[1], 64)
726726
if err != nil {
727727
return err

responses_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ func assertSomeResponseHeaders(t *testing.T, responses map[string]spec.Response)
158158
assert.InDeltaT(t, 3.00, *header.Minimum, epsilon)
159159
assert.FalseT(t, header.ExclusiveMinimum)
160160
assert.EqualValues(t, 27, header.Example)
161+
require.NotNil(t, header.MultipleOf, "'score' should have had a multipleOf")
162+
assert.InDeltaT(t, 3.00, *header.MultipleOf, epsilon, "'score' should have had multipleOf 3")
161163

162164
case "x-hdr-name":
163165
assert.EqualT(t, "Name of this some response instance", header.Description)

schema_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ func TestSchemaBuilder(t *testing.T) {
161161
assert.InDeltaT(t, 3.00, *prop.Minimum, epsilon)
162162
assert.FalseT(t, prop.ExclusiveMinimum, "'score' should not have had an exclusive minimum")
163163
assert.EqualValues(t, 27, prop.Example)
164+
require.NotNil(t, prop.MultipleOf, "'score' should have had a multipleOf")
165+
assert.InDeltaT(t, 3.00, *prop.MultipleOf, epsilon, "'score' should have had multipleOf 3")
164166

165167
expectedNameExtensions := spec.Extensions{
166168
"x-go-name": "Name",
@@ -1765,6 +1767,12 @@ func testSpecialTypesStruct(t *testing.T, sp *spec.Swagger) {
17651767
assertIsRef(t, mapSchema, "#/definitions/GoStruct")
17661768
})
17671769

1770+
t.Run("a property which is a named array type should render as a ref", func(t *testing.T) {
1771+
na, ok := props["NamedArray"]
1772+
require.TrueT(t, ok)
1773+
assertIsRef(t, &na, "#/definitions/go_array")
1774+
})
1775+
17681776
testSpecialTypesWhatNot(t, sp, props)
17691777
})
17701778
}

0 commit comments

Comments
 (0)