mirror of https://github.com/golang/go.git
database/sql: fix TestConversions when count > 1
Provide a fresh conversion table for TestConversions as it gets modified on each test. Change-Id: I6e2240d0c3455451271a6879e994b82222c3d44c Reviewed-on: https://go-review.googlesource.com/89595 Run-TryBot: Daniel Theophanes <kardianos@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
parent
e72e69a3f6
commit
1b69d19a42
|
|
@ -64,125 +64,128 @@ var (
|
|||
scaniface interface{}
|
||||
)
|
||||
|
||||
var conversionTests = []conversionTest{
|
||||
// Exact conversions (destination pointer type matches source type)
|
||||
{s: "foo", d: &scanstr, wantstr: "foo"},
|
||||
{s: 123, d: &scanint, wantint: 123},
|
||||
{s: someTime, d: &scantime, wanttime: someTime},
|
||||
func conversionTests() []conversionTest {
|
||||
// Return a fresh instance to test so "go test -count 2" works correctly.
|
||||
return []conversionTest{
|
||||
// Exact conversions (destination pointer type matches source type)
|
||||
{s: "foo", d: &scanstr, wantstr: "foo"},
|
||||
{s: 123, d: &scanint, wantint: 123},
|
||||
{s: someTime, d: &scantime, wanttime: someTime},
|
||||
|
||||
// To strings
|
||||
{s: "string", d: &scanstr, wantstr: "string"},
|
||||
{s: []byte("byteslice"), d: &scanstr, wantstr: "byteslice"},
|
||||
{s: 123, d: &scanstr, wantstr: "123"},
|
||||
{s: int8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: int64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint16(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint32(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: 1.5, d: &scanstr, wantstr: "1.5"},
|
||||
// To strings
|
||||
{s: "string", d: &scanstr, wantstr: "string"},
|
||||
{s: []byte("byteslice"), d: &scanstr, wantstr: "byteslice"},
|
||||
{s: 123, d: &scanstr, wantstr: "123"},
|
||||
{s: int8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: int64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint8(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint16(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint32(123), d: &scanstr, wantstr: "123"},
|
||||
{s: uint64(123), d: &scanstr, wantstr: "123"},
|
||||
{s: 1.5, d: &scanstr, wantstr: "1.5"},
|
||||
|
||||
// From time.Time:
|
||||
{s: time.Unix(1, 0).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01Z"},
|
||||
{s: time.Unix(1453874597, 0).In(time.FixedZone("here", -3600*8)), d: &scanstr, wantstr: "2016-01-26T22:03:17-08:00"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01.000000002Z"},
|
||||
{s: time.Time{}, d: &scanstr, wantstr: "0001-01-01T00:00:00Z"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanbytes, wantbytes: []byte("1970-01-01T00:00:01.000000002Z")},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scaniface, wantiface: time.Unix(1, 2).UTC()},
|
||||
// From time.Time:
|
||||
{s: time.Unix(1, 0).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01Z"},
|
||||
{s: time.Unix(1453874597, 0).In(time.FixedZone("here", -3600*8)), d: &scanstr, wantstr: "2016-01-26T22:03:17-08:00"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanstr, wantstr: "1970-01-01T00:00:01.000000002Z"},
|
||||
{s: time.Time{}, d: &scanstr, wantstr: "0001-01-01T00:00:00Z"},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scanbytes, wantbytes: []byte("1970-01-01T00:00:01.000000002Z")},
|
||||
{s: time.Unix(1, 2).UTC(), d: &scaniface, wantiface: time.Unix(1, 2).UTC()},
|
||||
|
||||
// To []byte
|
||||
{s: nil, d: &scanbytes, wantbytes: nil},
|
||||
{s: "string", d: &scanbytes, wantbytes: []byte("string")},
|
||||
{s: []byte("byteslice"), d: &scanbytes, wantbytes: []byte("byteslice")},
|
||||
{s: 123, d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint16(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint32(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: 1.5, d: &scanbytes, wantbytes: []byte("1.5")},
|
||||
// To []byte
|
||||
{s: nil, d: &scanbytes, wantbytes: nil},
|
||||
{s: "string", d: &scanbytes, wantbytes: []byte("string")},
|
||||
{s: []byte("byteslice"), d: &scanbytes, wantbytes: []byte("byteslice")},
|
||||
{s: 123, d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: int64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint8(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint16(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint32(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: uint64(123), d: &scanbytes, wantbytes: []byte("123")},
|
||||
{s: 1.5, d: &scanbytes, wantbytes: []byte("1.5")},
|
||||
|
||||
// To RawBytes
|
||||
{s: nil, d: &scanraw, wantraw: nil},
|
||||
{s: []byte("byteslice"), d: &scanraw, wantraw: RawBytes("byteslice")},
|
||||
{s: "string", d: &scanraw, wantraw: RawBytes("string")},
|
||||
{s: 123, d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: int8(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: int64(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint8(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint16(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint32(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint64(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: 1.5, d: &scanraw, wantraw: RawBytes("1.5")},
|
||||
// time.Time has been placed here to check that the RawBytes slice gets
|
||||
// correctly reset when calling time.Time.AppendFormat.
|
||||
{s: time.Unix(2, 5).UTC(), d: &scanraw, wantraw: RawBytes("1970-01-01T00:00:02.000000005Z")},
|
||||
// To RawBytes
|
||||
{s: nil, d: &scanraw, wantraw: nil},
|
||||
{s: []byte("byteslice"), d: &scanraw, wantraw: RawBytes("byteslice")},
|
||||
{s: "string", d: &scanraw, wantraw: RawBytes("string")},
|
||||
{s: 123, d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: int8(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: int64(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint8(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint16(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint32(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: uint64(123), d: &scanraw, wantraw: RawBytes("123")},
|
||||
{s: 1.5, d: &scanraw, wantraw: RawBytes("1.5")},
|
||||
// time.Time has been placed here to check that the RawBytes slice gets
|
||||
// correctly reset when calling time.Time.AppendFormat.
|
||||
{s: time.Unix(2, 5).UTC(), d: &scanraw, wantraw: RawBytes("1970-01-01T00:00:02.000000005Z")},
|
||||
|
||||
// Strings to integers
|
||||
{s: "255", d: &scanuint8, wantuint: 255},
|
||||
{s: "256", d: &scanuint8, wanterr: "converting driver.Value type string (\"256\") to a uint8: value out of range"},
|
||||
{s: "256", d: &scanuint16, wantuint: 256},
|
||||
{s: "-1", d: &scanint, wantint: -1},
|
||||
{s: "foo", d: &scanint, wanterr: "converting driver.Value type string (\"foo\") to a int: invalid syntax"},
|
||||
// Strings to integers
|
||||
{s: "255", d: &scanuint8, wantuint: 255},
|
||||
{s: "256", d: &scanuint8, wanterr: "converting driver.Value type string (\"256\") to a uint8: value out of range"},
|
||||
{s: "256", d: &scanuint16, wantuint: 256},
|
||||
{s: "-1", d: &scanint, wantint: -1},
|
||||
{s: "foo", d: &scanint, wanterr: "converting driver.Value type string (\"foo\") to a int: invalid syntax"},
|
||||
|
||||
// int64 to smaller integers
|
||||
{s: int64(5), d: &scanuint8, wantuint: 5},
|
||||
{s: int64(256), d: &scanuint8, wanterr: "converting driver.Value type int64 (\"256\") to a uint8: value out of range"},
|
||||
{s: int64(256), d: &scanuint16, wantuint: 256},
|
||||
{s: int64(65536), d: &scanuint16, wanterr: "converting driver.Value type int64 (\"65536\") to a uint16: value out of range"},
|
||||
// int64 to smaller integers
|
||||
{s: int64(5), d: &scanuint8, wantuint: 5},
|
||||
{s: int64(256), d: &scanuint8, wanterr: "converting driver.Value type int64 (\"256\") to a uint8: value out of range"},
|
||||
{s: int64(256), d: &scanuint16, wantuint: 256},
|
||||
{s: int64(65536), d: &scanuint16, wanterr: "converting driver.Value type int64 (\"65536\") to a uint16: value out of range"},
|
||||
|
||||
// True bools
|
||||
{s: true, d: &scanbool, wantbool: true},
|
||||
{s: "True", d: &scanbool, wantbool: true},
|
||||
{s: "TRUE", d: &scanbool, wantbool: true},
|
||||
{s: "1", d: &scanbool, wantbool: true},
|
||||
{s: 1, d: &scanbool, wantbool: true},
|
||||
{s: int64(1), d: &scanbool, wantbool: true},
|
||||
{s: uint16(1), d: &scanbool, wantbool: true},
|
||||
// True bools
|
||||
{s: true, d: &scanbool, wantbool: true},
|
||||
{s: "True", d: &scanbool, wantbool: true},
|
||||
{s: "TRUE", d: &scanbool, wantbool: true},
|
||||
{s: "1", d: &scanbool, wantbool: true},
|
||||
{s: 1, d: &scanbool, wantbool: true},
|
||||
{s: int64(1), d: &scanbool, wantbool: true},
|
||||
{s: uint16(1), d: &scanbool, wantbool: true},
|
||||
|
||||
// False bools
|
||||
{s: false, d: &scanbool, wantbool: false},
|
||||
{s: "false", d: &scanbool, wantbool: false},
|
||||
{s: "FALSE", d: &scanbool, wantbool: false},
|
||||
{s: "0", d: &scanbool, wantbool: false},
|
||||
{s: 0, d: &scanbool, wantbool: false},
|
||||
{s: int64(0), d: &scanbool, wantbool: false},
|
||||
{s: uint16(0), d: &scanbool, wantbool: false},
|
||||
// False bools
|
||||
{s: false, d: &scanbool, wantbool: false},
|
||||
{s: "false", d: &scanbool, wantbool: false},
|
||||
{s: "FALSE", d: &scanbool, wantbool: false},
|
||||
{s: "0", d: &scanbool, wantbool: false},
|
||||
{s: 0, d: &scanbool, wantbool: false},
|
||||
{s: int64(0), d: &scanbool, wantbool: false},
|
||||
{s: uint16(0), d: &scanbool, wantbool: false},
|
||||
|
||||
// Not bools
|
||||
{s: "yup", d: &scanbool, wanterr: `sql/driver: couldn't convert "yup" into type bool`},
|
||||
{s: 2, d: &scanbool, wanterr: `sql/driver: couldn't convert 2 into type bool`},
|
||||
// Not bools
|
||||
{s: "yup", d: &scanbool, wanterr: `sql/driver: couldn't convert "yup" into type bool`},
|
||||
{s: 2, d: &scanbool, wanterr: `sql/driver: couldn't convert 2 into type bool`},
|
||||
|
||||
// Floats
|
||||
{s: float64(1.5), d: &scanf64, wantf64: float64(1.5)},
|
||||
{s: int64(1), d: &scanf64, wantf64: float64(1)},
|
||||
{s: float64(1.5), d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf64, wantf64: float64(1.5)},
|
||||
// Floats
|
||||
{s: float64(1.5), d: &scanf64, wantf64: float64(1.5)},
|
||||
{s: int64(1), d: &scanf64, wantf64: float64(1)},
|
||||
{s: float64(1.5), d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf32, wantf32: float32(1.5)},
|
||||
{s: "1.5", d: &scanf64, wantf64: float64(1.5)},
|
||||
|
||||
// Pointers
|
||||
{s: interface{}(nil), d: &scanptr, wantnil: true},
|
||||
{s: int64(42), d: &scanptr, wantptr: &answer},
|
||||
// Pointers
|
||||
{s: interface{}(nil), d: &scanptr, wantnil: true},
|
||||
{s: int64(42), d: &scanptr, wantptr: &answer},
|
||||
|
||||
// To interface{}
|
||||
{s: float64(1.5), d: &scaniface, wantiface: float64(1.5)},
|
||||
{s: int64(1), d: &scaniface, wantiface: int64(1)},
|
||||
{s: "str", d: &scaniface, wantiface: "str"},
|
||||
{s: []byte("byteslice"), d: &scaniface, wantiface: []byte("byteslice")},
|
||||
{s: true, d: &scaniface, wantiface: true},
|
||||
{s: nil, d: &scaniface},
|
||||
{s: []byte(nil), d: &scaniface, wantiface: []byte(nil)},
|
||||
// To interface{}
|
||||
{s: float64(1.5), d: &scaniface, wantiface: float64(1.5)},
|
||||
{s: int64(1), d: &scaniface, wantiface: int64(1)},
|
||||
{s: "str", d: &scaniface, wantiface: "str"},
|
||||
{s: []byte("byteslice"), d: &scaniface, wantiface: []byte("byteslice")},
|
||||
{s: true, d: &scaniface, wantiface: true},
|
||||
{s: nil, d: &scaniface},
|
||||
{s: []byte(nil), d: &scaniface, wantiface: []byte(nil)},
|
||||
|
||||
// To a user-defined type
|
||||
{s: 1.5, d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: int64(123), d: new(userDefined), wantusrdef: 123},
|
||||
{s: "1.5", d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: []byte{1, 2, 3}, d: new(userDefinedSlice), wanterr: `unsupported Scan, storing driver.Value type []uint8 into type *sql.userDefinedSlice`},
|
||||
{s: "str", d: new(userDefinedString), wantusrstr: "str"},
|
||||
// To a user-defined type
|
||||
{s: 1.5, d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: int64(123), d: new(userDefined), wantusrdef: 123},
|
||||
{s: "1.5", d: new(userDefined), wantusrdef: 1.5},
|
||||
{s: []byte{1, 2, 3}, d: new(userDefinedSlice), wanterr: `unsupported Scan, storing driver.Value type []uint8 into type *sql.userDefinedSlice`},
|
||||
{s: "str", d: new(userDefinedString), wantusrstr: "str"},
|
||||
|
||||
// Other errors
|
||||
{s: complex(1, 2), d: &scanstr, wanterr: `unsupported Scan, storing driver.Value type complex128 into type *string`},
|
||||
// Other errors
|
||||
{s: complex(1, 2), d: &scanstr, wanterr: `unsupported Scan, storing driver.Value type complex128 into type *string`},
|
||||
}
|
||||
}
|
||||
|
||||
func intPtrValue(intptr interface{}) interface{} {
|
||||
|
|
@ -210,7 +213,7 @@ func timeValue(ptr interface{}) time.Time {
|
|||
}
|
||||
|
||||
func TestConversions(t *testing.T) {
|
||||
for n, ct := range conversionTests {
|
||||
for n, ct := range conversionTests() {
|
||||
err := convertAssign(ct.d, ct.s)
|
||||
errstr := ""
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Reference in New Issue