Skip to content

restructured dump/ tests - split single dump_test.go to separate files #563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
158 changes: 2 additions & 156 deletions dump/dump_test.go → dump/parser_test.go
Original file line number Diff line number Diff line change
@@ -1,151 +1,16 @@
package dump

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

. "github.com/pingcap/check"
"github.com/siddontang/go-mysql/client"
"github.com/siddontang/go-mysql/mysql"
)

// use docker mysql for test
var host = flag.String("host", "127.0.0.1", "MySQL host")
var port = flag.Int("port", 3306, "MySQL host")

var execution = flag.String("exec", "mysqldump", "mysqldump execution path")

func Test(t *testing.T) {
TestingT(t)
}

type schemaTestSuite struct {
conn *client.Conn
d *Dumper
}

var _ = Suite(&schemaTestSuite{})

func (s *schemaTestSuite) SetUpSuite(c *C) {
var err error
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
c.Assert(err, IsNil)

s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
c.Assert(err, IsNil)
c.Assert(s.d, NotNil)

s.d.SetCharset("utf8")
s.d.SetErrOut(os.Stderr)

_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test1")
c.Assert(err, IsNil)

_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test2")
c.Assert(err, IsNil)

str := `CREATE TABLE IF NOT EXISTS test%d.t%d (
id int AUTO_INCREMENT,
name varchar(256),
PRIMARY KEY(id)
) ENGINE=INNODB`
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)

str = `INSERT INTO test%d.t%d (name) VALUES ("a"), ("b"), ("\\"), ("''")`

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)
}

func (s *schemaTestSuite) TearDownSuite(c *C) {
if s.conn != nil {
_, err := s.conn.Execute("DROP DATABASE IF EXISTS test1")
c.Assert(err, IsNil)

_, err = s.conn.Execute("DROP DATABASE IF EXISTS test2")
c.Assert(err, IsNil)

s.conn.Close()
}
}

func (s *schemaTestSuite) TestDump(c *C) {
// Using mysql 5.7 can't work, error:
// mysqldump: Error 1412: Table definition has changed,
// please retry transaction when dumping table `test_replication` at row: 0
// err := s.d.Dump(ioutil.Discard)
// c.Assert(err, IsNil)

s.d.AddDatabases("test1", "test2")

s.d.AddIgnoreTables("test1", "t2")

err := s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)

s.d.AddTables("test1", "t1")

err = s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)
}

type testParseHandler struct {
gset mysql.GTIDSet
}

func (h *testParseHandler) BinLog(name string, pos uint64) error {
return nil
}

func (h *testParseHandler) GtidSet(gtidsets string) (err error) {
if h.gset != nil {
err = h.gset.Update(gtidsets)
} else {
h.gset, err = mysql.ParseGTIDSet("mysql", gtidsets)
}
return err
}

func (h *testParseHandler) Data(schema string, table string, values []string) error {
return nil
}

type GtidParseTest struct {
gset mysql.GTIDSet
type parserTestSuite struct {
}

func (h *GtidParseTest) UpdateGtidSet(gtidStr string) (err error) {
if h.gset != nil {
err = h.gset.Update(gtidStr)
} else {
h.gset, err = mysql.ParseGTIDSet("mysql", gtidStr)
}
return err
}
var _ = Suite(&parserTestSuite{})

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

type parserTestSuite struct {
}

var _ = Suite(&parserTestSuite{})

func (s *parserTestSuite) TestUnescape(c *C) {
tbl := []struct {
escaped string
Expand All @@ -247,20 +107,6 @@ func (s *parserTestSuite) TestUnescape(c *C) {
}
}

func (s *schemaTestSuite) TestParse(c *C) {
var buf bytes.Buffer

s.d.Reset()

s.d.AddDatabases("test1", "test2")

err := s.d.Dump(&buf)
c.Assert(err, IsNil)

err = Parse(&buf, new(testParseHandler), true)
c.Assert(err, IsNil)
}

func (s *parserTestSuite) TestParseValue(c *C) {
str := `'abc\\',''`
values, err := parseValues(str)
Expand Down
114 changes: 114 additions & 0 deletions dump/schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package dump

import (
"bytes"
"fmt"
"io/ioutil"
"os"

. "github.com/pingcap/check"
"github.com/siddontang/go-mysql/client"
)

type schemaTestSuite struct {
conn *client.Conn
d *Dumper
}

var _ = Suite(&schemaTestSuite{})

func (s *schemaTestSuite) SetUpSuite(c *C) {
var err error
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
c.Assert(err, IsNil)

s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
c.Assert(err, IsNil)
c.Assert(s.d, NotNil)

s.d.SetCharset("utf8")
s.d.SetErrOut(os.Stderr)

_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test1")
c.Assert(err, IsNil)

_, err = s.conn.Execute("CREATE DATABASE IF NOT EXISTS test2")
c.Assert(err, IsNil)

str := `CREATE TABLE IF NOT EXISTS test%d.t%d (
id int AUTO_INCREMENT,
name varchar(256),
PRIMARY KEY(id)
) ENGINE=INNODB`
_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)

str = `INSERT INTO test%d.t%d (name) VALUES ("a"), ("b"), ("\\"), ("''")`

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 1))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 1, 2))
c.Assert(err, IsNil)

_, err = s.conn.Execute(fmt.Sprintf(str, 2, 2))
c.Assert(err, IsNil)
}

func (s *schemaTestSuite) TearDownSuite(c *C) {
if s.conn != nil {
_, err := s.conn.Execute("DROP DATABASE IF EXISTS test1")
c.Assert(err, IsNil)

_, err = s.conn.Execute("DROP DATABASE IF EXISTS test2")
c.Assert(err, IsNil)

s.conn.Close()
}
}

func (s *schemaTestSuite) TestDump(c *C) {
// Using mysql 5.7 can't work, error:
// mysqldump: Error 1412: Table definition has changed,
// please retry transaction when dumping table `test_replication` at row: 0
// err := s.d.Dump(ioutil.Discard)
// c.Assert(err, IsNil)

s.d.AddDatabases("test1", "test2")

s.d.AddIgnoreTables("test1", "t2")

err := s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)

s.d.AddTables("test1", "t1")

err = s.d.Dump(ioutil.Discard)
c.Assert(err, IsNil)
}

func (s *schemaTestSuite) TestParse(c *C) {
var buf bytes.Buffer

s.d.Reset()

s.d.AddDatabases("test1", "test2")

err := s.d.Dump(&buf)
c.Assert(err, IsNil)

err = Parse(&buf, new(testParseHandler), true)
c.Assert(err, IsNil)
}
40 changes: 40 additions & 0 deletions dump/setup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dump

import (
"flag"
"testing"

. "github.com/pingcap/check"
"github.com/siddontang/go-mysql/mysql"
)

// use docker mysql for test
var host = flag.String("host", "127.0.0.1", "MySQL host")
var port = flag.Int("port", 3306, "MySQL host")

var execution = flag.String("exec", "mysqldump", "mysqldump execution path")

func Test(t *testing.T) {
TestingT(t)
}

type testParseHandler struct {
gset mysql.GTIDSet
}

func (h *testParseHandler) BinLog(name string, pos uint64) error {
return nil
}

func (h *testParseHandler) GtidSet(gtidsets string) (err error) {
if h.gset != nil {
err = h.gset.Update(gtidsets)
} else {
h.gset, err = mysql.ParseGTIDSet("mysql", gtidsets)
}
return err
}

func (h *testParseHandler) Data(schema string, table string, values []string) error {
return nil
}