This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
169 lines (162 loc) · 3.44 KB
/
main_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
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
func TestSwagger_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input Swagger
expected Violations
}{
{
"Missing Operation ID",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
Tags: []string{"tag"},
},
},
},
},
map[Resource][]string{
"/items": {"Missing operation id."},
},
},
{
"Resource must begin with method",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
OperationID: "itemsOperationID",
Tags: []string{"tag"},
},
},
},
},
map[Resource][]string{
"/items": {"'itemsOperationID': Resource must begin with 'get'."},
},
},
{
"Resource must define at least one tag.",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
OperationID: "getItems",
},
},
},
},
map[Resource][]string{
"/items": {"Resource must define at least one tag."},
},
},
{
"Body request model must be prefixed with method+Request",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"post": {
OperationID: "postItems",
Tags: []string{"tag"},
Parameters: []Parameter{
{
In: "body",
Schema: Schema{
Ref: `#/definitions/postItems`,
},
},
},
},
},
},
},
map[Resource][]string{
"/items": {"'postItems': Body request model must be prefixed with method+Request: '#/definitions/postItems'."},
},
},
{
"Query arguments must be lowercase",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
OperationID: "getItems",
Tags: []string{"tag"},
Parameters: []Parameter{
{
In: "query",
Name: "deletedAt",
},
},
},
},
},
},
map[Resource][]string{
"/items": {"'getItems': Query arguments must be lowercase: 'deletedAt'"},
},
},
{
"Instead of using Array prefer defining a new model.",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
OperationID: "getItems",
Tags: []string{"tag"},
Responses: map[Code]Response{
"200": {
Schema: Schema{
Type: "array",
},
},
},
},
},
},
},
map[Resource][]string{
"/items": {"'getItems': Instead of using Array as a response, prefer defining a new model."},
},
},
{
"Code 200, response model must be prefixed with method+Response.",
Swagger{
Paths: map[Resource]Paths{
"/items": map[Method]Path{
"get": {
OperationID: "getItems",
Tags: []string{"tag"},
Responses: map[Code]Response{
"200": {
Schema: Schema{
Ref: `#/definitions/getItems`,
},
},
},
},
},
},
},
map[Resource][]string{
"/items": {"'getItems': Code 200, response model must be prefixed with method+Response: '#/definitions/getItems'."},
},
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
actual := test.input.Validate()
if !cmp.Equal(test.expected, actual) {
t.Errorf("expected values do not match\n%s", cmp.Diff(test.expected, actual))
}
})
}
}