github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/loader/util_test.go (about) 1 // Copyright 2019 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package loader 15 16 import ( 17 "fmt" 18 "io" 19 "os" 20 "path" 21 "testing" 22 23 . "github.com/pingcap/check" 24 ) 25 26 func TestClient(t *testing.T) { 27 TestingT(t) 28 } 29 30 var _ = Suite(&testUtilSuite{}) 31 32 type testUtilSuite struct{} 33 34 func (t *testUtilSuite) TestSQLReplace(c *C) { 35 replaceTests := []struct { 36 in string 37 old, new string 38 out string 39 }{ 40 {"create database `xyz`", "xyz", "abc", "create database `abc`"}, 41 {"create database `xyz`", "crea", "abc", "create database `xyz`"}, 42 {"create database `xyz`", "create", "abc", "create database `xyz`"}, 43 {"create database `xyz`", "data", "abc", "create database `xyz`"}, 44 {"create database `xyz`", "database", "abc", "create database `xyz`"}, 45 {"create table `xyz`", "xyz", "abc", "create table `abc`"}, 46 {"create table `xyz`", "crea", "abc", "create table `xyz`"}, 47 {"create table `xyz`", "create", "abc", "create table `xyz`"}, 48 {"create table `xyz`", "tab", "abc", "create table `xyz`"}, 49 {"create table `xyz`", "table", "abc", "create table `xyz`"}, 50 {"insert into `xyz`", "xyz", "abc", "insert into `abc`"}, 51 {"insert into `xyz`", "ins", "abc", "insert into `xyz`"}, 52 {"insert into `xyz`", "insert", "abc", "insert into `xyz`"}, 53 {"insert into `xyz`", "in", "abc", "insert into `xyz`"}, 54 {"insert into `xyz`", "into", "abc", "insert into `xyz`"}, 55 {"INSERT INTO `xyz`", "xyz", "abc", "INSERT INTO `abc`"}, 56 } 57 58 for _, tt := range replaceTests { 59 c.Assert(SQLReplace(tt.in, tt.old, tt.new, false), Equals, tt.out) 60 } 61 62 c.Assert(SQLReplace("create table \"xyz\"", "xyz", "abc", true), 63 Equals, "create table \"abc\"") 64 } 65 66 func (t *testUtilSuite) TestShortSha1(c *C) { 67 c.Assert(shortSha1("/tmp/test_sha1_short_6"), Equals, "97b645") 68 } 69 70 func (t *testUtilSuite) TestGenerateSchemaCreateFile(c *C) { 71 dir := c.MkDir() 72 testCases := []struct { 73 schema string 74 createSQL string 75 }{ 76 { 77 "loader_test", 78 "CREATE DATABASE `loader_test`;\n", 79 }, { 80 "loader`test", 81 "CREATE DATABASE `loader``test`;\n", 82 }, 83 } 84 for _, testCase := range testCases { 85 err := generateSchemaCreateFile(dir, testCase.schema) 86 c.Assert(err, IsNil) 87 88 file, err := os.Open(path.Join(dir, fmt.Sprintf("%s-schema-create.sql", testCase.schema))) 89 c.Assert(err, IsNil) 90 91 data, err := io.ReadAll(file) 92 c.Assert(err, IsNil) 93 c.Assert(string(data), Equals, testCase.createSQL) 94 } 95 } 96 97 func (t *testUtilSuite) TestGetDBAndTableFromFilename(c *C) { 98 cases := []struct { 99 filename string 100 schema string 101 table string 102 errMsg string 103 }{ 104 {"db.tbl.sql", "db", "tbl", ""}, 105 {"db.tbl.0.sql", "db", "tbl", ""}, 106 {"db.sqltbl.sql", "db", "sqltbl", ""}, 107 {"db.sqltbl.0.sql", "db", "sqltbl", ""}, 108 {"sqldb.tbl.sql", "sqldb", "tbl", ""}, 109 {"sqldb.tbl.0.sql", "sqldb", "tbl", ""}, 110 {"db.tbl.sql0.sql", "db", "tbl", ""}, 111 {"db.tbl.0", "", "", ".*doesn't have a `.sql` suffix.*"}, 112 {"db.sql", "", "", ".*doesn't have correct `.` separator.*"}, 113 {"db.0.sql", "db", "0", ""}, // treat `0` as the table name. 114 } 115 116 for _, cs := range cases { 117 schema, table, err := getDBAndTableFromFilename(cs.filename) 118 if cs.errMsg != "" { 119 c.Assert(err, ErrorMatches, cs.errMsg) 120 } else { 121 c.Assert(err, IsNil) 122 c.Assert(schema, Equals, cs.schema) 123 c.Assert(table, Equals, cs.table) 124 } 125 } 126 }