Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
unittest: add unit tests for dump.go and writer.go (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: zhongzc <zhongzc_arch@outlook.com>
  • Loading branch information
zhongzc authored Feb 29, 2020
1 parent 7104b68 commit 1e277e6
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 3 deletions.
39 changes: 36 additions & 3 deletions v4/export/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ func (m *mockWriter) WriteTableData(ctx context.Context, ir TableDataIR) error {
return nil
}

func (s *testDumpSuite) TestDumpDatabase(c *C) {
mockConfig := DefaultConfig()
mockConfig.SortByPk = false
mockConfig.Database = "test"
mockConfig.Tables = map[string][]tableName{"test": {"t"}}
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)

showCreateDatabase := "CREATE DATABASE `test`"
rows := mock.NewRows([]string{"Database", "Create Database"}).AddRow("test", showCreateDatabase)
mock.ExpectQuery("SHOW CREATE DATABASE test").WillReturnRows(rows)
showCreateTableResult := "CREATE TABLE t (a INT)"
rows = mock.NewRows([]string{"Table", "Create Table"}).AddRow("t", showCreateTableResult)
mock.ExpectQuery("SHOW CREATE TABLE test.t").WillReturnRows(rows)
rows = mock.NewRows([]string{"a"}).AddRow(1)
mock.ExpectQuery("SELECT (.) FROM test.t LIMIT 1").WillReturnRows(rows)
rows = mock.NewRows([]string{"a"}).AddRow(1).AddRow(2)
mock.ExpectQuery("SELECT (.) FROM test.t").WillReturnRows(rows)

mockWriter := newMockWriter()
err = dumpDatabases(context.Background(), mockConfig, db, mockWriter)
c.Assert(err, IsNil)

c.Assert(len(mockWriter.databaseMeta), Equals, 1)
c.Assert(mockWriter.databaseMeta["test"], Equals, "CREATE DATABASE `test`")
c.Assert(mockWriter.tableMeta["test.t"], Equals, showCreateTableResult)
c.Assert(mock.ExpectationsWereMet(), IsNil)
}

