Skip to content

Commit 5aafa69

Browse files
authored
Merge pull request #563 from go-mysql-org/atercattus/restructured-dump-tests
restructured dump/ tests - split single dump_test.go to separate files
2 parents 2c302f7 + ed113dc commit 5aafa69

File tree

4 files changed

+156
-156
lines changed

4 files changed

+156
-156
lines changed
File renamed without changes.

dump/dump_test.go renamed to dump/parser_test.go

+2-156
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,16 @@
11
package dump
22

33
import (
4-
"bytes"
5-
"flag"
6-
"fmt"
7-
"io/ioutil"
8-
"os"
94
"strings"
10-
"testing"
115

126
. "github.com/pingcap/check"
13-
"github.com/siddontang/go-mysql/client"
147
"github.com/siddontang/go-mysql/mysql"
158
)
169

17-
// use docker mysql for test
18-
var host = flag.String("host", "127.0.0.1", "MySQL host")
19-
var port = flag.Int("port", 3306, "MySQL host")
20-
21-
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
22-
23-
func Test(t *testing.T) {
24-
TestingT(t)
25-
}
26-
27-
type schemaTestSuite struct {
28-
conn *client.Conn
29-
d *Dumper
30-
}
31-
32-
var _ = Suite(&schemaTestSuite{})
33-
34-
func (s *schemaTestSuite) SetUpSuite(c *C) {
35-
var err error
36-
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
37-
c.Assert(err, IsNil)
38-
39-
s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
40-
c.Assert(err, IsNil)
41-
c.Assert(s.d, NotNil)
42-
43-
s.d.SetCharset("utf8")
44-
s.d.SetErrOut(os.Stderr)
45-
46-
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test1")
47-
c.Assert(err, IsNil)
48-
49-
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test2")
50-
c.Assert(err, IsNil)
51-
52-
str := `CREATE TABLE IF NOT EXISTS test%d.t%d (
53-
id int AUTO_INCREMENT,
54-
name varchar(256),
55-
PRIMARY KEY(id)
56-
) ENGINE=INNODB`
57-
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
58-
c.Assert(err, IsNil)
59-
60-
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
61-
c.Assert(err, IsNil)
62-
63-
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
64-
c.Assert(err, IsNil)
65-
66-
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
67-
c.Assert(err, IsNil)
68-
69-
str = `INSERT INTO test%d.t%d (name) VALUES ("a"), ("b"), ("\\"), ("''")`
70-
71-
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
72-
c.Assert(err, IsNil)
73-
74-
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
75-
c.Assert(err, IsNil)
76-
77-
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
78-
c.Assert(err, IsNil)
79-
80-
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
81-
c.Assert(err, IsNil)
82-
}
83-
84-
func (s *schemaTestSuite) TearDownSuite(c *C) {
85-
if s.conn != nil {
86-
_, err := s.conn.Execute("DROP DATABASE IF EXISTS test1")
87-
c.Assert(err, IsNil)
88-
89-
_, err = s.conn.Execute("DROP DATABASE IF EXISTS test2")
90-
c.Assert(err, IsNil)
91-
92-
s.conn.Close()
93-
}
94-
}
95-
96-
func (s *schemaTestSuite) TestDump(c *C) {
97-
// Using mysql 5.7 can't work, error:
98-
// mysqldump: Error 1412: Table definition has changed,
99-
// please retry transaction when dumping table `test_replication` at row: 0
100-
// err := s.d.Dump(ioutil.Discard)
101-
// c.Assert(err, IsNil)
102-
103-
s.d.AddDatabases("test1", "test2")
104-
105-
s.d.AddIgnoreTables("test1", "t2")
106-
107-
err := s.d.Dump(ioutil.Discard)
108-
c.Assert(err, IsNil)
109-
110-
s.d.AddTables("test1", "t1")
111-
112-
err = s.d.Dump(ioutil.Discard)
113-
c.Assert(err, IsNil)
114-
}
115-
116-
type testParseHandler struct {
117-
gset mysql.GTIDSet
118-
}
119-
120-
func (h *testParseHandler) BinLog(name string, pos uint64) error {
121-
return nil
122-
}
123-
124-
func (h *testParseHandler) GtidSet(gtidsets string) (err error) {
125-
if h.gset != nil {
126-
err = h.gset.Update(gtidsets)
127-
} else {
128-
h.gset, err = mysql.ParseGTIDSet("mysql", gtidsets)
129-
}
130-
return err
131-
}
132-
133-
func (h *testParseHandler) Data(schema string, table string, values []string) error {
134-
return nil
135-
}
136-
137-
type GtidParseTest struct {
138-
gset mysql.GTIDSet
10+
type parserTestSuite struct {
13911
}
14012

141-
func (h *GtidParseTest) UpdateGtidSet(gtidStr string) (err error) {
142-
if h.gset != nil {
143-
err = h.gset.Update(gtidStr)
144-
} else {
145-
h.gset, err = mysql.ParseGTIDSet("mysql", gtidStr)
146-
}
147-
return err
148-
}
13+
var _ = Suite(&parserTestSuite{})
14914

15015
func (s *parserTestSuite) TestParseGtidExp(c *C) {
15116
// binlogExp := regexp.MustCompile("^CHANGE MASTER TO MASTER_LOG_FILE='(.+)', MASTER_LOG_POS=(\\d+);")
@@ -217,11 +82,6 @@ func (s *parserTestSuite) TestParseFindTable(c *C) {
21782
}
21883
}
21984

220-
type parserTestSuite struct {
221-
}
222-
223-
var _ = Suite(&parserTestSuite{})
224-
22585
func (s *parserTestSuite) TestUnescape(c *C) {
22686
tbl := []struct {
22787
escaped string
@@ -247,20 +107,6 @@ func (s *parserTestSuite) TestUnescape(c *C) {
247107
}
248108
}
249109

250-
func (s *schemaTestSuite) TestParse(c *C) {
251-
var buf bytes.Buffer
252-
253-
s.d.Reset()
254-
255-
s.d.AddDatabases("test1", "test2")
256-
257-
err := s.d.Dump(&buf)
258-
c.Assert(err, IsNil)
259-
260-
err = Parse(&buf, new(testParseHandler), true)
261-
c.Assert(err, IsNil)
262-
}
263-
264110
func (s *parserTestSuite) TestParseValue(c *C) {
265111
str := `'abc\\',''`
266112
values, err := parseValues(str)

dump/schema_test.go

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package dump
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
. "github.com/pingcap/check"
10+
"github.com/siddontang/go-mysql/client"
11+
)
12+
13+
type schemaTestSuite struct {
14+
conn *client.Conn
15+
d *Dumper
16+
}
17+
18+
var _ = Suite(&schemaTestSuite{})
19+
20+
func (s *schemaTestSuite) SetUpSuite(c *C) {
21+
var err error
22+
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
23+
c.Assert(err, IsNil)
24+
25+
s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
26+
c.Assert(err, IsNil)
27+
c.Assert(s.d, NotNil)
28+
29+
s.d.SetCharset("utf8")
30+
s.d.SetErrOut(os.Stderr)
31+
32+
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test1")
33+
c.Assert(err, IsNil)
34+
35+
_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test2")
36+
c.Assert(err, IsNil)
37+
38+
str := `CREATE TABLE IF NOT EXISTS test%d.t%d (
39+
id int AUTO_INCREMENT,
40+
name varchar(256),
41+
PRIMARY KEY(id)
42+
) ENGINE=INNODB`
43+
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
44+
c.Assert(err, IsNil)
45+
46+
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
47+
c.Assert(err, IsNil)
48+
49+
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
50+
c.Assert(err, IsNil)
51+
52+
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
53+
c.Assert(err, IsNil)
54+
55+
str = `INSERT INTO test%d.t%d (name) VALUES ("a"), ("b"), ("\\"), ("''")`
56+
57+
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
58+
c.Assert(err, IsNil)
59+
60+
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
61+
c.Assert(err, IsNil)
62+
63+
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
64+
c.Assert(err, IsNil)
65+
66+
_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
67+
c.Assert(err, IsNil)
68+
}
69+
70+
func (s *schemaTestSuite) TearDownSuite(c *C) {
71+
if s.conn != nil {
72+
_, err := s.conn.Execute("DROP DATABASE IF EXISTS test1")
73+
c.Assert(err, IsNil)
74+
75+
_, err = s.conn.Execute("DROP DATABASE IF EXISTS test2")
76+
c.Assert(err, IsNil)
77+
78+
s.conn.Close()
79+
}
80+
}
81+
82+
func (s *schemaTestSuite) TestDump(c *C) {
83+
// Using mysql 5.7 can't work, error:
84+
// mysqldump: Error 1412: Table definition has changed,
85+
// please retry transaction when dumping table `test_replication` at row: 0
86+
// err := s.d.Dump(ioutil.Discard)
87+
// c.Assert(err, IsNil)
88+
89+
s.d.AddDatabases("test1", "test2")
90+
91+
s.d.AddIgnoreTables("test1", "t2")
92+
93+
err := s.d.Dump(ioutil.Discard)
94+
c.Assert(err, IsNil)
95+
96+
s.d.AddTables("test1", "t1")
97+
98+
err = s.d.Dump(ioutil.Discard)
99+
c.Assert(err, IsNil)
100+
}
101+
102+
func (s *schemaTestSuite) TestParse(c *C) {
103+
var buf bytes.Buffer
104+
105+
s.d.Reset()
106+
107+
s.d.AddDatabases("test1", "test2")
108+
109+
err := s.d.Dump(&buf)
110+
c.Assert(err, IsNil)
111+
112+
err = Parse(&buf, new(testParseHandler), true)
113+
c.Assert(err, IsNil)
114+
}

dump/setup_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package dump
2+
3+
import (
4+
"flag"
5+
"testing"
6+
7+
. "github.com/pingcap/check"
8+
"github.com/siddontang/go-mysql/mysql"
9+
)
10+
11+
// use docker mysql for test
12+
var host = flag.String("host", "127.0.0.1", "MySQL host")
13+
var port = flag.Int("port", 3306, "MySQL host")
14+
15+
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
16+
17+
func Test(t *testing.T) {
18+
TestingT(t)
19+
}
20+
21+
type testParseHandler struct {
22+
gset mysql.GTIDSet
23+
}
24+
25+
func (h *testParseHandler) BinLog(name string, pos uint64) error {
26+
return nil
27+
}
28+
29+
func (h *testParseHandler) GtidSet(gtidsets string) (err error) {
30+
if h.gset != nil {
31+
err = h.gset.Update(gtidsets)
32+
} else {
33+
h.gset, err = mysql.ParseGTIDSet("mysql", gtidsets)
34+
}
35+
return err
36+
}
37+
38+
func (h *testParseHandler) Data(schema string, table string, values []string) error {
39+
return nil
40+
}

0 commit comments

Comments
 (0)