forked from siddontang/mixer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_test.go
151 lines (118 loc) · 2.77 KB
/
conn_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package client
import (
"fmt"
. "github.com/siddontang/mixer/mysql"
"testing"
)
func newTestConn() *Conn {
c := new(Conn)
if err := c.Connect("127.0.0.1:3306", "root", "", "mixer"); err != nil {
panic(err)
}
return c
}
func TestConn_Connect(t *testing.T) {
c := newTestConn()
defer c.Close()
}
func TestConn_Ping(t *testing.T) {
c := newTestConn()
defer c.Close()
if err := c.Ping(); err != nil {
t.Fatal(err)
}
}
func TestConn_DeleteTable(t *testing.T) {
c := newTestConn()
defer c.Close()
if _, err := c.Execute("drop table if exists mixer_test_conn"); err != nil {
t.Fatal(err)
}
}
func TestConn_CreateTable(t *testing.T) {
s := `CREATE TABLE IF NOT EXISTS mixer_test_conn (
id BIGINT(64) UNSIGNED NOT NULL,
str VARCHAR(256),
f DOUBLE,
e enum("test1", "test2"),
u tinyint unsigned,
i tinyint,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8`
c := newTestConn()
defer c.Close()
if _, err := c.Execute(s); err != nil {
t.Fatal(err)
}
}
func TestConn_Insert(t *testing.T) {
s := `insert into mixer_test_conn (id, str, f, e) values(1, "a", 3.14, "test1")`
c := newTestConn()
defer c.Close()
if pkg, err := c.Execute(s); err != nil {
t.Fatal(err)
} else {
if pkg.AffectedRows != 1 {
t.Fatal(pkg.AffectedRows)
}
}
}
func TestConn_Select(t *testing.T) {
s := `select str, f, e from mixer_test_conn where id = 1`
c := newTestConn()
defer c.Close()
if result, err := c.Execute(s); err != nil {
t.Fatal(err)
} else {
if len(result.Fields) != 3 {
t.Fatal(len(result.Fields))
}
if len(result.Values) != 1 {
t.Fatal(len(result.Values))
}
if str, _ := result.GetString(0, 0); str != "a" {
t.Fatal("invalid str", str)
}
if f, _ := result.GetFloat(0, 1); f != float64(3.14) {
t.Fatal("invalid f", f)
}
if e, _ := result.GetString(0, 2); e != "test1" {
t.Fatal("invalid e", e)
}
if str, _ := result.GetStringByName(0, "str"); str != "a" {
t.Fatal("invalid str", str)
}
if f, _ := result.GetFloatByName(0, "f"); f != float64(3.14) {
t.Fatal("invalid f", f)
}
if e, _ := result.GetStringByName(0, "e"); e != "test1" {
t.Fatal("invalid e", e)
}
}
}
func TestConn_Escape(t *testing.T) {
c := newTestConn()
defer c.Close()
e := `""''\abc`
s := fmt.Sprintf(`insert into mixer_test_conn (id, str) values(5, "%s")`,
Escape(e))
if _, err := c.Execute(s); err != nil {
t.Fatal(err)
}
s = `select str from mixer_test_conn where id = ?`
if r, err := c.Execute(s, 5); err != nil {
t.Fatal(err)
} else {
str, _ := r.GetString(0, 0)
if str != e {
t.Fatal(str)
}
}
}
func TestConn_SetCharset(t *testing.T) {
c := newTestConn()
defer c.Close()
if err := c.SetCharset("gb2312"); err != nil {
t.Fatal(err)
}
}