Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 1 | // Copyright 2011 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package sql |
| 6 | |
| 7 | import ( |
Brad Fitzpatrick | 7fc4c07 | 2012-01-19 16:04:26 -0800 | [diff] [blame] | 8 | "database/sql/driver" |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 9 | "fmt" |
| 10 | "reflect" |
| 11 | "testing" |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 12 | "time" |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 13 | ) |
| 14 | |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 15 | var someTime = time.Unix(123, 0) |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 16 | var answer int64 = 42 |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 17 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 18 | type conversionTest struct { |
| 19 | s, d interface{} // source and destination |
| 20 | |
| 21 | // following are used if they're non-zero |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 22 | wantint int64 |
| 23 | wantuint uint64 |
| 24 | wantstr string |
Julien Schmidt | 81d26e3 | 2013-03-22 12:19:21 -0700 | [diff] [blame] | 25 | wantbytes []byte |
| 26 | wantraw RawBytes |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 27 | wantf32 float32 |
| 28 | wantf64 float64 |
| 29 | wanttime time.Time |
| 30 | wantbool bool // used if d is of type *bool |
| 31 | wanterr string |
| 32 | wantiface interface{} |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 33 | wantptr *int64 // if non-nil, *d's pointed value must be equal to *wantptr |
| 34 | wantnil bool // if true, *d must be *int64(nil) |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // Target variables for scanning into. |
| 38 | var ( |
| 39 | scanstr string |
Julien Schmidt | 81d26e3 | 2013-03-22 12:19:21 -0700 | [diff] [blame] | 40 | scanbytes []byte |
| 41 | scanraw RawBytes |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 42 | scanint int |
| 43 | scanint8 int8 |
| 44 | scanint16 int16 |
| 45 | scanint32 int32 |
| 46 | scanuint8 uint8 |
| 47 | scanuint16 uint16 |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 48 | scanbool bool |
| 49 | scanf32 float32 |
| 50 | scanf64 float64 |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 51 | scantime time.Time |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 52 | scanptr *int64 |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 53 | scaniface interface{} |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 54 | ) |
| 55 | |
| 56 | var conversionTests = []conversionTest{ |
| 57 | // Exact conversions (destination pointer type matches source type) |
| 58 | {s: "foo", d: &scanstr, wantstr: "foo"}, |
| 59 | {s: 123, d: &scanint, wantint: 123}, |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 60 | {s: someTime, d: &scantime, wanttime: someTime}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 61 | |
| 62 | // To strings |
Julien Schmidt | 81d26e3 | 2013-03-22 12:19:21 -0700 | [diff] [blame] | 63 | {s: "string", d: &scanstr, wantstr: "string"}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 64 | {s: []byte("byteslice"), d: &scanstr, wantstr: "byteslice"}, |
| 65 | {s: 123, d: &scanstr, wantstr: "123"}, |
| 66 | {s: int8(123), d: &scanstr, wantstr: "123"}, |
| 67 | {s: int64(123), d: &scanstr, wantstr: "123"}, |
| 68 | {s: uint8(123), d: &scanstr, wantstr: "123"}, |
| 69 | {s: uint16(123), d: &scanstr, wantstr: "123"}, |
| 70 | {s: uint32(123), d: &scanstr, wantstr: "123"}, |
| 71 | {s: uint64(123), d: &scanstr, wantstr: "123"}, |
| 72 | {s: 1.5, d: &scanstr, wantstr: "1.5"}, |
| 73 | |
Julien Schmidt | 81d26e3 | 2013-03-22 12:19:21 -0700 | [diff] [blame] | 74 | // To []byte |
| 75 | {s: nil, d: &scanbytes, wantbytes: nil}, |
| 76 | {s: "string", d: &scanbytes, wantbytes: []byte("string")}, |
| 77 | {s: []byte("byteslice"), d: &scanbytes, wantbytes: []byte("byteslice")}, |
| 78 | {s: 123, d: &scanbytes, wantbytes: []byte("123")}, |
| 79 | {s: int8(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 80 | {s: int64(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 81 | {s: uint8(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 82 | {s: uint16(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 83 | {s: uint32(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 84 | {s: uint64(123), d: &scanbytes, wantbytes: []byte("123")}, |
| 85 | {s: 1.5, d: &scanbytes, wantbytes: []byte("1.5")}, |
| 86 | |
| 87 | // To RawBytes |
| 88 | {s: nil, d: &scanraw, wantraw: nil}, |
| 89 | {s: []byte("byteslice"), d: &scanraw, wantraw: RawBytes("byteslice")}, |
| 90 | {s: 123, d: &scanraw, wantraw: RawBytes("123")}, |
| 91 | {s: int8(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 92 | {s: int64(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 93 | {s: uint8(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 94 | {s: uint16(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 95 | {s: uint32(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 96 | {s: uint64(123), d: &scanraw, wantraw: RawBytes("123")}, |
| 97 | {s: 1.5, d: &scanraw, wantraw: RawBytes("1.5")}, |
| 98 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 99 | // Strings to integers |
| 100 | {s: "255", d: &scanuint8, wantuint: 255}, |
Rob Pike | 02f6719 | 2011-12-13 10:42:05 -0800 | [diff] [blame] | 101 | {s: "256", d: &scanuint8, wanterr: `converting string "256" to a uint8: strconv.ParseUint: parsing "256": value out of range`}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 102 | {s: "256", d: &scanuint16, wantuint: 256}, |
| 103 | {s: "-1", d: &scanint, wantint: -1}, |
Rob Pike | 02f6719 | 2011-12-13 10:42:05 -0800 | [diff] [blame] | 104 | {s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: strconv.ParseInt: parsing "foo": invalid syntax`}, |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 105 | |
| 106 | // True bools |
| 107 | {s: true, d: &scanbool, wantbool: true}, |
| 108 | {s: "True", d: &scanbool, wantbool: true}, |
| 109 | {s: "TRUE", d: &scanbool, wantbool: true}, |
| 110 | {s: "1", d: &scanbool, wantbool: true}, |
| 111 | {s: 1, d: &scanbool, wantbool: true}, |
| 112 | {s: int64(1), d: &scanbool, wantbool: true}, |
| 113 | {s: uint16(1), d: &scanbool, wantbool: true}, |
| 114 | |
| 115 | // False bools |
| 116 | {s: false, d: &scanbool, wantbool: false}, |
| 117 | {s: "false", d: &scanbool, wantbool: false}, |
| 118 | {s: "FALSE", d: &scanbool, wantbool: false}, |
| 119 | {s: "0", d: &scanbool, wantbool: false}, |
| 120 | {s: 0, d: &scanbool, wantbool: false}, |
| 121 | {s: int64(0), d: &scanbool, wantbool: false}, |
| 122 | {s: uint16(0), d: &scanbool, wantbool: false}, |
| 123 | |
| 124 | // Not bools |
| 125 | {s: "yup", d: &scanbool, wanterr: `sql/driver: couldn't convert "yup" into type bool`}, |
| 126 | {s: 2, d: &scanbool, wanterr: `sql/driver: couldn't convert 2 into type bool`}, |
| 127 | |
| 128 | // Floats |
| 129 | {s: float64(1.5), d: &scanf64, wantf64: float64(1.5)}, |
| 130 | {s: int64(1), d: &scanf64, wantf64: float64(1)}, |
| 131 | {s: float64(1.5), d: &scanf32, wantf32: float32(1.5)}, |
| 132 | {s: "1.5", d: &scanf32, wantf32: float32(1.5)}, |
| 133 | {s: "1.5", d: &scanf64, wantf64: float64(1.5)}, |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 134 | |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 135 | // Pointers |
| 136 | {s: interface{}(nil), d: &scanptr, wantnil: true}, |
| 137 | {s: int64(42), d: &scanptr, wantptr: &answer}, |
| 138 | |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 139 | // To interface{} |
| 140 | {s: float64(1.5), d: &scaniface, wantiface: float64(1.5)}, |
| 141 | {s: int64(1), d: &scaniface, wantiface: int64(1)}, |
| 142 | {s: "str", d: &scaniface, wantiface: "str"}, |
| 143 | {s: []byte("byteslice"), d: &scaniface, wantiface: []byte("byteslice")}, |
| 144 | {s: true, d: &scaniface, wantiface: true}, |
| 145 | {s: nil, d: &scaniface}, |
Julien Schmidt | 5e74f50 | 2013-03-25 17:43:30 -0700 | [diff] [blame] | 146 | {s: []byte(nil), d: &scaniface, wantiface: []byte(nil)}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 149 | func intPtrValue(intptr interface{}) interface{} { |
| 150 | return reflect.Indirect(reflect.Indirect(reflect.ValueOf(intptr))).Int() |
| 151 | } |
| 152 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 153 | func intValue(intptr interface{}) int64 { |
| 154 | return reflect.Indirect(reflect.ValueOf(intptr)).Int() |
| 155 | } |
| 156 | |
| 157 | func uintValue(intptr interface{}) uint64 { |
| 158 | return reflect.Indirect(reflect.ValueOf(intptr)).Uint() |
| 159 | } |
| 160 | |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 161 | func float64Value(ptr interface{}) float64 { |
| 162 | return *(ptr.(*float64)) |
| 163 | } |
| 164 | |
| 165 | func float32Value(ptr interface{}) float32 { |
| 166 | return *(ptr.(*float32)) |
| 167 | } |
| 168 | |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 169 | func timeValue(ptr interface{}) time.Time { |
| 170 | return *(ptr.(*time.Time)) |
| 171 | } |
| 172 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 173 | func TestConversions(t *testing.T) { |
| 174 | for n, ct := range conversionTests { |
| 175 | err := convertAssign(ct.d, ct.s) |
| 176 | errstr := "" |
| 177 | if err != nil { |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 178 | errstr = err.Error() |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 179 | } |
| 180 | errf := func(format string, args ...interface{}) { |
| 181 | base := fmt.Sprintf("convertAssign #%d: for %v (%T) -> %T, ", n, ct.s, ct.s, ct.d) |
| 182 | t.Errorf(base+format, args...) |
| 183 | } |
| 184 | if errstr != ct.wanterr { |
| 185 | errf("got error %q, want error %q", errstr, ct.wanterr) |
| 186 | } |
| 187 | if ct.wantstr != "" && ct.wantstr != scanstr { |
| 188 | errf("want string %q, got %q", ct.wantstr, scanstr) |
| 189 | } |
| 190 | if ct.wantint != 0 && ct.wantint != intValue(ct.d) { |
| 191 | errf("want int %d, got %d", ct.wantint, intValue(ct.d)) |
| 192 | } |
| 193 | if ct.wantuint != 0 && ct.wantuint != uintValue(ct.d) { |
| 194 | errf("want uint %d, got %d", ct.wantuint, uintValue(ct.d)) |
| 195 | } |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 196 | if ct.wantf32 != 0 && ct.wantf32 != float32Value(ct.d) { |
| 197 | errf("want float32 %v, got %v", ct.wantf32, float32Value(ct.d)) |
| 198 | } |
| 199 | if ct.wantf64 != 0 && ct.wantf64 != float64Value(ct.d) { |
| 200 | errf("want float32 %v, got %v", ct.wantf64, float64Value(ct.d)) |
| 201 | } |
| 202 | if bp, boolTest := ct.d.(*bool); boolTest && *bp != ct.wantbool && ct.wanterr == "" { |
| 203 | errf("want bool %v, got %v", ct.wantbool, *bp) |
| 204 | } |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame] | 205 | if !ct.wanttime.IsZero() && !ct.wanttime.Equal(timeValue(ct.d)) { |
| 206 | errf("want time %v, got %v", ct.wanttime, timeValue(ct.d)) |
| 207 | } |
Andrew Pritchard | cc39bb9 | 2012-02-08 17:14:15 +1100 | [diff] [blame] | 208 | if ct.wantnil && *ct.d.(**int64) != nil { |
| 209 | errf("want nil, got %v", intPtrValue(ct.d)) |
| 210 | } |
| 211 | if ct.wantptr != nil { |
| 212 | if *ct.d.(**int64) == nil { |
| 213 | errf("want pointer to %v, got nil", *ct.wantptr) |
| 214 | } else if *ct.wantptr != intPtrValue(ct.d) { |
| 215 | errf("want pointer to %v, got %v", *ct.wantptr, intPtrValue(ct.d)) |
| 216 | } |
| 217 | } |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 218 | if ifptr, ok := ct.d.(*interface{}); ok { |
| 219 | if !reflect.DeepEqual(ct.wantiface, scaniface) { |
| 220 | errf("want interface %#v, got %#v", ct.wantiface, scaniface) |
| 221 | continue |
| 222 | } |
| 223 | if srcBytes, ok := ct.s.([]byte); ok { |
| 224 | dstBytes := (*ifptr).([]byte) |
Julien Schmidt | 5e74f50 | 2013-03-25 17:43:30 -0700 | [diff] [blame] | 225 | if len(srcBytes) > 0 && &dstBytes[0] == &srcBytes[0] { |
Brad Fitzpatrick | 9c060b8 | 2012-02-06 10:06:22 -0800 | [diff] [blame] | 226 | errf("copy into interface{} didn't copy []byte data") |
| 227 | } |
| 228 | } |
| 229 | } |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Brad Fitzpatrick | bc0139b | 2012-01-19 09:27:45 -0800 | [diff] [blame] | 233 | func TestNullString(t *testing.T) { |
| 234 | var ns NullString |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 235 | convertAssign(&ns, []byte("foo")) |
| 236 | if !ns.Valid { |
| 237 | t.Errorf("expecting not null") |
| 238 | } |
| 239 | if ns.String != "foo" { |
| 240 | t.Errorf("expecting foo; got %q", ns.String) |
| 241 | } |
| 242 | convertAssign(&ns, nil) |
| 243 | if ns.Valid { |
| 244 | t.Errorf("expecting null on nil") |
| 245 | } |
| 246 | if ns.String != "" { |
| 247 | t.Errorf("expecting blank on nil; got %q", ns.String) |
| 248 | } |
| 249 | } |
Brad Fitzpatrick | bc0139b | 2012-01-19 09:27:45 -0800 | [diff] [blame] | 250 | |
| 251 | type valueConverterTest struct { |
| 252 | c driver.ValueConverter |
| 253 | in, out interface{} |
| 254 | err string |
| 255 | } |
| 256 | |
| 257 | var valueConverterTests = []valueConverterTest{ |
| 258 | {driver.DefaultParameterConverter, NullString{"hi", true}, "hi", ""}, |
| 259 | {driver.DefaultParameterConverter, NullString{"", false}, nil, ""}, |
| 260 | } |
| 261 | |
| 262 | func TestValueConverters(t *testing.T) { |
| 263 | for i, tt := range valueConverterTests { |
| 264 | out, err := tt.c.ConvertValue(tt.in) |
| 265 | goterr := "" |
| 266 | if err != nil { |
| 267 | goterr = err.Error() |
| 268 | } |
| 269 | if goterr != tt.err { |
Brad Fitzpatrick | e5babef | 2013-10-16 16:30:39 -0700 | [diff] [blame] | 270 | t.Errorf("test %d: %T(%T(%v)) error = %q; want error = %q", |
Brad Fitzpatrick | bc0139b | 2012-01-19 09:27:45 -0800 | [diff] [blame] | 271 | i, tt.c, tt.in, tt.in, goterr, tt.err) |
| 272 | } |
| 273 | if tt.err != "" { |
| 274 | continue |
| 275 | } |
| 276 | if !reflect.DeepEqual(out, tt.out) { |
Brad Fitzpatrick | e5babef | 2013-10-16 16:30:39 -0700 | [diff] [blame] | 277 | t.Errorf("test %d: %T(%T(%v)) = %v (%T); want %v (%T)", |
Brad Fitzpatrick | bc0139b | 2012-01-19 09:27:45 -0800 | [diff] [blame] | 278 | i, tt.c, tt.in, tt.in, out, out, tt.out, tt.out) |
| 279 | } |
| 280 | } |
| 281 | } |
Brad Fitzpatrick | 258ed3f | 2014-01-10 12:19:36 -0800 | [diff] [blame^] | 282 | |
| 283 | // Tests that assigning to RawBytes doesn't allocate (and also works). |
| 284 | func TestRawBytesAllocs(t *testing.T) { |
| 285 | buf := make(RawBytes, 10) |
| 286 | test := func(name string, in interface{}, want string) { |
| 287 | if err := convertAssign(&buf, in); err != nil { |
| 288 | t.Fatalf("%s: convertAssign = %v", name, err) |
| 289 | } |
| 290 | match := len(buf) == len(want) |
| 291 | if match { |
| 292 | for i, b := range buf { |
| 293 | if want[i] != b { |
| 294 | match = false |
| 295 | break |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | if !match { |
| 300 | t.Fatalf("%s: got %q (len %d); want %q (len %d)", name, buf, len(buf), want, len(want)) |
| 301 | } |
| 302 | } |
| 303 | n := testing.AllocsPerRun(100, func() { |
| 304 | test("uint64", uint64(12345678), "12345678") |
| 305 | test("uint32", uint32(1234), "1234") |
| 306 | test("uint16", uint16(12), "12") |
| 307 | test("uint8", uint8(1), "1") |
| 308 | test("uint", uint(123), "123") |
| 309 | test("int", int(123), "123") |
| 310 | test("int8", int8(1), "1") |
| 311 | test("int16", int16(12), "12") |
| 312 | test("int32", int32(1234), "1234") |
| 313 | test("int64", int64(12345678), "12345678") |
| 314 | test("float32", float32(1.5), "1.5") |
| 315 | test("float64", float64(64), "64") |
| 316 | test("bool", false, "false") |
| 317 | }) |
| 318 | if n > 0.5 { |
| 319 | t.Fatalf("allocs = %v; want 0", n) |
| 320 | } |
| 321 | |
| 322 | // This one involves a convT2E allocation, string -> interface{} |
| 323 | n = testing.AllocsPerRun(100, func() { |
| 324 | test("string", "foo", "foo") |
| 325 | }) |
| 326 | if n > 1.5 { |
| 327 | t.Fatalf("allocs = %v; want max 1", n) |
| 328 | } |
| 329 | } |