This repository has been archived by the owner on Aug 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
unittest: add unit tests for dump.go and writer.go (#29)
Signed-off-by: zhongzc <zhongzc_arch@outlook.com>
- Loading branch information
Showing
2 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |