blob: 702ba4399d5445bb87494c8b6cbaa63e94dbc2ec [file] [log] [blame]
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -07001// 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
5package sql
6
7import (
8 "fmt"
9 "reflect"
10 "testing"
Brad Fitzpatrickbf734d62012-01-13 15:45:05 -080011 "time"
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070012)
13
Brad Fitzpatrickbf734d62012-01-13 15:45:05 -080014var someTime = time.Unix(123, 0)
15
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070016type 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 Fitzpatrick8089e572011-11-02 11:46:04 -070023 wantf32 float32
24 wantf64 float64
Brad Fitzpatrickbf734d62012-01-13 15:45:05 -080025 wanttime time.Time
Brad Fitzpatrick8089e572011-11-02 11:46:04 -070026 wantbool bool // used if d is of type *bool
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070027 wanterr string
28}
29
30// Target variables for scanning into.
31var (
32 scanstr string
33 scanint int
34 scanint8 int8
35 scanint16 int16
36 scanint32 int32
37 scanuint8 uint8
38 scanuint16 uint16
Brad Fitzpatrick8089e572011-11-02 11:46:04 -070039 scanbool bool
40 scanf32 float32
41 scanf64 float64
Brad Fitzpatrickbf734d62012-01-13 15:45:05 -080042 scantime time.Time
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070043)
44
45var 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 Fitzpatrickbf734d62012-01-13 15:45:05 -080049 {s: someTime, d: &scantime, wanttime: someTime},
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070050
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 Pike02f67192011-12-13 10:42:05 -080064 {s: "256", d: &scanuint8, wanterr: `converting string "256" to a uint8: strconv.ParseUint: parsing "256": value out of range`},
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -070065 {s: "256", d: &scanuint16, wantuint: 256},
66 {s: "-1", d: &scanint, wantint: -1},
Rob Pike02f67192011-12-13 10:42:05 -080067 {s: "foo", d: &scanint, wanterr: `converting string "foo" to a int: strconv.ParseInt: parsing "foo": invalid syntax`},
Brad Fitzpatrick8089e572011-11-02 11:46:04 -070068
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 Fitzpatrick357f2cb2011-09-29 16:12:21 -070097}
98
99func intValue(intptr interface{}) int64 {
100 return reflect.Indirect(reflect.ValueOf(intptr)).Int()
101}
102
103func uintValue(intptr interface{}) uint64 {
104 return reflect.Indirect(reflect.ValueOf(intptr)).Uint()
105}
106
Brad Fitzpatrick8089e572011-11-02 11:46:04 -0700107func float64Value(ptr interface{}) float64 {
108 return *(ptr.(*float64))
109}
110
111func float32Value(ptr interface{}) float32 {
112 return *(ptr.(*float32))
113}
114
Brad Fitzpatrickbf734d62012-01-13 15:45:05 -0800115func timeValue(ptr interface{}) time.Time {
116 return *(ptr.(*time.Time))
117}
118
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700119func TestConversions(t *testing.T) {
120 for n, ct := range conversionTests {
121 err := convertAssign(ct.d, ct.s)
122 errstr := ""
123 if err != nil {
Russ Coxc2049d22011-11-01 22:04:37 -0400124 errstr = err.Error()
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700125 }
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 Fitzpatrick8089e572011-11-02 11:46:04 -0700142 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 Fitzpatrickbf734d62012-01-13 15:45:05 -0800151 if !ct.wanttime.IsZero() && !ct.wanttime.Equal(timeValue(ct.d)) {
152 errf("want time %v, got %v", ct.wanttime, timeValue(ct.d))
153 }
Brad Fitzpatrick357f2cb2011-09-29 16:12:21 -0700154 }
155}
156
157func 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}