OSDN Git Service

98a6c193321848a4c5ee20ba87e6aa6a2257da1b
[bytom/vapor.git] / vendor / github.com / go-sql-driver / mysql / statement_test.go
1 // Go MySQL Driver - A MySQL-Driver for Go's database/sql package
2 //
3 // Copyright 2017 The Go-MySQL-Driver Authors. All rights reserved.
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this file,
7 // You can obtain one at http://mozilla.org/MPL/2.0/.
8
9 package mysql
10
11 import (
12         "bytes"
13         "testing"
14 )
15
16 func TestConvertDerivedString(t *testing.T) {
17         type derived string
18
19         output, err := converter{}.ConvertValue(derived("value"))
20         if err != nil {
21                 t.Fatal("Derived string type not convertible", err)
22         }
23
24         if output != "value" {
25                 t.Fatalf("Derived string type not converted, got %#v %T", output, output)
26         }
27 }
28
29 func TestConvertDerivedByteSlice(t *testing.T) {
30         type derived []uint8
31
32         output, err := converter{}.ConvertValue(derived("value"))
33         if err != nil {
34                 t.Fatal("Byte slice not convertible", err)
35         }
36
37         if bytes.Compare(output.([]byte), []byte("value")) != 0 {
38                 t.Fatalf("Byte slice not converted, got %#v %T", output, output)
39         }
40 }
41
42 func TestConvertDerivedUnsupportedSlice(t *testing.T) {
43         type derived []int
44
45         _, err := converter{}.ConvertValue(derived{1})
46         if err == nil || err.Error() != "unsupported type mysql.derived, a slice of int" {
47                 t.Fatal("Unexpected error", err)
48         }
49 }
50
51 func TestConvertDerivedBool(t *testing.T) {
52         type derived bool
53
54         output, err := converter{}.ConvertValue(derived(true))
55         if err != nil {
56                 t.Fatal("Derived bool type not convertible", err)
57         }
58
59         if output != true {
60                 t.Fatalf("Derived bool type not converted, got %#v %T", output, output)
61         }
62 }
63
64 func TestConvertPointer(t *testing.T) {
65         str := "value"
66
67         output, err := converter{}.ConvertValue(&str)
68         if err != nil {
69                 t.Fatal("Pointer type not convertible", err)
70         }
71
72         if output != "value" {
73                 t.Fatalf("Pointer type not converted, got %#v %T", output, output)
74         }
75 }
76
77 func TestConvertSignedIntegers(t *testing.T) {
78         values := []interface{}{
79                 int8(-42),
80                 int16(-42),
81                 int32(-42),
82                 int64(-42),
83                 int(-42),
84         }
85
86         for _, value := range values {
87                 output, err := converter{}.ConvertValue(value)
88                 if err != nil {
89                         t.Fatalf("%T type not convertible %s", value, err)
90                 }
91
92                 if output != int64(-42) {
93                         t.Fatalf("%T type not converted, got %#v %T", value, output, output)
94                 }
95         }
96 }
97
98 func TestConvertUnsignedIntegers(t *testing.T) {
99         values := []interface{}{
100                 uint8(42),
101                 uint16(42),
102                 uint32(42),
103                 uint64(42),
104                 uint(42),
105         }
106
107         for _, value := range values {
108                 output, err := converter{}.ConvertValue(value)
109                 if err != nil {
110                         t.Fatalf("%T type not convertible %s", value, err)
111                 }
112
113                 if output != int64(42) {
114                         t.Fatalf("%T type not converted, got %#v %T", value, output, output)
115                 }
116         }
117
118         output, err := converter{}.ConvertValue(^uint64(0))
119         if err != nil {
120                 t.Fatal("uint64 high-bit not convertible", err)
121         }
122
123         if output != "18446744073709551615" {
124                 t.Fatalf("uint64 high-bit not converted, got %#v %T", output, output)
125         }
126 }