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 ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "testing" |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 11 | "time" |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 14 | var someTime = time.Unix(123, 0) |
| 15 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 16 | type conversionTest struct { |
| 17 | s, d interface{} // source and destination |
| 18 | |
| 19 | // following are used if they're non-zero |
| 20 | wantint int64 |
| 21 | wantuint uint64 |
| 22 | wantstr string |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 23 | wantf32 float32 |
| 24 | wantf64 float64 |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 25 | wanttime time.Time |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 26 | wantbool bool // used if d is of type *bool |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 27 | wanterr string |
| 28 | } |
| 29 | |
| 30 | // Target variables for scanning into. |
| 31 | var ( |
| 32 | scanstr string |
| 33 | scanint int |
| 34 | scanint8 int8 |
| 35 | scanint16 int16 |
| 36 | scanint32 int32 |
| 37 | scanuint8 uint8 |
| 38 | scanuint16 uint16 |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 39 | scanbool bool |
| 40 | scanf32 float32 |
| 41 | scanf64 float64 |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 42 | scantime time.Time |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 43 | ) |
| 44 | |
| 45 | var conversionTests = []conversionTest{ |
| 46 | // Exact conversions (destination pointer type matches source type) |
| 47 | {s: "foo", d: &scanstr, wantstr: "foo"}, |
| 48 | {s: 123, d: &scanint, wantint: 123}, |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 49 | {s: someTime, d: &scantime, wanttime: someTime}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 50 | |
| 51 | // To strings |
| 52 | {s: []byte("byteslice"), d: &scanstr, wantstr: "byteslice"}, |
| 53 | {s: 123, d: &scanstr, wantstr: "123"}, |
| 54 | {s: int8(123), d: &scanstr, wantstr: "123"}, |
| 55 | {s: int64(123), d: &scanstr, wantstr: "123"}, |
| 56 | {s: uint8(123), d: &scanstr, wantstr: "123"}, |
| 57 | {s: uint16(123), d: &scanstr, wantstr: "123"}, |
| 58 | {s: uint32(123), d: &scanstr, wantstr: "123"}, |
| 59 | {s: uint64(123), d: &scanstr, wantstr: "123"}, |
| 60 | {s: 1.5, d: &scanstr, wantstr: "1.5"}, |
| 61 | |
| 62 | // Strings to integers |
| 63 | {s: "255", d: &scanuint8, wantuint: 255}, |
Rob Pike | 02f6719 | 2011-12-13 10:42:05 -0800 | [diff] [blame] | 64 | {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] | 65 | {s: "256", d: &scanuint16, wantuint: 256}, |
| 66 | {s: "-1", d: &scanint, wantint: -1}, |
Rob Pike | 02f6719 | 2011-12-13 10:42:05 -0800 | [diff] [blame] | 67 | {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] | 68 | |
| 69 | // True bools |
| 70 | {s: true, d: &scanbool, wantbool: true}, |
| 71 | {s: "True", d: &scanbool, wantbool: true}, |
| 72 | {s: "TRUE", d: &scanbool, wantbool: true}, |
| 73 | {s: "1", d: &scanbool, wantbool: true}, |
| 74 | {s: 1, d: &scanbool, wantbool: true}, |
| 75 | {s: int64(1), d: &scanbool, wantbool: true}, |
| 76 | {s: uint16(1), d: &scanbool, wantbool: true}, |
| 77 | |
| 78 | // False bools |
| 79 | {s: false, d: &scanbool, wantbool: false}, |
| 80 | {s: "false", d: &scanbool, wantbool: false}, |
| 81 | {s: "FALSE", d: &scanbool, wantbool: false}, |
| 82 | {s: "0", d: &scanbool, wantbool: false}, |
| 83 | {s: 0, d: &scanbool, wantbool: false}, |
| 84 | {s: int64(0), d: &scanbool, wantbool: false}, |
| 85 | {s: uint16(0), d: &scanbool, wantbool: false}, |
| 86 | |
| 87 | // Not bools |
| 88 | {s: "yup", d: &scanbool, wanterr: `sql/driver: couldn't convert "yup" into type bool`}, |
| 89 | {s: 2, d: &scanbool, wanterr: `sql/driver: couldn't convert 2 into type bool`}, |
| 90 | |
| 91 | // Floats |
| 92 | {s: float64(1.5), d: &scanf64, wantf64: float64(1.5)}, |
| 93 | {s: int64(1), d: &scanf64, wantf64: float64(1)}, |
| 94 | {s: float64(1.5), d: &scanf32, wantf32: float32(1.5)}, |
| 95 | {s: "1.5", d: &scanf32, wantf32: float32(1.5)}, |
| 96 | {s: "1.5", d: &scanf64, wantf64: float64(1.5)}, |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func intValue(intptr interface{}) int64 { |
| 100 | return reflect.Indirect(reflect.ValueOf(intptr)).Int() |
| 101 | } |
| 102 | |
| 103 | func uintValue(intptr interface{}) uint64 { |
| 104 | return reflect.Indirect(reflect.ValueOf(intptr)).Uint() |
| 105 | } |
| 106 | |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 107 | func float64Value(ptr interface{}) float64 { |
| 108 | return *(ptr.(*float64)) |
| 109 | } |
| 110 | |
| 111 | func float32Value(ptr interface{}) float32 { |
| 112 | return *(ptr.(*float32)) |
| 113 | } |
| 114 | |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 115 | func timeValue(ptr interface{}) time.Time { |
| 116 | return *(ptr.(*time.Time)) |
| 117 | } |
| 118 | |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 119 | func TestConversions(t *testing.T) { |
| 120 | for n, ct := range conversionTests { |
| 121 | err := convertAssign(ct.d, ct.s) |
| 122 | errstr := "" |
| 123 | if err != nil { |
Russ Cox | c2049d2 | 2011-11-01 22:04:37 -0400 | [diff] [blame] | 124 | errstr = err.Error() |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 125 | } |
| 126 | errf := func(format string, args ...interface{}) { |
| 127 | base := fmt.Sprintf("convertAssign #%d: for %v (%T) -> %T, ", n, ct.s, ct.s, ct.d) |
| 128 | t.Errorf(base+format, args...) |
| 129 | } |
| 130 | if errstr != ct.wanterr { |
| 131 | errf("got error %q, want error %q", errstr, ct.wanterr) |
| 132 | } |
| 133 | if ct.wantstr != "" && ct.wantstr != scanstr { |
| 134 | errf("want string %q, got %q", ct.wantstr, scanstr) |
| 135 | } |
| 136 | if ct.wantint != 0 && ct.wantint != intValue(ct.d) { |
| 137 | errf("want int %d, got %d", ct.wantint, intValue(ct.d)) |
| 138 | } |
| 139 | if ct.wantuint != 0 && ct.wantuint != uintValue(ct.d) { |
| 140 | errf("want uint %d, got %d", ct.wantuint, uintValue(ct.d)) |
| 141 | } |
Brad Fitzpatrick | 8089e57 | 2011-11-02 11:46:04 -0700 | [diff] [blame] | 142 | if ct.wantf32 != 0 && ct.wantf32 != float32Value(ct.d) { |
| 143 | errf("want float32 %v, got %v", ct.wantf32, float32Value(ct.d)) |
| 144 | } |
| 145 | if ct.wantf64 != 0 && ct.wantf64 != float64Value(ct.d) { |
| 146 | errf("want float32 %v, got %v", ct.wantf64, float64Value(ct.d)) |
| 147 | } |
| 148 | if bp, boolTest := ct.d.(*bool); boolTest && *bp != ct.wantbool && ct.wanterr == "" { |
| 149 | errf("want bool %v, got %v", ct.wantbool, *bp) |
| 150 | } |
Brad Fitzpatrick | bf734d6 | 2012-01-13 15:45:05 -0800 | [diff] [blame^] | 151 | if !ct.wanttime.IsZero() && !ct.wanttime.Equal(timeValue(ct.d)) { |
| 152 | errf("want time %v, got %v", ct.wanttime, timeValue(ct.d)) |
| 153 | } |
Brad Fitzpatrick | 357f2cb | 2011-09-29 16:12:21 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | func TestNullableString(t *testing.T) { |
| 158 | var ns NullableString |
| 159 | convertAssign(&ns, []byte("foo")) |
| 160 | if !ns.Valid { |
| 161 | t.Errorf("expecting not null") |
| 162 | } |
| 163 | if ns.String != "foo" { |
| 164 | t.Errorf("expecting foo; got %q", ns.String) |
| 165 | } |
| 166 | convertAssign(&ns, nil) |
| 167 | if ns.Valid { |
| 168 | t.Errorf("expecting null on nil") |
| 169 | } |
| 170 | if ns.String != "" { |
| 171 | t.Errorf("expecting blank on nil; got %q", ns.String) |
| 172 | } |
| 173 | } |