1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package validate
16
17 import (
18 "testing"
19 "time"
20
21 "github.com/stretchr/testify/assert"
22 "k8s.io/kube-openapi/pkg/validation/strfmt"
23 )
24
25 type expectedJSONType struct {
26 value interface{}
27 expectedJSONType string
28 expectedSwaggerFormat string
29 }
30
31 func TestType_schemaInfoForType(t *testing.T) {
32 testTypes := []expectedJSONType{
33 {
34 value: []byte("abc"),
35 expectedJSONType: stringType,
36 expectedSwaggerFormat: "byte",
37 },
38 {
39 value: strfmt.Date(time.Date(2014, 10, 10, 0, 0, 0, 0, time.UTC)),
40 expectedJSONType: stringType,
41 expectedSwaggerFormat: "date",
42 },
43 {
44 value: strfmt.NewDateTime(),
45 expectedJSONType: stringType,
46 expectedSwaggerFormat: "date-time",
47 },
48 {
49 value: strfmt.URI("http://thisisleadingusnowhere.com"),
50 expectedJSONType: stringType,
51 expectedSwaggerFormat: "uri",
52 },
53 {
54 value: strfmt.Email("fred@esasymoney.com"),
55 expectedJSONType: stringType,
56 expectedSwaggerFormat: "email",
57 },
58 {
59 value: strfmt.Hostname("www.github.com"),
60 expectedJSONType: stringType,
61 expectedSwaggerFormat: "hostname",
62 },
63 {
64 value: strfmt.Password("secret"),
65 expectedJSONType: stringType,
66 expectedSwaggerFormat: "password",
67 },
68 {
69 value: strfmt.IPv4("192.168.224.1"),
70 expectedJSONType: stringType,
71 expectedSwaggerFormat: "ipv4",
72 },
73 {
74 value: strfmt.IPv6("::1"),
75 expectedJSONType: stringType,
76 expectedSwaggerFormat: "ipv6",
77 },
78 {
79 value: strfmt.MAC("01:02:03:04:05:06"),
80 expectedJSONType: stringType,
81 expectedSwaggerFormat: "mac",
82 },
83 {
84 value: strfmt.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e"),
85 expectedJSONType: stringType,
86 expectedSwaggerFormat: "uuid",
87 },
88 {
89 value: strfmt.UUID3("bcd02e22-68f0-3046-a512-327cca9def8f"),
90 expectedJSONType: stringType,
91 expectedSwaggerFormat: "uuid3",
92 },
93 {
94 value: strfmt.UUID4("025b0d74-00a2-4048-bf57-227c5111bb34"),
95 expectedJSONType: stringType,
96 expectedSwaggerFormat: "uuid4",
97 },
98 {
99 value: strfmt.UUID5("886313e1-3b8a-5372-9b90-0c9aee199e5d"),
100 expectedJSONType: stringType,
101 expectedSwaggerFormat: "uuid5",
102 },
103 {
104 value: strfmt.ISBN("0321751043"),
105 expectedJSONType: stringType,
106 expectedSwaggerFormat: "isbn",
107 },
108 {
109 value: strfmt.ISBN10("0321751043"),
110 expectedJSONType: stringType,
111 expectedSwaggerFormat: "isbn10",
112 },
113 {
114 value: strfmt.ISBN13("978-0321751041"),
115 expectedJSONType: stringType,
116 expectedSwaggerFormat: "isbn13",
117 },
118 {
119 value: strfmt.CreditCard("4111-1111-1111-1111"),
120 expectedJSONType: stringType,
121 expectedSwaggerFormat: "creditcard",
122 },
123 {
124 value: strfmt.SSN("111-11-1111"),
125 expectedJSONType: stringType,
126 expectedSwaggerFormat: "ssn",
127 },
128 {
129 value: strfmt.HexColor("#FFFFFF"),
130 expectedJSONType: stringType,
131 expectedSwaggerFormat: "hexcolor",
132 },
133 {
134 value: strfmt.RGBColor("rgb(255,255,255)"),
135 expectedJSONType: stringType,
136 expectedSwaggerFormat: "rgbcolor",
137 },
138
139 {
140 value: true,
141 expectedJSONType: "boolean",
142 expectedSwaggerFormat: "",
143 },
144 {
145 value: int8(12),
146 expectedJSONType: "integer",
147 expectedSwaggerFormat: "int32",
148 },
149 {
150 value: uint8(12),
151 expectedJSONType: "integer",
152 expectedSwaggerFormat: "int32",
153 },
154 {
155 value: int16(12),
156 expectedJSONType: "integer",
157 expectedSwaggerFormat: "int32",
158 },
159 {
160 value: uint16(12),
161 expectedJSONType: "integer",
162
163 expectedSwaggerFormat: "int32",
164 },
165 {
166 value: int32(12),
167 expectedJSONType: "integer",
168 expectedSwaggerFormat: "int32",
169 },
170 {
171 value: uint32(12),
172 expectedJSONType: "integer",
173
174 expectedSwaggerFormat: "int32",
175 },
176 {
177 value: int(12),
178 expectedJSONType: "integer",
179 expectedSwaggerFormat: "int64",
180 },
181 {
182 value: uint(12),
183 expectedJSONType: "integer",
184
185 expectedSwaggerFormat: "int64",
186 },
187 {
188 value: int64(12),
189 expectedJSONType: "integer",
190 expectedSwaggerFormat: "int64",
191 },
192 {
193 value: uint64(12),
194 expectedJSONType: "integer",
195
196 expectedSwaggerFormat: "int64",
197 },
198 {
199 value: float32(12),
200 expectedJSONType: "number",
201
202 expectedSwaggerFormat: "float32",
203 },
204 {
205 value: float64(12),
206 expectedJSONType: "number",
207
208 expectedSwaggerFormat: "float64",
209 },
210 {
211 value: []string{},
212 expectedJSONType: "array",
213 expectedSwaggerFormat: "",
214 },
215 {
216 value: expectedJSONType{},
217 expectedJSONType: "object",
218 expectedSwaggerFormat: "",
219 },
220 {
221 value: map[string]bool{"key": false},
222 expectedJSONType: "object",
223 expectedSwaggerFormat: "",
224 },
225 {
226 value: "simply a string",
227 expectedJSONType: stringType,
228 expectedSwaggerFormat: "",
229 },
230 {
231
232 value: [4]int{1, 2, 4, 4},
233 expectedJSONType: "",
234 expectedSwaggerFormat: "",
235 },
236 {
237 value: strfmt.Base64("ZWxpemFiZXRocG9zZXk="),
238 expectedJSONType: stringType,
239 expectedSwaggerFormat: "byte",
240 },
241 {
242 value: strfmt.Duration(0),
243 expectedJSONType: stringType,
244 expectedSwaggerFormat: "duration",
245 },
246
251 }
252
253 v := &typeValidator{}
254 for _, x := range testTypes {
255 jsonType, swaggerFormat := v.schemaInfoForType(x.value)
256 assert.Equal(t, x.expectedJSONType, jsonType)
257 assert.Equal(t, x.expectedSwaggerFormat, swaggerFormat)
258
259 jsonType, swaggerFormat = v.schemaInfoForType(&x.value)
260 assert.Equal(t, x.expectedJSONType, jsonType)
261 assert.Equal(t, x.expectedSwaggerFormat, swaggerFormat)
262 }
263 }
264
View as plain text