Skip to content

Commit f14527e

Browse files
authored
Merge pull request #803 from lance6716/change-test-framekwork
change unit test framework to https://github.com/stretchr/testify
2 parents bd8645a + 17ecaaf commit f14527e

31 files changed

+1053
-1221
lines changed

.github/workflows/ci.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ jobs:
3737
- name: Checkout code
3838
uses: actions/checkout@v1
3939
- name: Run tests
40-
run: go test ./...
40+
run: |
41+
# separate test to avoid RESET MASTER conflict
42+
go test $(go list ./... | grep -v canal)
43+
go test $(go list ./... | grep canal)
4144
4245
golangci:
4346
name: golangci

canal/canal_test.go

+45-61
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,24 @@ import (
55
"testing"
66
"time"
77

8-
. "github.com/pingcap/check"
9-
"github.com/pingcap/errors"
108
"github.com/pingcap/tidb/parser"
119
"github.com/siddontang/go-log/log"
10+
"github.com/stretchr/testify/require"
11+
"github.com/stretchr/testify/suite"
1212

1313
"github.com/go-mysql-org/go-mysql/mysql"
1414
"github.com/go-mysql-org/go-mysql/replication"
1515
"github.com/go-mysql-org/go-mysql/test_util"
1616
)
1717

18-
func Test(t *testing.T) {
19-
TestingT(t)
20-
}
21-
2218
type canalTestSuite struct {
19+
suite.Suite
2320
c *Canal
2421
}
2522

26-
var _ = Suite(&canalTestSuite{})
23+
func TestCanalSuite(t *testing.T) {
24+
suite.Run(t, new(canalTestSuite))
25+
}
2726

2827
const (
2928
miA = 0
@@ -35,7 +34,7 @@ const (
3534
umiC = 16777215
3635
)
3736

38-
func (s *canalTestSuite) SetUpSuite(c *C) {
37+
func (s *canalTestSuite) SetupSuite() {
3938
cfg := NewDefaultConfig()
4039
cfg.Addr = fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
4140
cfg.User = "root"
@@ -55,8 +54,8 @@ func (s *canalTestSuite) SetUpSuite(c *C) {
5554

5655
var err error
5756
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")
6059
sql := `
6160
CREATE TABLE IF NOT EXISTS test.canal_test (
6261
id int AUTO_INCREMENT,
@@ -68,28 +67,28 @@ func (s *canalTestSuite) SetUpSuite(c *C) {
6867
)ENGINE=innodb;
6968
`
7069

71-
s.execute(c, sql)
70+
s.execute(sql)
7271

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 (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?)",
7574
"1", "a", miA, umiA,
7675
`\0\ndsfasdf`, "b", miC, umiC,
7776
"", "c", miB, umiB,
7877
)
7978

80-
s.execute(c, "SET GLOBAL binlog_format = 'ROW'")
79+
s.execute("SET GLOBAL binlog_format = 'ROW'")
8180

82-
s.c.SetEventHandler(&testEventHandler{c: c})
81+
s.c.SetEventHandler(&testEventHandler{})
8382
go func() {
8483
set, _ := mysql.ParseGTIDSet("mysql", "")
8584
err = s.c.StartFromGTID(set)
86-
c.Assert(err, IsNil)
85+
require.NoError(s.T(), err)
8786
}()
8887
}
8988

90-
func (s *canalTestSuite) TearDownSuite(c *C) {
89+
func (s *canalTestSuite) TearDownSuite() {
9190
// 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")
9392
time.Sleep(time.Second)
9493

9594
if s.c != nil {
@@ -98,15 +97,14 @@ func (s *canalTestSuite) TearDownSuite(c *C) {
9897
}
9998
}
10099

101-
func (s *canalTestSuite) execute(c *C, query string, args ...interface{}) *mysql.Result {
100+
func (s *canalTestSuite) execute(query string, args ...interface{}) *mysql.Result {
102101
r, err := s.c.Execute(query, args...)
103-
c.Assert(err, IsNil)
102+
require.NoError(s.T(), err)
104103
return r
105104
}
106105

107106
type testEventHandler struct {
108107
DummyEventHandler
109-
c *C
110108
}
111109

112110
func (h *testEventHandler) OnRow(e *RowsEvent) error {
@@ -122,45 +120,41 @@ func (h *testEventHandler) String() string {
122120
return "testEventHandler"
123121
}
124122

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() {
130124
<-s.c.WaitDumpDone()
131125

132126
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))
134128
}
135-
s.execute(c, "INSERT INTO test.canal_test (mi,umi) VALUES (?,?), (?,?), (?,?)",
129+
s.execute("INSERT INTO test.canal_test (mi,umi) VALUES (?,?), (?,?), (?,?)",
136130
miA, umiA,
137131
miC, umiC,
138132
miB, umiB,
139133
)
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")
142136

143137
err := s.c.CatchMasterPos(10 * time.Second)
144-
c.Assert(err, IsNil)
138+
require.NoError(s.T(), err)
145139
}
146140

147-
func (s *canalTestSuite) TestCanalFilter(c *C) {
141+
func (s *canalTestSuite) TestCanalFilter() {
148142
// included
149143
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)
152146
_, 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)
154148
// excluded
155149
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)
158152
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)
161155
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)
164158
}
165159

