Skip to content

Commit 7cbba72

Browse files
committed
Added unit-tests. Small fixes to stream example.
1 parent 002492e commit 7cbba72

File tree

3 files changed

+189
-10
lines changed

3 files changed

+189
-10
lines changed

build_test.go

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package docs
22

3-
import "testing"
3+
import (
4+
"bytes"
5+
"testing"
6+
)
47

58
func TestUnitBuild(t *testing.T) {
69
t.Parallel()
@@ -109,3 +112,124 @@ func TestUnitGetPathFromFirstElem(t *testing.T) {
109112
}
110113

111114
// QUICK CHECK TESTS ARE COMING WITH NEXT RELEASE.
115+
116+
func TestOAS_BuildStream(t *testing.T) {
117+
tests := []struct {
118+
name string
119+
oas *OAS
120+
wantW string
121+
wantErr bool
122+
}{
123+
{
124+
name: "success",
125+
oas: &OAS{
126+
OASVersion: "3.0.1",
127+
Info: Info{
128+
Title: "Test",
129+
Description: "Test object",
130+
},
131+
Components: Components{
132+
Component{
133+
Schemas: Schemas{Schema{
134+
Name: "schema_testing",
135+
Properties: SchemaProperties{
136+
SchemaProperty{
137+
Name: "EnumProp",
138+
Type: "enum",
139+
Description: "short desc",
140+
Enum: []string{"enum", "test", "strSlc"},
141+
},
142+
SchemaProperty{
143+
Name: "intProp",
144+
Type: "integer",
145+
Format: "int64",
146+
Description: "short desc",
147+
Default: 1337,
148+
},
149+
},
150+
XML: XMLEntry{Name: "XML entry test"},
151+
}},
152+
SecuritySchemes: SecuritySchemes{SecurityScheme{
153+
Name: "ses_scheme_testing",
154+
In: "not empty",
155+
Flows: SecurityFlows{SecurityFlow{
156+
Type: "implicit",
157+
AuthURL: "http://petstore.swagger.io/oauth/dialog",
158+
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+
},
167+
},
168+
}},
169+
}},
170+
},
171+
},
172+
},
173+
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+
`,
221+
},
222+
}
223+
for _, tt := range tests {
224+
t.Run(tt.name, func(t *testing.T) {
225+
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)
228+
return
229+
}
230+
if gotW := w.String(); gotW != tt.wantW {
231+
t.Errorf("OAS.BuildStream() = [%v], want {%v}", gotW, tt.wantW)
232+
}
233+
})
234+
}
235+
}

examples/stream_output/main.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353
},
5454
// HandlerFuncName: "handleCreateUser",
5555
RequestBody: docs.RequestBody{
56-
Description: "Create a new User",
56+
Description: "Get a user",
5757
Content: docs.ContentTypes{
5858
getContentApplicationJSON("#/components/schemas/User"),
5959
},
@@ -76,14 +76,6 @@ func main() {
7676
}
7777
})
7878

79-
// print routes
80-
// hm := reflect.ValueOf(mux).Elem()
81-
// fl := hm.FieldByIndex([]int{1})
82-
// fmt.Printf("%+v\n", fl)
83-
84-
// path, _ := os.Getwd()
85-
// fmt.Printf("cwd: %s\n", path)
86-
8779
fmt.Printf("Listening at :%d", port)
8880
if err := http.ListenAndServe(fmt.Sprintf(":%d", port), LogginMiddleware(mux)); err != nil {
8981
if errors.Is(err, http.ErrServerClosed) {

routing_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,66 @@ func TestQuickUnitAttachRoutes(t *testing.T) {
268268
t.Errorf("Check failed: %#v", err)
269269
}
270270
}
271+
272+
func TestOAS_AddRoute(t *testing.T) {
273+
274+
var (
275+
respose200 = Response{Code: 200, Description: "Ok"}
276+
respose404 = Response{Code: 404, Description: "Not Found"}
277+
contentTypeUser = ContentType{Name: "application/json", Schema: "#/components/schemas/User"}
278+
requestBodyGetUser = RequestBody{
279+
Description: "Get a User",
280+
Content: ContentTypes{contentTypeUser},
281+
Required: true,
282+
}
283+
requestBodyCreateUser = RequestBody{
284+
Description: "Create a new User",
285+
Content: ContentTypes{contentTypeUser},
286+
Required: true,
287+
}
288+
pathGetUser = Path{
289+
Route: "/users",
290+
HTTPMethod: "GET",
291+
OperationID: "getUser",
292+
Summary: "Get a User",
293+
Responses: Responses{respose200},
294+
RequestBody: requestBodyGetUser,
295+
}
296+
pathCreateUser = Path{
297+
Route: "/users",
298+
HTTPMethod: "POST",
299+
OperationID: "createUser",
300+
Summary: "Create a new User",
301+
Responses: Responses{respose200, respose404},
302+
RequestBody: requestBodyCreateUser,
303+
}
304+
)
305+
306+
tests := []struct {
307+
name string
308+
oas *OAS
309+
path Path
310+
wantPaths Paths
311+
}{
312+
{
313+
name: "success-no-existing-paths",
314+
oas: &OAS{},
315+
path: pathGetUser,
316+
wantPaths: Paths{pathGetUser},
317+
},
318+
{
319+
name: "success-existing-paths",
320+
oas: &OAS{Paths: Paths{pathGetUser}},
321+
path: pathCreateUser,
322+
wantPaths: Paths{pathGetUser, pathCreateUser},
323+
},
324+
}
325+
for _, tt := range tests {
326+
t.Run(tt.name, func(t *testing.T) {
327+
tt.oas.AddRoute(tt.path)
328+
if !reflect.DeepEqual(tt.wantPaths, tt.oas.Paths) {
329+
t.Errorf("OAS.AddRoute() = [%v], want {%v}", tt.oas.Paths, tt.wantPaths)
330+
}
331+
})
332+
}
333+
}

0 commit comments

Comments
 (0)