| 1 |
// Copyright 2015 go-swagger maintainers |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 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("[email protected]"), |
| 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 |
// Numerical values |
| 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 |
// TODO: should be uint32 |
| 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 |
// TODO: should be uint32 |
| 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 |
// TODO: should be uint64 |
| 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 |
// TODO: should be uint64 |
| 196 |
expectedSwaggerFormat: "int64", |
| 197 |
}, |
| 198 |
{ |
| 199 |
value: float32(12), |
| 200 |
expectedJSONType: "number", |
| 201 |
// TODO: should be float |
| 202 |
expectedSwaggerFormat: "float32", |
| 203 |
}, |
| 204 |
{ |
| 205 |
value: float64(12), |
| 206 |
expectedJSONType: "number", |
| 207 |
// TODO: should be double |
| 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 |
// NOTE: Go array returns no JSON type |
| 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 |
/* |
| 247 |
Test case for : case reflect.Interface: |
| 248 |
// What to do here? |
| 249 |
panic("dunno what to do here") |
| 250 |
*/ |
| 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 |
} |