166160
func TestCreateTableExp(t *testing.T) {
@@ -170,22 +164,20 @@ func TestCreateTableExp(t *testing.T) {
170164
"CREATE TABLE IF NOT EXISTS mydb.`mytable` (`id` int(10)) ENGINE=InnoDB",
171165
"CREATE TABLE IF NOT EXISTS `mydb`.mytable (`id` int(10)) ENGINE=InnoDB",
172166
}
173-
table := "mytable"
174-
db := "mydb"
167+
expected := &node{
168+
db: "mydb",
169+
table: "mytable",
170+
}
175171
pr := parser.New()
176172
for _, s := range cases {
177173
stmts, _, err := pr.Parse(s, "", "")
178-
if err != nil {
179-
t.Fatalf("TestCreateTableExp:case %s failed\n", s)
180-
}
174+
require.NoError(t, err)
181175
for _, st := range stmts {
182176
nodes := parseStmt(st)
183177
if len(nodes) == 0 {
184178
continue
185179
}
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])
189181
}
190182
}
191183
}
@@ -203,9 +195,7 @@ func TestAlterTableExp(t *testing.T) {
203195
pr := parser.New()
204196
for _, s := range cases {
205197
stmts, _, err := pr.Parse(s, "", "")
206-
if err != nil {
207-
t.Fatalf("TestAlterTableExp:case %s failed\n", s)
208-
}
198+
require.NoError(t, err)
209199
for _, st := range stmts {
210200
nodes := parseStmt(st)
211201
if len(nodes) == 0 {
@@ -237,9 +227,7 @@ func TestRenameTableExp(t *testing.T) {
237227
pr := parser.New()
238228
for _, s := range cases {
239229
stmts, _, err := pr.Parse(s, "", "")
240-
if err != nil {
241-
t.Fatalf("TestRenameTableExp:case %s failed\n", s)
242-
}
230+
require.NoError(t, err)
243231
for _, st := range stmts {
244232
nodes := parseStmt(st)
245233
if len(nodes) == 0 {
@@ -281,9 +269,7 @@ func TestDropTableExp(t *testing.T) {
281269
pr := parser.New()
282270
for _, s := range cases {
283271
stmts, _, err := pr.Parse(s, "", "")
284-
if err != nil {
285-
t.Fatalf("TestDropTableExp:case %s failed\n", s)
286-
}
272+
require.NoError(t, err)
287273
for _, st := range stmts {
288274
nodes := parseStmt(st)
289275
if len(nodes) == 0 {
@@ -325,9 +311,7 @@ func TestWithoutSchemeExp(t *testing.T) {
325311
pr := parser.New()
326312
for _, s := range cases {
327313
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)
331315
for _, st := range stmts {
332316
nodes := parseStmt(st)
333317
if len(nodes) == 0 {

client/auth_test.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package client
22

33
import (
4-
"bytes"
54
"testing"
65

76
"github.com/go-mysql-org/go-mysql/mysql"
7+
"github.com/stretchr/testify/require"
88
)
99

1010
func TestConnGenAttributes(t *testing.T) {
@@ -26,17 +26,11 @@ func TestConnGenAttributes(t *testing.T) {
2626
// the order of the attributes map cannot be guaranteed so to test the content
2727
// of the attribute data we need to check its partial contents
2828

29-
if len(data) != 98 {
30-
t.Fatalf("unexpected data length, got %d", len(data))
31-
}
32-
if data[0] != 0x61 {
33-
t.Fatalf("unexpected length-encoded int, got %#x", data[0])
34-
}
29+
require.Len(t, data, 98)
30+
require.Equal(t, byte(0x61), data[0])
3531

3632
for k, v := range c.attributes {
3733
fixt := append(mysql.PutLengthEncodedString([]byte(k)), mysql.PutLengthEncodedString([]byte(v))...)
38-
if !bytes.Contains(data, fixt) {
39-
t.Fatalf("%s attribute not found", k)
40-
}
34+
require.Subset(t, data, fixt)
4135
}
4236
}

0 commit comments

Comments
 (0)