forked from emicklei/go-restful-swagger12
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel_builder_test.go
executable file
·173 lines (159 loc) · 3.02 KB
/
model_builder_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
package swagger
import (
"reflect"
"testing"
)
func TestSampleToModelAsJson(t *testing.T) {
testFromStruct(t, sample{items: []item{}}, `{
"sample": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"items": {
"type": "array",
"items": {
"$ref": "#/definitions/item"
}
},
"root": {
"$ref": "#/definitions/item"
}
}
},
"item": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}`)
}
//test anonymous struct
func TestAnonymousPtrStruct(t *testing.T) {
type X struct {
A *struct {
B int
}
}
expected := `{
"X": {
"type": "object",
"properties": {
"A": {
"$ref": "#/definitions/X.A"
}
}
},
"X.A": {
"type": "object",
"properties": {
"B": {
"type": "integer",
"format": "int32"
}
}
}
}`
testFromStruct(t, X{}, expected)
}
type File struct {
History []File
HistoryPtrs []*File
}
//test recursion struct
func TestRecursiveStructure(t *testing.T) {
testFromStruct(t, File{}, `{
"File": {
"type": "object",
"properties": {
"History": {
"type": "array",
"items": {
"$ref": "#/definitions/File"
}
},
"HistoryPtrs": {
"type": "array",
"items": {
"$ref": "#/definitions/File"
}
}
}
}
}`)
}
func TestAtMap(t *testing.T) {
name := []string{"Book", "Student", "Class", "abc", "ABC"}
mapItem := map[string]*Items{}
mapItem["Book"] = &Items{}
mapItem["Student"] = &Items{}
mapItem["Class"] = &Items{}
mapItem["abc"] = &Items{}
mapItem["ABC"] = &Items{}
for _, n := range name {
if _, ok := atMap(n, &mapItem); !ok {
t.Errorf("get %v but not %v", false, true)
}
}
}
func TestIsPrimitiveType(t *testing.T) {
modelName := []string{"uint", "uint8", "uint16", "uint32", "uint64", "int", "int8", "int16",
"int32", "int64", "float32", "float64", "bool", "string", "byte", "rune", "time.Time"}
b := modelBuilder{}
for _, name := range modelName {
if ok := b.isPrimitiveType(name); !ok {
t.Errorf("get %v but not %v", false, true)
}
}
}
type model struct {
strType string
intType int
boolType bool
sliceType []string
ptrType *string
mapType map[string]Example
exaType Example
}
type Example struct {
Id, Name string
}
func TestKeyFrom(t *testing.T) {
typed := []string{"string", "int", "bool", "string", "*string", "map[string]swagger.Example", "swagger.Example"}
b := modelBuilder{}
model := model{}
st := reflect.TypeOf(model)
for i := 0; i < st.NumField(); i++ {
tp := b.keyFrom(st.Field(i).Type)
if tp != typed[i] {
t.Errorf("get %v but not %v", tp, typed[i])
}
}
}
func Test(t *testing.T) {
type X struct {
A int
B int `json:"C"`
D int `json:"-"`
}
expected := `{
"X": {
"type": "object",
"properties": {
"A": {
"type": "integer",
"format": "int32"
},
"C": {
"type": "integer",
"format": "int32"
}
}
}
}`
testFromStruct(t, X{}, expected)
}