Skip to content

Commit

Permalink
fix error test
Browse files Browse the repository at this point in the history
  • Loading branch information
joechenrh committed Jan 13, 2025
1 parent a8f9aa0 commit b50a7c8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 55 deletions.
24 changes: 12 additions & 12 deletions sync_diff_inspector/report/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"github.com/BurntSushi/toml"
"github.com/DATA-DOG/go-sqlmock"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
"github.com/pingcap/tiflow/sync_diff_inspector/chunk"
"github.com/pingcap/tiflow/sync_diff_inspector/config"
"github.com/pingcap/tiflow/sync_diff_inspector/source/common"
"github.com/pingcap/tiflow/sync_diff_inspector/utils"
"github.com/stretchr/testify/require"
)

Expand All @@ -45,10 +45,10 @@ func TestReport(t *testing.T) {

report := NewReport(task)
createTableSQL1 := "create table `test`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo1, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo1, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)
createTableSQL2 := "create table `atest`.`atbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo2, err := dbutiltest.GetTableInfoBySQL(createTableSQL2, parser.New())
tableInfo2, err := utils.GetTableInfoBySQL(createTableSQL2, parser.New())
require.NoError(t, err)

tableDiffs := []*common.TableDiff{
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestCalculateTotal(t *testing.T) {

report := NewReport(task)
createTableSQL := "create table `test`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

tableDiffs := []*common.TableDiff{
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestCalculateTotal(t *testing.T) {
func TestPrint(t *testing.T) {
report := NewReport(task)
createTableSQL := "create table `test`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

tableDiffs := []*common.TableDiff{
Expand Down Expand Up @@ -275,13 +275,13 @@ func TestPrint(t *testing.T) {
func TestGetSnapshot(t *testing.T) {
report := NewReport(task)
createTableSQL1 := "create table `test`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo1, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo1, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)
createTableSQL2 := "create table `atest`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo2, err := dbutiltest.GetTableInfoBySQL(createTableSQL2, parser.New())
tableInfo2, err := utils.GetTableInfoBySQL(createTableSQL2, parser.New())
require.NoError(t, err)
createTableSQL3 := "create table `xtest`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo3, err := dbutiltest.GetTableInfoBySQL(createTableSQL3, parser.New())
tableInfo3, err := utils.GetTableInfoBySQL(createTableSQL3, parser.New())
require.NoError(t, err)

tableDiffs := []*common.TableDiff{
Expand Down Expand Up @@ -392,16 +392,16 @@ func TestCommitSummary(t *testing.T) {
outputDir := "./"
report := NewReport(&config.TaskConfig{OutputDir: outputDir, FixDir: task.FixDir})
createTableSQL1 := "create table `test`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo1, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo1, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)
createTableSQL2 := "create table `atest`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo2, err := dbutiltest.GetTableInfoBySQL(createTableSQL2, parser.New())
tableInfo2, err := utils.GetTableInfoBySQL(createTableSQL2, parser.New())
require.NoError(t, err)
createTableSQL3 := "create table `xtest`.`tbl`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo3, err := dbutiltest.GetTableInfoBySQL(createTableSQL3, parser.New())
tableInfo3, err := utils.GetTableInfoBySQL(createTableSQL3, parser.New())
require.NoError(t, err)
createTableSQL4 := "create table `xtest`.`tb1`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo4, err := dbutiltest.GetTableInfoBySQL(createTableSQL4, parser.New())
tableInfo4, err := utils.GetTableInfoBySQL(createTableSQL4, parser.New())
require.NoError(t, err)
tableDiffs := []*common.TableDiff{
{
Expand Down
3 changes: 1 addition & 2 deletions sync_diff_inspector/source/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import (

"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/util/dbutil"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
"github.com/pingcap/tiflow/sync_diff_inspector/utils"
"github.com/stretchr/testify/require"
)

func TestRowData(t *testing.T) {
createTableSQL := "create table test.test(id int(24), name varchar(24), age int(24), primary key(id, name));"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

_, orderKeyCols := dbutil.SelectUniqueOrderKey(tableInfo)
Expand Down
5 changes: 2 additions & 3 deletions sync_diff_inspector/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
_ "github.com/go-sql-driver/mysql"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/util/dbutil"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
filter "github.com/pingcap/tidb/pkg/util/table-filter"
router "github.com/pingcap/tidb/pkg/util/table-router"
"github.com/pingcap/tiflow/sync_diff_inspector/chunk"
Expand Down Expand Up @@ -293,7 +292,7 @@ func TestFallbackToRandomIfRangeIsSet(t *testing.T) {
"`c` char(120) NOT NULL DEFAULT '', " +
"PRIMARY KEY (`id`), KEY `k_1` (`k`))"

tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)

table1 := &common.TableDiff{
Expand Down Expand Up @@ -625,7 +624,7 @@ func TestTiDBRouter(t *testing.T) {
func prepareTiDBTables(t *testing.T, tableCases []*tableCaseType) []*common.TableDiff {
tableDiffs := make([]*common.TableDiff, 0, len(tableCases))
for n, tableCase := range tableCases {
tableInfo, err := dbutiltest.GetTableInfoBySQL(tableCase.createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(tableCase.createTableSQL, parser.New())
require.NoError(t, err)
tableDiffs = append(tableDiffs, &common.TableDiff{
Schema: "source_test",
Expand Down
8 changes: 4 additions & 4 deletions sync_diff_inspector/splitter/index_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"testing"

"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
"github.com/pingcap/tiflow/sync_diff_inspector/utils"
"github.com/stretchr/testify/require"
)

Expand All @@ -30,7 +30,7 @@ func TestIndexFieldsSimple(t *testing.T) {
"`c` char(120) NOT NULL DEFAULT '', " +
"PRIMARY KEY (`id`), KEY `k_1` (`k`))"

tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)

fields, err := indexFieldsFromConfigString("k", tableInfo)
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestIndexFieldsComposite(t *testing.T) {
"KEY `k_1` (`k`)," +
"UNIQUE INDEX `c_1` (`c`))"

tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)

fields, err := indexFieldsFromConfigString("id, k", tableInfo)
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestIndexFieldsEmpty(t *testing.T) {
"`c` char(120) NOT NULL DEFAULT '', " +
"PRIMARY KEY (`id`), KEY `k_1` (`k`))"

tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL1, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL1, parser.New())
require.NoError(t, err)

fields, err := indexFieldsFromConfigString("", tableInfo)
Expand Down
15 changes: 7 additions & 8 deletions sync_diff_inspector/splitter/splitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/pingcap/tidb/pkg/parser"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
"github.com/pingcap/tiflow/sync_diff_inspector/chunk"
"github.com/pingcap/tiflow/sync_diff_inspector/source/common"
"github.com/pingcap/tiflow/sync_diff_inspector/utils"
Expand Down Expand Up @@ -142,7 +141,7 @@ func TestSplitRangeByRandom(t *testing.T) {
}

for _, testCase := range testCases {
tableInfo, err := dbutiltest.GetTableInfoBySQL(testCase.createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(testCase.createTableSQL, parser.New())
require.NoError(t, err)

splitCols, err := GetSplitFields(tableInfo, nil)
Expand Down Expand Up @@ -322,7 +321,7 @@ func TestRandomSpliter(t *testing.T) {
}

for _, testCase := range testCases {
tableInfo, err := dbutiltest.GetTableInfoBySQL(testCase.createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(testCase.createTableSQL, parser.New())
require.NoError(t, err)

info, needUnifiedTimeStamp := utils.ResetColumns(tableInfo, testCase.IgnoreColumns)
Expand Down Expand Up @@ -357,7 +356,7 @@ func TestRandomSpliter(t *testing.T) {

// Test Checkpoint
stopJ := 3
tableInfo, err := dbutiltest.GetTableInfoBySQL(testCases[0].createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(testCases[0].createTableSQL, parser.New())
require.NoError(t, err)

tableDiff := &common.TableDiff{
Expand Down Expand Up @@ -431,7 +430,7 @@ func TestBucketSpliter(t *testing.T) {
require.NoError(t, err)

createTableSQL := "create table `test`.`test`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

testCases := []struct {
Expand Down Expand Up @@ -743,7 +742,7 @@ func TestLimitSpliter(t *testing.T) {
ctx := context.Background()

createTableSQL := "create table `test`.`test`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

testCases := []struct {
Expand Down Expand Up @@ -884,7 +883,7 @@ func TestChunkSize(t *testing.T) {
require.NoError(t, err)

createTableSQL := "create table `test`.`test`(`a` int, `b` varchar(10), `c` float, `d` datetime, primary key(`a`, `b`))"
tableInfo, err := dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err := utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

tableDiff := &common.TableDiff{
Expand Down Expand Up @@ -925,7 +924,7 @@ func TestChunkSize(t *testing.T) {
require.Equal(t, randomIter.chunkSize, int64(100000))

createTableSQL = "create table `test`.`test`(`a` int, `b` varchar(10), `c` float, `d` datetime)"
tableInfo, err = dbutiltest.GetTableInfoBySQL(createTableSQL, parser.New())
tableInfo, err = utils.GetTableInfoBySQL(createTableSQL, parser.New())
require.NoError(t, err)

tableDiffNoIndex := &common.TableDiff{
Expand Down
42 changes: 40 additions & 2 deletions sync_diff_inspector/utils/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/collate"
"github.com/pingcap/tidb/pkg/util/dbutil"
"github.com/pingcap/tidb/pkg/util/dbutil/dbutiltest"
"github.com/pingcap/tidb/pkg/util/mock"
)

Expand Down Expand Up @@ -191,5 +190,44 @@ func GetTableInfo(
if err != nil {
return nil, errors.Trace(err)
}
return dbutiltest.GetTableInfoBySQL(createTableSQL, parser2)
return GetTableInfoBySQL(createTableSQL, parser2)
}

// GetTableInfoBySQL gets the table info from SQL.
func GetTableInfoBySQL(createTableSQL string, parser2 *parser.Parser) (table *model.TableInfo, err error) {
stmt, err := parser2.ParseOneStmt(createTableSQL, "", "")
if err != nil {
return nil, errors.Trace(err)
}

s, ok := stmt.(*ast.CreateTableStmt)
if ok {
table, err := ddl.BuildTableInfoWithStmt(metabuild.NewContext(), s, mysql.DefaultCharset, "", nil)
if err != nil {
return nil, errors.Trace(err)
}

// put primary key in indices
if table.PKIsHandle {
pkIndex := &model.IndexInfo{
Name: pmodel.NewCIStr("PRIMARY"),
Primary: true,
State: model.StatePublic,
Unique: true,
Tp: pmodel.IndexTypeBtree,
Columns: []*model.IndexColumn{
{
Name: table.GetPkName(),
Length: types.UnspecifiedLength,
},
},
}

table.Indices = append(table.Indices, pkIndex)
}

return table, nil
}

return nil, errors.Errorf("get table info from sql %s failed", createTableSQL)
}
Loading

0 comments on commit b50a7c8

Please sign in to comment.