-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathconn_test.go
126 lines (98 loc) · 3.14 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
package client
import (
"fmt"
. "github.com/pingcap/check"
"github.com/go-mysql-org/go-mysql/mysql"
)
type connTestSuite struct {
c *Conn
port string
}
func (s *connTestSuite) SetUpSuite(c *C) {
var err error
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
s.c, err = Connect(addr, *testUser, *testPassword, "")
if err != nil {
c.Fatal(err)
}
_, err = s.c.Execute("CREATE DATABASE IF NOT EXISTS " + *testDB)
c.Assert(err, IsNil)
_, err = s.c.Execute("USE " + *testDB)
c.Assert(err, IsNil)
s.testExecute_CreateTable(c)
}
func (s *connTestSuite) TearDownSuite(c *C) {
if s.c == nil {
return
}
s.testExecute_DropTable(c)
if s.c != nil {
s.c.Close()
}
}
var (
testExecuteSelectStreamingRows = [...]string{"foo", "helloworld", "bar", "", "spam"}
testExecuteSelectStreamingTablename = "execute_plain_table"
)
func (s *connTestSuite) testExecute_CreateTable(c *C) {
str := `CREATE TABLE IF NOT EXISTS ` + testExecuteSelectStreamingTablename + ` (
id INT UNSIGNED NOT NULL,
str VARCHAR(256),
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8`
result, err := s.c.Execute(str)
c.Assert(err, IsNil)
result.Close()
result, err = s.c.Execute(`TRUNCATE TABLE ` + testExecuteSelectStreamingTablename)
c.Assert(err, IsNil)
result.Close()
stmt, err := s.c.Prepare(`INSERT INTO ` + testExecuteSelectStreamingTablename + ` (id, str) VALUES (?, ?)`)
c.Assert(err, IsNil)
defer stmt.Close()
for id, str := range testExecuteSelectStreamingRows {
result, err := stmt.Execute(id, str)
c.Assert(err, IsNil)
result.Close()
}
}
func (s *connTestSuite) testExecute_DropTable(c *C) {
_, err := s.c.Execute(`drop table if exists ` + testExecuteSelectStreamingTablename)
c.Assert(err, IsNil)
}
func (s *connTestSuite) TestExecuteSelectStreaming(c *C) {
var (
expectedRowId int64
perResultCallbackCalledTimes int
result mysql.Result
)
const colsInResult = 2 // id, str
err := s.c.ExecuteSelectStreaming(`SELECT id, str FROM `+testExecuteSelectStreamingTablename+` ORDER BY id`,
&result,
func(row []mysql.FieldValue) error {
// Check number of columns
c.Assert(row, HasLen, colsInResult)
// Check type of columns
c.Assert(row[0].Type, Equals, mysql.FieldValueType(mysql.FieldValueTypeUnsigned))
c.Assert(row[1].Type, Equals, mysql.FieldValueType(mysql.FieldValueTypeString))
id := row[0].AsInt64()
str := row[1].AsString()
// Check order of rows
c.Assert(id, Equals, expectedRowId)
// Check string values (protection from incorrect reuse of memory)
c.Assert(string(str), Equals, testExecuteSelectStreamingRows[id])
expectedRowId++
return nil
}, func(result *mysql.Result) error {
// result.Resultset must be defined at this point
c.Assert(result.Resultset, NotNil)
// Check number of columns
c.Assert(result.Resultset.Fields, HasLen, colsInResult)
perResultCallbackCalledTimes++
return nil
})
c.Assert(err, IsNil)
// Check total rows count
c.Assert(expectedRowId, Equals, int64(len(testExecuteSelectStreamingRows)))
// Check perResultCallback call count
c.Assert(perResultCallbackCalledTimes, Equals, 1)
}