-
Notifications
You must be signed in to change notification settings - Fork 15
/
generate_test.go
123 lines (114 loc) · 5.13 KB
/
generate_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
package swagno
import (
"encoding/json"
"os"
"testing"
"github.com/go-swagno/swagno/components/definition"
"github.com/go-swagno/swagno/components/endpoint"
"github.com/go-swagno/swagno/components/http/response"
"github.com/go-swagno/swagno/components/mime"
"github.com/go-swagno/swagno/components/parameter"
"github.com/go-swagno/swagno/example/models"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
var desc = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed id malesuada lorem, et fermentum sapien. Vivamus non pharetra risus, in efficitur leo. Suspendisse sed metus sit amet mi laoreet imperdiet. Donec aliquam eros eu blandit feugiat. Quisque scelerisque justo ac vehicula bibendum. Fusce suscipit arcu nisl, eu maximus odio consequat quis. Curabitur fermentum eleifend tellus, lobortis hendrerit velit varius vitae."
func TestSwaggerGeneration(t *testing.T) {
testCases := []struct {
name string
endpoints []*endpoint.EndPoint
file string
}{
{
name: "Basic Functionality Test",
// TODO i don't want to ruin the current design by adding in validation checks for user input, but a nice compromise
// would be to have an optional function that iterates through the endpoints and validates all the endpoints for syntactical
// errors. This way the Swagno isn't more restrictive then the actual OpenAPI parser is when rendering
// but the client still has an option to check for errors if they wish to do so.
endpoints: []*endpoint.EndPoint{
endpoint.New(
endpoint.GET,
"/product",
endpoint.WithTags("product"),
endpoint.WithErrors([]response.Response{response.New(models.UnsuccessfulResponse{}, "400", "Bad Request")}),
endpoint.WithSuccessfulReturns([]response.Response{response.New(models.EmptySuccessfulResponse{}, "200", "OK")}),
endpoint.WithDescription(desc),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
endpoint.WithConsume([]mime.MIME{mime.JSON}),
endpoint.WithSummary("this is a test summary"),
),
endpoint.New(
endpoint.GET,
"/product/{id}",
endpoint.WithTags("product"),
endpoint.WithParams(parameter.IntParam("id", parameter.Path, parameter.WithRequired())),
endpoint.WithSuccessfulReturns([]response.Response{response.New(models.SuccessfulResponse{}, "201", "Request Accepted")}),
endpoint.WithErrors([]response.Response{response.New(models.UnsuccessfulResponse{}, "400", "Bad Request")}),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
),
endpoint.New(
endpoint.GET,
"/product/{id}/detail",
endpoint.WithTags("product"),
endpoint.WithParams(parameter.IntParam("id", parameter.Path, parameter.WithRequired())),
endpoint.WithSuccessfulReturns([]response.Response{response.New(models.SuccessfulResponse{}, "201", "Request Accepted")}),
endpoint.WithErrors([]response.Response{response.New(models.UnsuccessfulResponse{}, "400", "Bad Request")}),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
),
endpoint.New(
endpoint.POST,
"/product",
endpoint.WithTags("product"),
endpoint.WithBody(models.ProductPost{}),
endpoint.WithSuccessfulReturns([]response.Response{response.New(models.SuccessfulResponse{}, "201", "Request Accepted")}),
endpoint.WithErrors([]response.Response{response.New(models.UnsuccessfulResponse{}, "400", "Bad Request")}),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
),
},
file: "testdata/expected_output/bft.json",
},
{
name: "Deeply Nested Model Test",
endpoints: []*endpoint.EndPoint{
endpoint.New(
endpoint.GET,
"/deeplynested",
endpoint.WithSuccessfulReturns([]response.Response{response.New(models.ComplexSuccessfulResponse{}, "200", "OK")}),
endpoint.WithDescription(desc),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
endpoint.WithConsume([]mime.MIME{mime.JSON}),
endpoint.WithSummary("this is a test summary"),
),
endpoint.New(
endpoint.GET,
"/arraydeeplynested",
endpoint.WithSuccessfulReturns([]response.Response{response.New([]models.ComplexSuccessfulResponse{}, "200", "OK")}),
endpoint.WithDescription(desc),
endpoint.WithProduce([]mime.MIME{mime.JSON, mime.XML}),
endpoint.WithConsume([]mime.MIME{mime.JSON}),
endpoint.WithSummary("this is a test summary"),
),
},
file: "testdata/expected_output/dnmt.json",
},
}
// Iterate through test cases
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
expectedJsonData, err := os.ReadFile(tc.file)
if err != nil {
t.Fatalf("Error reading file: %v", err)
}
want := New(Config{Title: "Testing API", Version: "v1.0.0"})
if err := json.Unmarshal(expectedJsonData, want); err != nil {
t.Fatal(err)
}
got := New(Config{Title: "Testing API", Version: "v1.0.0"})
got.AddEndpoints(tc.endpoints)
got.generateSwaggerJson()
if diff := cmp.Diff(want, got, cmpopts.IgnoreFields(Swagger{}, "endpoints"), cmpopts.IgnoreFields(definition.DefinitionProperties{}, "Example", "IsRequired")); diff != "" {
t.Errorf("JsonSwagger() mismatch (-expected +got):\n%s", diff)
}
})
}
}