func (s *testDumpSuite) TestDumpTable(c *C) {
mockConfig := DefaultConfig()
mockConfig.SortByPk = false
Expand All @@ -50,21 +79,25 @@ func (s *testDumpSuite) TestDumpTable(c *C) {
showCreateTableResult := "CREATE TABLE t (a INT)"
rows := mock.NewRows([]string{"Table", "Create Table"}).AddRow("t", showCreateTableResult)
mock.ExpectQuery("SHOW CREATE TABLE test.t").WillReturnRows(rows)
rows = mock.NewRows([]string{"id"}).AddRow(1)
rows = mock.NewRows([]string{"a"}).AddRow(1)
mock.ExpectQuery("SELECT (.) FROM test.t LIMIT 1").WillReturnRows(rows)
rows = mock.NewRows([]string{"id"}).AddRow(1).AddRow(2)
rows = mock.NewRows([]string{"a"}).AddRow(1).AddRow(2)
mock.ExpectQuery("SELECT (.) FROM test.t").WillReturnRows(rows)

mockWriter := newMockWriter()
err = dumpTable(context.Background(), mockConfig, db, "test", "t", mockWriter)
c.Assert(err, IsNil)

c.Assert(len(mockWriter.databaseMeta), Equals, 0)
c.Assert(mockWriter.tableMeta["test.t"], Equals, showCreateTableResult)
c.Assert(len(mockWriter.tableData), Equals, 1)
tbDataRes := mockWriter.tableData[0]
c.Assert(tbDataRes.DatabaseName(), Equals, "test")
c.Assert(tbDataRes.TableName(), Equals, "t")
c.Assert(tbDataRes.ColumnCount(), Equals, uint(1))
c.Assert(tbDataRes.SpecialComments().HasNext(), Equals, false)
c.Assert(tbDataRes.Rows().HasNext(), Equals, true)
receiver := newSimpleRowReceiver(1)
c.Assert(tbDataRes.Rows().Next(receiver), IsNil)
c.Assert(receiver.data[0], Equals, "2")
c.Assert(mock.ExpectationsWereMet(), IsNil)
}
152 changes: 152 additions & 0 deletions v4/export/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package export

import (
"context"
"database/sql/driver"
. "github.com/pingcap/check"
"io/ioutil"
"os"
"path"
)

var _ = Suite(&testWriterSuite{})

type testWriterSuite struct{}

func (s *testDumpSuite) TestWriteDatabaseMeta(c *C) {
dir, err := ioutil.TempDir("", "dumpling")
c.Assert(err, IsNil)
defer os.RemoveAll(dir)

config := DefaultConfig()
config.OutputDirPath = dir
ctx := context.Background()

writer, err := NewSimpleWriter(config)
c.Assert(err, IsNil)
err = writer.WriteDatabaseMeta(ctx, "test", "CREATE DATABASE `test`")
c.Assert(err, IsNil)
p := path.Join(dir, "test-schema-create.sql")
_, err = os.Stat(p)
c.Assert(err, IsNil)
bytes, err := ioutil.ReadFile(p)
c.Assert(err, IsNil)
c.Assert(string(bytes), Equals, "CREATE DATABASE `test`;\n")
}

func (s *testDumpSuite) TestWriteTableMeta(c *C) {
dir, err := ioutil.TempDir("", "dumpling")
c.Assert(err, IsNil)
defer os.RemoveAll(dir)

config := DefaultConfig()
config.OutputDirPath = dir
ctx := context.Background()

writer, err := NewSimpleWriter(config)
c.Assert(err, IsNil)
err = writer.WriteTableMeta(ctx, "test", "t", "CREATE TABLE t (a INT)")
c.Assert(err, IsNil)
p := path.Join(dir, "test.t-schema.sql")
_, err = os.Stat(p)
c.Assert(err, IsNil)
bytes, err := ioutil.ReadFile(p)
c.Assert(err, IsNil)
c.Assert(string(bytes), Equals, "CREATE TABLE t (a INT);\n")
}

func (s *testDumpSuite) TestWriteTableData(c *C) {
dir, err := ioutil.TempDir("", "dumpling")
c.Assert(err, IsNil)
defer os.RemoveAll(dir)

config := DefaultConfig()
config.OutputDirPath = dir
ctx := context.Background()

writer, err := NewSimpleWriter(config)
c.Assert(err, IsNil)

data := [][]driver.Value{
{"1", "male", "bob@mail.com", "020-1234", nil},
{"2", "female", "sarah@mail.com", "020-1253", "healthy"},
{"3", "male", "john@mail.com", "020-1256", "healthy"},
{"4", "female", "sarah@mail.com", "020-1235", "healthy"},
}
colTypes := []string{"INT", "SET", "VARCHAR", "VARCHAR", "TEXT"}
specCmts := []string{
"/*!40101 SET NAMES binary*/;",
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;",
}
tableIR := newMockTableIR("test", "employee", data, specCmts, colTypes)
err = writer.WriteTableData(ctx, tableIR)
c.Assert(err, IsNil)

p := path.Join(dir, "test.employee.sql")
_, err = os.Stat(p)
c.Assert(err, IsNil)
bytes, err := ioutil.ReadFile(p)
c.Assert(err, IsNil)

expected := "/*!40101 SET NAMES binary*/;\n" +
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;\n" +
"INSERT INTO `employee` VALUES\n" +
"(1, 'male', 'bob@mail.com', '020-1234', NULL),\n" +
"(2, 'female', 'sarah@mail.com', '020-1253', 'healthy'),\n" +
"(3, 'male', 'john@mail.com', '020-1256', 'healthy'),\n" +
"(4, 'female', 'sarah@mail.com', '020-1235', 'healthy');\n"
c.Assert(string(bytes), Equals, expected)
}

func (s *testDumpSuite) TestWriteTableDataWithFileSize(c *C) {
dir, err := ioutil.TempDir("", "dumpling")
c.Assert(err, IsNil)
defer os.RemoveAll(dir)

config := DefaultConfig()
config.OutputDirPath = dir
config.FileSize = 50
ctx := context.Background()

writer, err := NewSimpleWriter(config)
c.Assert(err, IsNil)

data := [][]driver.Value{
{"1", "male", "bob@mail.com", "020-1234", nil},
{"2", "female", "sarah@mail.com", "020-1253", "healthy"},
{"3", "male", "john@mail.com", "020-1256", "healthy"},
{"4", "female", "sarah@mail.com", "020-1235", "healthy"},
}
colTypes := []string{"INT", "SET", "VARCHAR", "VARCHAR", "TEXT"}
specCmts := []string{
"/*!40101 SET NAMES binary*/;",
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;",
}
tableIR := newMockTableIR("test", "employee", data, specCmts, colTypes)
err = writer.WriteTableData(ctx, tableIR)
c.Assert(err, IsNil)

cases := map[string]string {
"test.employee.0.sql":
"/*!40101 SET NAMES binary*/;\n" +
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;\n" +
"INSERT INTO `employee` VALUES\n" +
"(1, 'male', 'bob@mail.com', '020-1234', NULL),\n" +
"(2, 'female', 'sarah@mail.com', '020-1253', 'healthy');\n",
"test.employee.1.sql":
"/*!40101 SET NAMES binary*/;\n" +
"/*!40014 SET FOREIGN_KEY_CHECKS=0*/;\n" +
"INSERT INTO `employee` VALUES\n" +
"(3, 'male', 'john@mail.com', '020-1256', 'healthy'),\n" +
"(4, 'female', 'sarah@mail.com', '020-1235', 'healthy');\n",
}

for p, expected := range cases {
p := path.Join(dir, p)
_, err = os.Stat(p)
c.Assert(err, IsNil)
bytes, err := ioutil.ReadFile(p)
c.Assert(err, IsNil)
c.Assert(string(bytes), Equals, expected)
}
}

0 comments on commit 1e277e6

Please sign in to comment.