forked from swaggo/swag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
160 lines (124 loc) · 4.89 KB
/
schema_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
package swag
import (
"go/ast"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
)
func TestValidDataType(t *testing.T) {
t.Parallel()
assert.NoError(t, CheckSchemaType(STRING))
assert.NoError(t, CheckSchemaType(NUMBER))
assert.NoError(t, CheckSchemaType(INTEGER))
assert.NoError(t, CheckSchemaType(BOOLEAN))
assert.NoError(t, CheckSchemaType(ARRAY))
assert.NoError(t, CheckSchemaType(OBJECT))
assert.Error(t, CheckSchemaType("oops"))
}
func TestTransToValidSchemeType(t *testing.T) {
t.Parallel()
assert.Equal(t, TransToValidSchemeType("uint"), INTEGER)
assert.Equal(t, TransToValidSchemeType("uint32"), INTEGER)
assert.Equal(t, TransToValidSchemeType("uint64"), INTEGER)
assert.Equal(t, TransToValidSchemeType("float32"), NUMBER)
assert.Equal(t, TransToValidSchemeType("bool"), BOOLEAN)
assert.Equal(t, TransToValidSchemeType("string"), STRING)
// should accept any type, due to user defined types
other := "oops"
assert.Equal(t, TransToValidSchemeType(other), other)
}
func TestTransToValidCollectionFormat(t *testing.T) {
t.Parallel()
assert.Equal(t, TransToValidCollectionFormat("csv"), "csv")
assert.Equal(t, TransToValidCollectionFormat("multi"), "multi")
assert.Equal(t, TransToValidCollectionFormat("pipes"), "pipes")
assert.Equal(t, TransToValidCollectionFormat("tsv"), "tsv")
assert.Equal(t, TransToValidSchemeType("string"), STRING)
// should accept any type, due to user defined types
assert.Equal(t, TransToValidCollectionFormat("oops"), "")
}
func TestIsGolangPrimitiveType(t *testing.T) {
t.Parallel()
assert.Equal(t, IsGolangPrimitiveType("uint"), true)
assert.Equal(t, IsGolangPrimitiveType("int"), true)
assert.Equal(t, IsGolangPrimitiveType("uint8"), true)
assert.Equal(t, IsGolangPrimitiveType("uint16"), true)
assert.Equal(t, IsGolangPrimitiveType("int16"), true)
assert.Equal(t, IsGolangPrimitiveType("byte"), true)
assert.Equal(t, IsGolangPrimitiveType("uint32"), true)
assert.Equal(t, IsGolangPrimitiveType("int32"), true)
assert.Equal(t, IsGolangPrimitiveType("rune"), true)
assert.Equal(t, IsGolangPrimitiveType("uint64"), true)
assert.Equal(t, IsGolangPrimitiveType("int64"), true)
assert.Equal(t, IsGolangPrimitiveType("float32"), true)
assert.Equal(t, IsGolangPrimitiveType("float64"), true)
assert.Equal(t, IsGolangPrimitiveType("bool"), true)
assert.Equal(t, IsGolangPrimitiveType("string"), true)
assert.Equal(t, IsGolangPrimitiveType("oops"), false)
}
func TestIsSimplePrimitiveType(t *testing.T) {
t.Parallel()
assert.Equal(t, IsSimplePrimitiveType("string"), true)
assert.Equal(t, IsSimplePrimitiveType("number"), true)
assert.Equal(t, IsSimplePrimitiveType("integer"), true)
assert.Equal(t, IsSimplePrimitiveType("boolean"), true)
assert.Equal(t, IsSimplePrimitiveType("oops"), false)
}
func TestBuildCustomSchema(t *testing.T) {
t.Parallel()
var (
schema *spec.Schema
err error
)
schema, err = BuildCustomSchema([]string{})
assert.NoError(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"primitive"})
assert.Error(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"primitive", "oops"})
assert.Error(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"primitive", "string"})
assert.NoError(t, err)
assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"string"})
schema, err = BuildCustomSchema([]string{"array"})
assert.Error(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"array", "oops"})
assert.Error(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"array", "string"})
assert.NoError(t, err)
assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"array"})
assert.Equal(t, schema.SchemaProps.Items.Schema.SchemaProps.Type, spec.StringOrArray{"string"})
schema, err = BuildCustomSchema([]string{"object"})
assert.NoError(t, err)
assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"object"})
schema, err = BuildCustomSchema([]string{"object", "oops"})
assert.Error(t, err)
assert.Nil(t, schema)
schema, err = BuildCustomSchema([]string{"object", "string"})
assert.NoError(t, err)
assert.Equal(t, schema.SchemaProps.Type, spec.StringOrArray{"object"})
assert.Equal(t, schema.SchemaProps.AdditionalProperties.Schema.Type, spec.StringOrArray{"string"})
}
func TestIsNumericType(t *testing.T) {
t.Parallel()
assert.Equal(t, IsNumericType(INTEGER), true)
assert.Equal(t, IsNumericType(NUMBER), true)
assert.Equal(t, IsNumericType(STRING), false)
}
func TestTypeDocName(t *testing.T) {
t.Parallel()
expected := "a/package"
assert.Equal(t, expected, TypeDocName(expected, nil))
expected = "package.Model"
assert.Equal(t, expected, TypeDocName("package", &ast.TypeSpec{Name: &ast.Ident{Name: "Model"}}))
expected = "Model"
assert.Equal(t, expected, TypeDocName("package", &ast.TypeSpec{
Comment: &ast.CommentGroup{
List: []*ast.Comment{{Text: "// @name Model"}},
},
}))
}