forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidb_test.go
406 lines (348 loc) · 10.7 KB
/
tidb_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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2015 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package tidb
import (
"database/sql"
"errors"
"flag"
"fmt"
"os"
"runtime"
"sync"
"testing"
"time"
"github.com/ngaut/log"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/rset"
)
var store = flag.String("store", "memory", "registered store name, [memory, goleveldb, boltdb]")
func TestT(t *testing.T) {
TestingT(t)
}
var _ = Suite(&testMainSuite{})
type testMainSuite struct {
dbName string
createDBSQL string
dropDBSQL string
useDBSQL string
createTableSQL string
insertSQL string
selectSQL string
}
type brokenStore struct{}
func (s *brokenStore) Open(schema string) (kv.Storage, error) {
return nil, errors.New("try again later")
}
func (s *testMainSuite) SetUpSuite(c *C) {
s.dbName = "test_main_db"
s.createDBSQL = fmt.Sprintf("create database if not exists %s;", s.dbName)
s.dropDBSQL = fmt.Sprintf("drop database %s;", s.dbName)
s.useDBSQL = fmt.Sprintf("use %s;", s.dbName)
s.createTableSQL = `
CREATE TABLE tbl_test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id));
CREATE TABLE tbl_test1(id INT NOT NULL DEFAULT 2, name varchar(255), PRIMARY KEY(id), INDEX name(name));
CREATE TABLE tbl_test2(id INT NOT NULL DEFAULT 3, name varchar(255), PRIMARY KEY(id));`
s.selectSQL = `SELECT * from tbl_test;`
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetLevelByString("error")
}
func (s *testMainSuite) TearDownSuite(c *C) {
removeStore(c, s.dbName)
}
func mustBegin(c *C, currDB *sql.DB) *sql.Tx {
tx, err := currDB.Begin()
c.Assert(err, IsNil)
return tx
}
func mustCommit(c *C, tx *sql.Tx) {
err := tx.Commit()
c.Assert(err, IsNil)
}
func mustExecuteSQL(c *C, tx *sql.Tx, sql string, args ...interface{}) sql.Result {
r, err := tx.Exec(sql, args...)
c.Assert(err, IsNil)
return r
}
func mustExec(c *C, currDB *sql.DB, sql string, args ...interface{}) sql.Result {
tx := mustBegin(c, currDB)
r := mustExecuteSQL(c, tx, sql, args...)
mustCommit(c, tx)
return r
}
func checkResult(c *C, r sql.Result, affectedRows int64, insertID int64) {
gotRows, err := r.RowsAffected()
c.Assert(err, IsNil)
c.Assert(gotRows, Equals, affectedRows)
gotID, err := r.LastInsertId()
c.Assert(err, IsNil)
c.Assert(gotID, Equals, insertID)
}
func (s *testMainSuite) TestConcurrent(c *C) {
dbName := "test_concurrent_db"
defer removeStore(c, dbName)
testDB, err := sql.Open(DriverName, *store+"://"+dbName+"/"+dbName)
c.Assert(err, IsNil)
defer testDB.Close()
var wg sync.WaitGroup
// create db
createDBSQL := fmt.Sprintf("create database if not exists %s;", dbName)
dropDBSQL := fmt.Sprintf("drop database %s;", dbName)
useDBSQL := fmt.Sprintf("use %s;", dbName)
createTableSQL := ` CREATE TABLE test(id INT NOT NULL DEFAULT 1, name varchar(255), PRIMARY KEY(id)); `
mustExec(c, testDB, dropDBSQL)
mustExec(c, testDB, createDBSQL)
mustExec(c, testDB, useDBSQL)
mustExec(c, testDB, createTableSQL)
f := func(start, count int) {
defer wg.Done()
for i := 0; i < count; i++ {
// insert data
mustExec(c, testDB, fmt.Sprintf(`INSERT INTO test VALUES (%d, "hello");`, start+i))
}
}
step := 10
for i := 0; i < step; i++ {
wg.Add(1)
go f(i*step, step)
}
wg.Wait()
mustExec(c, testDB, dropDBSQL)
}
func (s *testMainSuite) TestTableInfoMeta(c *C) {
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
defer testDB.Close()
// create db
mustExec(c, testDB, s.createDBSQL)
// use db
mustExec(c, testDB, s.useDBSQL)
// create table
mustExec(c, testDB, s.createTableSQL)
// insert data
r := mustExec(c, testDB, `INSERT INTO tbl_test VALUES (1, "hello");`)
checkResult(c, r, 1, 0)
r = mustExec(c, testDB, `INSERT INTO tbl_test VALUES (2, "hello");`)
checkResult(c, r, 1, 0)
r = mustExec(c, testDB, `UPDATE tbl_test SET name = "abc" where id = 2;`)
checkResult(c, r, 1, 0)
r = mustExec(c, testDB, `DELETE from tbl_test where id = 2;`)
checkResult(c, r, 1, 0)
// select data
tx := mustBegin(c, testDB)
rows, err := tx.Query(s.selectSQL)
c.Assert(err, IsNil)
defer rows.Close()
for rows.Next() {
var id int
var name string
rows.Scan(&id, &name)
c.Assert(id, Equals, 1)
c.Assert(name, Equals, "hello")
}
mustCommit(c, tx)
// drop db
mustExec(c, testDB, s.dropDBSQL)
}
func (s *testMainSuite) TestInfoSchema(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)
rs := mustExecSQL(c, se, "SELECT CHARACTER_SET_NAME FROM INFORMATION_SCHEMA.CHARACTER_SETS WHERE CHARACTER_SET_NAME = 'utf8mb4'")
row, err := rs.FirstRow()
c.Assert(err, IsNil)
match(c, row, "utf8mb4")
}
func (s *testMainSuite) TestCaseInsensitive(c *C) {
store := newStore(c, s.dbName)
se := newSession(c, store, s.dbName)
mustExecSQL(c, se, "create table T (a text, B int)")
mustExecSQL(c, se, "insert t (A, b) values ('aaa', 1)")
rs := mustExecSQL(c, se, "select * from t")
fields, err := rs.Fields()
c.Assert(err, IsNil)
c.Assert(fields[0].Name, Equals, "a")
c.Assert(fields[1].Name, Equals, "B")
rs = mustExecSQL(c, se, "select A, b from t")
fields, err = rs.Fields()
c.Assert(err, IsNil)
c.Assert(fields[0].Name, Equals, "A")
c.Assert(fields[1].Name, Equals, "b")
mustExecSQL(c, se, "update T set b = B + 1")
mustExecSQL(c, se, "update T set B = b + 1")
rs = mustExecSQL(c, se, "select b from T")
rows, err := rs.Rows(-1, 0)
c.Assert(err, IsNil)
match(c, rows[0], 3)
mustExecSQL(c, se, s.dropDBSQL)
}
func (s *testMainSuite) TestDriverPrepare(c *C) {
testDB, err := sql.Open(DriverName, *store+"://"+s.dbName+"/"+s.dbName)
c.Assert(err, IsNil)
defer testDB.Close()
// create db
mustExec(c, testDB, s.createDBSQL)
// use db
mustExec(c, testDB, s.useDBSQL)
// create table
mustExec(c, testDB, "create table t (a int, b int)")
mustExec(c, testDB, "insert t values (?, ?)", 1, 2)
row := testDB.QueryRow("select * from t where 1 = ?", 1)
// TODO: This will fail test
// row := testDB.QueryRow("select ?, ? from t", "a", "b")
var a, b int
err = row.Scan(&a, &b)
c.Assert(err, IsNil)
c.Assert(a, Equals, 1)
c.Assert(b, Equals, 2)
mustExec(c, testDB, s.dropDBSQL)
}
// Testcase for delete panic
func (s *testMainSuite) TestDeletePanic(c *C) {
db, err := sql.Open("tidb", "memory://test/test")
defer db.Close()
_, err = db.Exec("create table t (c int)")
c.Assert(err, IsNil)
_, err = db.Exec("insert into t values (1), (2), (3)")
c.Assert(err, IsNil)
_, err = db.Query("delete from `t` where `c` = ?", 1)
c.Assert(err, IsNil)
rs, err := db.Query("delete from `t` where `c` = ?", 2)
c.Assert(err, IsNil)
cols, err := rs.Columns()
c.Assert(err, IsNil)
c.Assert(cols, HasLen, 0)
c.Assert(rs.Next(), IsFalse)
c.Assert(rs.Next(), IsFalse)
c.Assert(rs.Close(), IsNil)
}
// Testcase for arg type.
func (s *testMainSuite) TestCheckArgs(c *C) {
db, err := sql.Open("tidb", "memory://test/test")
defer db.Close()
c.Assert(err, IsNil)
mustExec(c, db, "create table if not exists t (c datetime)")
mustExec(c, db, "insert t values (?)", time.Now())
mustExec(c, db, "drop table t")
checkArgs(nil, true, false, int8(1), int16(1), int32(1), int64(1), 1,
uint8(1), uint16(1), uint32(1), uint64(1), uint(1), float32(1), float64(1),
"abc", []byte("abc"), time.Now(), time.Hour, time.Local)
}
func (s *testMainSuite) TestIsQuery(c *C) {
tbl := []struct {
sql string
ok bool
}{
{"/*comment*/ select 1;", true},
{"/*comment*/ /*comment*/ select 1;", true},
{"select /*comment*/ 1 /*comment*/;", true},
{"(select /*comment*/ 1 /*comment*/);", true},
}
for _, t := range tbl {
c.Assert(IsQuery(t.sql), Equals, t.ok, Commentf(t.sql))
}
}
func (s *testMainSuite) TestitrimSQL(c *C) {
tbl := []struct {
sql string
target string
}{
{"/*comment*/ select 1; ", "select 1;"},
{"/*comment*/ /*comment*/ select 1;", "select 1;"},
{"select /*comment*/ 1 /*comment*/;", "select /*comment*/ 1 /*comment*/;"},
{"/*comment select 1; ", "/*comment select 1;"},
}
for _, t := range tbl {
c.Assert(trimSQL(t.sql), Equals, t.target, Commentf(t.sql))
}
}
func (s *testMainSuite) TestRetryOpenStore(c *C) {
begin := time.Now()
RegisterStore("dummy", &brokenStore{})
_, err := newStoreWithRetry("dummy://dummy-store", 3)
c.Assert(err, NotNil)
elapse := time.Since(begin)
c.Assert(uint64(elapse), GreaterEqual, uint64(3*time.Second))
}
func sessionExec(c *C, se Session, sql string) ([]rset.Recordset, error) {
se.Execute("BEGIN;")
r, err := se.Execute(sql)
c.Assert(err, IsNil)
se.Execute("COMMIT;")
return r, err
}
func newStore(c *C, dbPath string) kv.Storage {
store, err := NewStore(*store + "://" + dbPath)
c.Assert(err, IsNil)
return store
}
func newSession(c *C, store kv.Storage, dbName string) Session {
se, err := CreateSession(store)
c.Assert(err, IsNil)
mustExecSQL(c, se, "create database if not exists "+dbName)
mustExecSQL(c, se, "use "+dbName)
return se
}
func removeStore(c *C, dbPath string) {
os.RemoveAll(dbPath)
}
func exec(c *C, se Session, sql string, args ...interface{}) (rset.Recordset, error) {
if len(args) == 0 {
rs, err := se.Execute(sql)
if err == nil && len(rs) > 0 {
return rs[0], nil
}
return nil, err
}
stmtID, _, _, err := se.PrepareStmt(sql)
if err != nil {
return nil, err
}
rs, err := se.ExecutePreparedStmt(stmtID, args...)
if err != nil {
return nil, err
}
return rs, nil
}
func mustExecSQL(c *C, se Session, sql string, args ...interface{}) rset.Recordset {
rs, err := exec(c, se, sql, args...)
c.Assert(err, IsNil)
return rs
}
func match(c *C, row []interface{}, expected ...interface{}) {
c.Assert(len(row), Equals, len(expected))
for i := range row {
got := fmt.Sprintf("%v", row[i])
need := fmt.Sprintf("%v", expected[i])
c.Assert(got, Equals, need)
}
}
func matches(c *C, rows [][]interface{}, expected [][]interface{}) {
c.Assert(len(rows), Equals, len(expected))
for i := 0; i < len(rows); i++ {
match(c, rows[i], expected[i]...)
}
}
func mustExecMatch(c *C, se Session, sql string, expected [][]interface{}) {
r := mustExecSQL(c, se, sql)
rows, err := r.Rows(-1, 0)
c.Assert(err, IsNil)
matches(c, rows, expected)
}
func mustExecFailed(c *C, se Session, sql string, args ...interface{}) {
r, err := exec(c, se, sql, args...)
if err == nil && r != nil {
// sometimes we may meet error after executing first row.
_, err = r.FirstRow()
}
c.Assert(err, NotNil)
}