...
Run Format

Source file src/database/sql/driver/types_test.go

Documentation: database/sql/driver

     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 driver
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  type valueConverterTest struct {
    14  	c   ValueConverter
    15  	in  interface{}
    16  	out interface{}
    17  	err string
    18  }
    19  
    20  var now = time.Now()
    21  var answer int64 = 42
    22  
    23  type (
    24  	i  int64
    25  	f  float64
    26  	b  bool
    27  	bs []byte
    28  	s  string
    29  	t  time.Time
    30  	is []int
    31  )
    32  
    33  var valueConverterTests = []valueConverterTest{
    34  	{Bool, "true", true, ""},
    35  	{Bool, "True", true, ""},
    36  	{Bool, []byte("t"), true, ""},
    37  	{Bool, true, true, ""},
    38  	{Bool, "1", true, ""},
    39  	{Bool, 1, true, ""},
    40  	{Bool, int64(1), true, ""},
    41  	{Bool, uint16(1), true, ""},
    42  	{Bool, "false", false, ""},
    43  	{Bool, false, false, ""},
    44  	{Bool, "0", false, ""},
    45  	{Bool, 0, false, ""},
    46  	{Bool, int64(0), false, ""},
    47  	{Bool, uint16(0), false, ""},
    48  	{c: Bool, in: "foo", err: "sql/driver: couldn't convert \"foo\" into type bool"},
    49  	{c: Bool, in: 2, err: "sql/driver: couldn't convert 2 into type bool"},
    50  	{DefaultParameterConverter, now, now, ""},
    51  	{DefaultParameterConverter, (*int64)(nil), nil, ""},
    52  	{DefaultParameterConverter, &answer, answer, ""},
    53  	{DefaultParameterConverter, &now, now, ""},
    54  	{DefaultParameterConverter, i(9), int64(9), ""},
    55  	{DefaultParameterConverter, f(0.1), float64(0.1), ""},
    56  	{DefaultParameterConverter, b(true), true, ""},
    57  	{DefaultParameterConverter, bs{1}, []byte{1}, ""},
    58  	{DefaultParameterConverter, s("a"), "a", ""},
    59  	{DefaultParameterConverter, is{1}, nil, "unsupported type driver.is, a slice of int"},
    60  }
    61  
    62  func TestValueConverters(t *testing.T) {
    63  	for i, tt := range valueConverterTests {
    64  		out, err := tt.c.ConvertValue(tt.in)
    65  		goterr := ""
    66  		if err != nil {
    67  			goterr = err.Error()
    68  		}
    69  		if goterr != tt.err {
    70  			t.Errorf("test %d: %T(%T(%v)) error = %q; want error = %q",
    71  				i, tt.c, tt.in, tt.in, goterr, tt.err)
    72  		}
    73  		if tt.err != "" {
    74  			continue
    75  		}
    76  		if !reflect.DeepEqual(out, tt.out) {
    77  			t.Errorf("test %d: %T(%T(%v)) = %v (%T); want %v (%T)",
    78  				i, tt.c, tt.in, tt.in, out, out, tt.out, tt.out)
    79  		}
    80  	}
    81  }
    82  

View as plain text