@@ -5,25 +5,24 @@ import (
5
5
"testing"
6
6
"time"
7
7
8
- . "github.com/pingcap/check"
9
- "github.com/pingcap/errors"
10
8
"github.com/pingcap/tidb/parser"
11
9
"github.com/siddontang/go-log/log"
10
+ "github.com/stretchr/testify/require"
11
+ "github.com/stretchr/testify/suite"
12
12
13
13
"github.com/go-mysql-org/go-mysql/mysql"
14
14
"github.com/go-mysql-org/go-mysql/replication"
15
15
"github.com/go-mysql-org/go-mysql/test_util"
16
16
)
17
17
18
- func Test (t * testing.T ) {
19
- TestingT (t )
20
- }
21
-
22
18
type canalTestSuite struct {
19
+ suite.Suite
23
20
c * Canal
24
21
}
25
22
26
- var _ = Suite (& canalTestSuite {})
23
+ func TestCanalSuite (t * testing.T ) {
24
+ suite .Run (t , new (canalTestSuite ))
25
+ }
27
26
28
27
const (
29
28
miA = 0
@@ -35,7 +34,7 @@ const (
35
34
umiC = 16777215
36
35
)
37
36
38
- func (s * canalTestSuite ) SetUpSuite ( c * C ) {
37
+ func (s * canalTestSuite ) SetupSuite ( ) {
39
38
cfg := NewDefaultConfig ()
40
39
cfg .Addr = fmt .Sprintf ("%s:%s" , * test_util .MysqlHost , * test_util .MysqlPort )
41
40
cfg .User = "root"
@@ -55,8 +54,8 @@ func (s *canalTestSuite) SetUpSuite(c *C) {
55
54
56
55
var err error
57
56
s .c , err = NewCanal (cfg )
58
- c . Assert ( err , IsNil )
59
- s .execute (c , "DROP TABLE IF EXISTS test.canal_test" )
57
+ require . NoError ( s . T (), err )
58
+ s .execute ("DROP TABLE IF EXISTS test.canal_test" )
60
59
sql := `
61
60
CREATE TABLE IF NOT EXISTS test.canal_test (
62
61
id int AUTO_INCREMENT,
@@ -68,28 +67,28 @@ func (s *canalTestSuite) SetUpSuite(c *C) {
68
67
)ENGINE=innodb;
69
68
`
70
69
71
- s .execute (c , sql )
70
+ s .execute (sql )
72
71
73
- s .execute (c , "DELETE FROM test.canal_test" )
74
- s .execute (c , "INSERT INTO test.canal_test (content, name, mi, umi) VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)" ,
72
+ s .execute ("DELETE FROM test.canal_test" )
73
+ s .execute ("INSERT INTO test.canal_test (content, name, mi, umi) VALUES (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)" ,
75
74
"1" , "a" , miA , umiA ,
76
75
`\0\ndsfasdf` , "b" , miC , umiC ,
77
76
"" , "c" , miB , umiB ,
78
77
)
79
78
80
- s .execute (c , "SET GLOBAL binlog_format = 'ROW'" )
79
+ s .execute ("SET GLOBAL binlog_format = 'ROW'" )
81
80
82
- s .c .SetEventHandler (& testEventHandler {c : c })
81
+ s .c .SetEventHandler (& testEventHandler {})
83
82
go func () {
84
83
set , _ := mysql .ParseGTIDSet ("mysql" , "" )
85
84
err = s .c .StartFromGTID (set )
86
- c . Assert ( err , IsNil )
85
+ require . NoError ( s . T (), err )
87
86
}()
88
87
}
89
88
90
- func (s * canalTestSuite ) TearDownSuite (c * C ) {
89
+ func (s * canalTestSuite ) TearDownSuite () {
91
90
// To test the heartbeat and read timeout,so need to sleep 1 seconds without data transmission
92
- c .Logf ("Start testing the heartbeat and read timeout" )
91
+ s . T () .Logf ("Start testing the heartbeat and read timeout" )
93
92
time .Sleep (time .Second )
94
93
95
94
if s .c != nil {
@@ -98,15 +97,14 @@ func (s *canalTestSuite) TearDownSuite(c *C) {
98
97
}
99
98
}
100
99
101
- func (s * canalTestSuite ) execute (c * C , query string , args ... interface {}) * mysql.Result {
100
+ func (s * canalTestSuite ) execute (query string , args ... interface {}) * mysql.Result {
102
101
r , err := s .c .Execute (query , args ... )
103
- c . Assert ( err , IsNil )
102
+ require . NoError ( s . T (), err )
104
103
return r
105
104
}
106
105
107
106
type testEventHandler struct {
108
107
DummyEventHandler
109
- c * C
110
108
}
111
109
112
110
func (h * testEventHandler ) OnRow (e * RowsEvent ) error {
@@ -122,45 +120,41 @@ func (h *testEventHandler) String() string {
122
120
return "testEventHandler"
123
121
}
124
122
125
- func (h * testEventHandler ) OnPosSynced (header * replication.EventHeader , p mysql.Position , set mysql.GTIDSet , f bool ) error {
126
- return nil
127
- }
128
-
129
- func (s * canalTestSuite ) TestCanal (c * C ) {
123
+ func (s * canalTestSuite ) TestCanal () {
130
124
<- s .c .WaitDumpDone ()
131
125
132
126
for i := 1 ; i < 10 ; i ++ {
133
- s .execute (c , "INSERT INTO test.canal_test (name) VALUES (?)" , fmt .Sprintf ("%d" , i ))
127
+ s .execute ("INSERT INTO test.canal_test (name) VALUES (?)" , fmt .Sprintf ("%d" , i ))
134
128
}
135
- s .execute (c , "INSERT INTO test.canal_test (mi,umi) VALUES (?,?), (?,?), (?,?)" ,
129
+ s .execute ("INSERT INTO test.canal_test (mi,umi) VALUES (?,?), (?,?), (?,?)" ,
136
130
miA , umiA ,
137
131
miC , umiC ,
138
132
miB , umiB ,
139
133
)
140
- s .execute (c , "ALTER TABLE test.canal_test ADD `age` INT(5) NOT NULL AFTER `name`" )
141
- s .execute (c , "INSERT INTO test.canal_test (name,age) VALUES (?,?)" , "d" , "18" )
134
+ s .execute ("ALTER TABLE test.canal_test ADD `age` INT(5) NOT NULL AFTER `name`" )
135
+ s .execute ("INSERT INTO test.canal_test (name,age) VALUES (?,?)" , "d" , "18" )
142
136
143
137
err := s .c .CatchMasterPos (10 * time .Second )
144
- c . Assert ( err , IsNil )
138
+ require . NoError ( s . T (), err )
145
139
}
146
140
147
- func (s * canalTestSuite ) TestCanalFilter (c * C ) {
141
+ func (s * canalTestSuite ) TestCanalFilter () {
148
142
// included
149
143
sch , err := s .c .GetTable ("test" , "canal_test" )
150
- c . Assert ( err , IsNil )
151
- c . Assert ( sch , NotNil )
144
+ require . NoError ( s . T (), err )
145
+ require . NotNil ( s . T (), sch )
152
146
_ , err = s .c .GetTable ("not_exist_db" , "canal_test" )
153
- c . Assert ( errors . Trace ( err ), Not ( Equals ) , ErrExcludedTable )
147
+ require . NotErrorIs ( s . T ( ), err , ErrExcludedTable )
154
148
// excluded
155
149
sch , err = s .c .GetTable ("test" , "canal_test_inner" )
156
- c . Assert ( errors . Cause ( err ), Equals , ErrExcludedTable )
157
- c . Assert ( sch , IsNil )
150
+ require . ErrorIs ( s . T ( ), err , ErrExcludedTable )
151
+ require . Nil ( s . T (), sch )
158
152
sch , err = s .c .GetTable ("mysql" , "canal_test" )
159
- c . Assert ( errors . Cause ( err ), Equals , ErrExcludedTable )
160
- c . Assert ( sch , IsNil )
153
+ require . ErrorIs ( s . T ( ), err , ErrExcludedTable )
154
+ require . Nil ( s . T (), sch )
161
155
sch , err = s .c .GetTable ("not_exist_db" , "not_canal_test" )
162
- c . Assert ( errors . Cause ( err ), Equals , ErrExcludedTable )
163
- c . Assert ( sch , IsNil )
156
+ require . ErrorIs ( s . T ( ), err , ErrExcludedTable )
157
+ require . Nil ( s . T (), sch )
164
158
}
165
159
166
160
func TestCreateTableExp (t * testing.T ) {
@@ -170,22 +164,20 @@ func TestCreateTableExp(t *testing.T) {
170
164
"CREATE TABLE IF NOT EXISTS mydb.`mytable` (`id` int(10)) ENGINE=InnoDB" ,
171
165
"CREATE TABLE IF NOT EXISTS `mydb`.mytable (`id` int(10)) ENGINE=InnoDB" ,
172
166
}
173
- table := "mytable"
174
- db := "mydb"
167
+ expected := & node {
168
+ db : "mydb" ,
169
+ table : "mytable" ,
170
+ }
175
171
pr := parser .New ()
176
172
for _ , s := range cases {
177
173
stmts , _ , err := pr .Parse (s , "" , "" )
178
- if err != nil {
179
- t .Fatalf ("TestCreateTableExp:case %s failed\n " , s )
180
- }
174
+ require .NoError (t , err )
181
175
for _ , st := range stmts {
182
176
nodes := parseStmt (st )
183
177
if len (nodes ) == 0 {
184
178
continue
185
179
}
186
- if nodes [0 ].db != db || nodes [0 ].table != table {
187
- t .Fatalf ("TestCreateTableExp:case %s failed\n " , s )
188
- }
180
+ require .Equal (t , expected , nodes [0 ])
189
181
}
190
182
}
191
183
}
@@ -203,9 +195,7 @@ func TestAlterTableExp(t *testing.T) {
203
195
pr := parser .New ()
204
196
for _ , s := range cases {
205
197
stmts , _ , err := pr .Parse (s , "" , "" )
206
- if err != nil {
207
- t .Fatalf ("TestAlterTableExp:case %s failed\n " , s )
208
- }
198
+ require .NoError (t , err )
209
199
for _ , st := range stmts {
210
200
nodes := parseStmt (st )
211
201
if len (nodes ) == 0 {
@@ -237,9 +227,7 @@ func TestRenameTableExp(t *testing.T) {
237
227
pr := parser .New ()
238
228
for _ , s := range cases {
239
229
stmts , _ , err := pr .Parse (s , "" , "" )
240
- if err != nil {
241
- t .Fatalf ("TestRenameTableExp:case %s failed\n " , s )
242
- }
230
+ require .NoError (t , err )
243
231
for _ , st := range stmts {
244
232
nodes := parseStmt (st )
245
233
if len (nodes ) == 0 {
@@ -281,9 +269,7 @@ func TestDropTableExp(t *testing.T) {
281
269
pr := parser .New ()
282
270
for _ , s := range cases {
283
271
stmts , _ , err := pr .Parse (s , "" , "" )
284
- if err != nil {
285
- t .Fatalf ("TestDropTableExp:case %s failed\n " , s )
286
- }
272
+ require .NoError (t , err )
287
273
for _ , st := range stmts {
288
274
nodes := parseStmt (st )
289
275
if len (nodes ) == 0 {
@@ -325,9 +311,7 @@ func TestWithoutSchemeExp(t *testing.T) {
325
311
pr := parser .New ()
326
312
for _ , s := range cases {
327
313
stmts , _ , err := pr .Parse (string (s .Query ), "" , "" )
328
- if err != nil {
329
- t .Fatalf ("TestCreateTableExp:case %s failed\n " , s .Query )
330
- }
314
+ require .NoError (t , err )
331
315
for _ , st := range stmts {
332
316
nodes := parseStmt (st )
333
317
if len (nodes ) == 0 {
0 commit comments