@@ -2,9 +2,59 @@ package docs
2
2
3
3
import (
4
4
"bytes"
5
+ "reflect"
5
6
"testing"
6
7
)
7
8
9
+ const (
10
+ buildStreamTestWant = `openapi: 3.0.1
11
+ info:
12
+ title: Test
13
+ description: Test object
14
+ termsOfService: ""
15
+ contact:
16
+ email: ""
17
+ license:
18
+ name: ""
19
+ url: ""
20
+ version: ""
21
+ externalDocs:
22
+ description: ""
23
+ url: ""
24
+ servers: []
25
+ tags: []
26
+ paths: {}
27
+ components:
28
+ schemas:
29
+ schema_testing:
30
+ properties:
31
+ EnumProp:
32
+ description: short desc
33
+ enum:
34
+ - enum
35
+ - test
36
+ - strSlc
37
+ type: enum
38
+ intProp:
39
+ default: 1337
40
+ description: short desc
41
+ format: int64
42
+ type: integer
43
+ type: ""
44
+ xml:
45
+ name: XML entry test
46
+ securitySchemes:
47
+ ses_scheme_testing:
48
+ flows:
49
+ implicit:
50
+ authorizationUrl: http://petstore.swagger.io/oauth/dialog
51
+ scopes:
52
+ read:pets: Read Pets
53
+ write:pets: Write to Pets
54
+ in: not empty
55
+ `
56
+ )
57
+
8
58
func TestUnitBuild (t * testing.T ) {
9
59
t .Parallel ()
10
60
@@ -114,6 +164,8 @@ func TestUnitGetPathFromFirstElem(t *testing.T) {
114
164
// QUICK CHECK TESTS ARE COMING WITH NEXT RELEASE.
115
165
116
166
func TestOAS_BuildStream (t * testing.T ) {
167
+ t .Parallel ()
168
+
117
169
tests := []struct {
118
170
name string
119
171
oas * OAS
@@ -124,27 +176,19 @@ func TestOAS_BuildStream(t *testing.T) {
124
176
name : "success" ,
125
177
oas : & OAS {
126
178
OASVersion : "3.0.1" ,
127
- Info : Info {
128
- Title : "Test" ,
129
- Description : "Test object" ,
130
- },
179
+ Info : Info {Title : "Test" , Description : "Test object" },
131
180
Components : Components {
132
181
Component {
133
182
Schemas : Schemas {Schema {
134
183
Name : "schema_testing" ,
135
184
Properties : SchemaProperties {
136
185
SchemaProperty {
137
- Name : "EnumProp" ,
138
- Type : "enum" ,
139
- Description : "short desc" ,
140
- Enum : []string {"enum" , "test" , "strSlc" },
186
+ Name : "EnumProp" , Type : "enum" , Description : "short desc" ,
187
+ Enum : []string {"enum" , "test" , "strSlc" },
141
188
},
142
189
SchemaProperty {
143
- Name : "intProp" ,
144
- Type : "integer" ,
145
- Format : "int64" ,
146
- Description : "short desc" ,
147
- Default : 1337 ,
190
+ Name : "intProp" , Type : "integer" , Format : "int64" ,
191
+ Description : "short desc" , Default : 1337 ,
148
192
},
149
193
},
150
194
XML : XMLEntry {Name : "XML entry test" },
@@ -156,79 +200,134 @@ func TestOAS_BuildStream(t *testing.T) {
156
200
Type : "implicit" ,
157
201
AuthURL : "http://petstore.swagger.io/oauth/dialog" ,
158
202
Scopes : SecurityScopes {
159
- SecurityScope {
160
- Name : "write:pets" ,
161
- Description : "Write to Pets" ,
162
- },
163
- SecurityScope {
164
- Name : "read:pets" ,
165
- Description : "Read Pets" ,
166
- },
203
+ SecurityScope {Name : "write:pets" , Description : "Write to Pets" },
204
+ SecurityScope {Name : "read:pets" , Description : "Read Pets" },
167
205
},
168
206
}},
169
207
}},
170
208
},
171
209
},
172
210
},
173
211
wantErr : false ,
174
- wantW : `openapi: 3.0.1
175
- info:
176
- title: Test
177
- description: Test object
178
- termsOfService: ""
179
- contact:
180
- email: ""
181
- license:
182
- name: ""
183
- url: ""
184
- version: ""
185
- externalDocs:
186
- description: ""
187
- url: ""
188
- servers: []
189
- tags: []
190
- paths: {}
191
- components:
192
- schemas:
193
- schema_testing:
194
- $ref: ""
195
- properties:
196
- EnumProp:
197
- description: short desc
198
- enum:
199
- - enum
200
- - test
201
- - strSlc
202
- type: enum
203
- intProp:
204
- default: 1337
205
- description: short desc
206
- format: int64
207
- type: integer
208
- type: ""
209
- xml:
210
- name: XML entry test
211
- securitySchemes:
212
- ses_scheme_testing:
213
- flows:
214
- implicit:
215
- authorizationUrl: http://petstore.swagger.io/oauth/dialog
216
- scopes:
217
- read:pets: Read Pets
218
- write:pets: Write to Pets
219
- in: not empty
220
- ` ,
212
+ wantW : buildStreamTestWant ,
221
213
},
222
214
}
215
+
223
216
for _ , tt := range tests {
224
- t .Run (tt .name , func (t * testing.T ) {
217
+ trn := tt
218
+
219
+ t .Run (trn .name , func (t * testing.T ) {
220
+ t .Parallel ()
221
+
225
222
w := & bytes.Buffer {}
226
- if err := tt .oas .BuildStream (w ); (err != nil ) != tt .wantErr {
227
- t .Errorf ("OAS.BuildStream() error = %v, wantErr %v" , err , tt .wantErr )
223
+ if err := trn .oas .BuildStream (w ); (err != nil ) != trn .wantErr {
224
+ t .Errorf ("OAS.BuildStream() error = %v, wantErr %v" , err , trn .wantErr )
228
225
return
229
226
}
230
- if gotW := w .String (); gotW != tt .wantW {
231
- t .Errorf ("OAS.BuildStream() = [%v], want {%v}" , gotW , tt .wantW )
227
+ if gotW := w .String (); gotW != trn .wantW {
228
+ t .Errorf ("OAS.BuildStream() = [%v], want {%v}" , gotW , trn .wantW )
229
+ }
230
+ })
231
+ }
232
+ }
233
+
234
+ func Test_makeParametersMap (t * testing.T ) {
235
+ type args struct {
236
+ parameters * Parameters
237
+ }
238
+ tests := []struct {
239
+ name string
240
+ args args
241
+ want []map [string ]interface {}
242
+ }{
243
+ {
244
+ name : "success-minimal" ,
245
+ args : args {
246
+ parameters : & Parameters {{
247
+ Name : "id" ,
248
+ In : "path" ,
249
+ Description : "test" ,
250
+ Required : true ,
251
+ Schema : Schema {Name : "id" , Type : "integer" },
252
+ }},
253
+ },
254
+ want : []map [string ]interface {}{{
255
+ "name" : "id" ,
256
+ "in" : "path" ,
257
+ "description" : "test" ,
258
+ "required" : true ,
259
+ "schema" : map [string ]interface {}{"name" : "id" , "type" : "integer" },
260
+ }},
261
+ },
262
+ {
263
+ name : "success-full" ,
264
+ args : args {
265
+ parameters : & Parameters {{
266
+ Name : "id" ,
267
+ In : "path" ,
268
+ Description : "test" ,
269
+ Required : true ,
270
+ Schema : Schema {
271
+ Name : "id" ,
272
+ Type : "integer" ,
273
+ Properties : SchemaProperties {{Name : "id" , Type : "integer" }},
274
+ },
275
+ }},
276
+ },
277
+ want : []map [string ]interface {}{{
278
+ "name" : "id" ,
279
+ "in" : "path" ,
280
+ "description" : "test" ,
281
+ "required" : true ,
282
+ "schema" : map [string ]interface {}{"name" : "id" , "type" : "integer" ,
283
+ "properties" : map [string ]interface {}{
284
+ "id" : map [string ]interface {}{"type" : "integer" },
285
+ }},
286
+ }},
287
+ },
288
+ {
289
+ name : "success-ref" ,
290
+ args : args {
291
+ parameters : & Parameters {{
292
+ Name : "id" ,
293
+ In : "path" ,
294
+ Description : "test" ,
295
+ Required : true ,
296
+ Schema : Schema {Ref : "$some-ref" },
297
+ }},
298
+ },
299
+ want : []map [string ]interface {}{{
300
+ "name" : "id" ,
301
+ "in" : "path" ,
302
+ "description" : "test" ,
303
+ "required" : true ,
304
+ "schema" : map [string ]interface {}{"$ref" : "$some-ref" },
305
+ }},
306
+ },
307
+ {
308
+ name : "success-xml-entry" ,
309
+ args : args {
310
+ parameters : & Parameters {{
311
+ Name : "id" ,
312
+ In : "path" ,
313
+ Description : "test" ,
314
+ Required : true ,
315
+ Schema : Schema {Name : "id" , Type : "integer" , XML : XMLEntry {Name : "id" }},
316
+ }},
317
+ },
318
+ want : []map [string ]interface {}{{
319
+ "name" : "id" ,
320
+ "in" : "path" ,
321
+ "description" : "test" ,
322
+ "required" : true ,
323
+ "schema" : map [string ]interface {}{"name" : "id" , "type" : "integer" , "xml" : map [string ]interface {}{"name" : "id" }},
324
+ }},
325
+ },
326
+ }
327
+ for _ , tt := range tests {
328
+ t .Run (tt .name , func (t * testing.T ) {
329
+ if got := makeParametersMap (tt .args .parameters ); ! reflect .DeepEqual (got , tt .want ) {
330
+ t .Errorf ("makeParametersMap() = %+v, want %+v" , got , tt .want )
232
331
}
233
332
})
234
333
}
0 commit comments