Skip to content

Commit

Permalink
Refactor testTableInfo function
Browse files Browse the repository at this point in the history
  • Loading branch information
clovis1122 committed Nov 23, 2021
1 parent 71e64bf commit e47c1f3
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 36 deletions.
3 changes: 2 additions & 1 deletion ddl/column_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (s *testColumnChangeSuite) TestColumnChange(c *C) {
c.Assert(err, IsNil)
}()
// create table t (c1 int, c2 int);
tblInfo := testTableInfo(c, d, "t", 2)
tblInfo, err := testTableInfo(d, "t", 2)
c.Assert(err, IsNil)
ctx := testNewContext(d)
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
Expand Down
15 changes: 10 additions & 5 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ func (s *testColumnSuite) TestColumnBasic(c *C) {
c.Assert(err, IsNil)
}()

tblInfo := testTableInfo(c, d, "t1", 3)
tblInfo, err := testTableInfo(d, "t1", 3)
c.Assert(err, IsNil)
ctx := testNewContext(d)

testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
Expand Down Expand Up @@ -843,7 +844,8 @@ func (s *testColumnSuite) TestAddColumn(c *C) {
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
ctx := testNewContext(d)

err = ctx.NewTxn(context.Background())
Expand Down Expand Up @@ -931,7 +933,8 @@ func (s *testColumnSuite) TestAddColumns(c *C) {
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
ctx := testNewContext(d)

err = ctx.NewTxn(context.Background())
Expand Down Expand Up @@ -1016,7 +1019,8 @@ func (s *testColumnSuite) TestDropColumn(c *C) {
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t2", 4)
tblInfo, err := testTableInfo(d, "t2", 4)
c.Assert(err, IsNil)
ctx := testNewContext(d)

err = ctx.NewTxn(context.Background())
Expand Down Expand Up @@ -1092,7 +1096,8 @@ func (s *testColumnSuite) TestDropColumns(c *C) {
WithLease(testLease),
)
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t2", 4)
tblInfo, err := testTableInfo(d, "t2", 4)
c.Assert(err, IsNil)
ctx := testNewContext(d)

err = ctx.NewTxn(context.Background())
Expand Down
30 changes: 20 additions & 10 deletions ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ func (s *testDDLSuite) TestTableError(c *C) {
job := doDDLJobErr(c, dbInfo.ID, -1, model.ActionDropTable, nil, ctx, d)

// Table ID or schema ID is wrong, so getting table is failed.
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
err = kv.RunInNewTxn(context.Background(), store, false, func(ctx context.Context, txn kv.Transaction) error {
job.SchemaID = -1
Expand Down Expand Up @@ -374,7 +375,8 @@ func (s *testDDLSuite) TestForeignKeyError(c *C) {

dbInfo, err := testSchemaInfo(d, "test_ddl")
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d, dbInfo)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
doDDLJobErr(c, dbInfo.ID, tblInfo.ID, model.ActionDropForeignKey, []interface{}{model.NewCIStr("c1_foreign_key")}, ctx, d)
Expand Down Expand Up @@ -405,7 +407,8 @@ func (s *testDDLSuite) TestIndexError(c *C) {

dbInfo, err := testSchemaInfo(d, "test_ddl")
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d, dbInfo)
testCreateTable(c, ctx, d, dbInfo, tblInfo)

Expand Down Expand Up @@ -448,7 +451,8 @@ func (s *testDDLSuite) TestColumnError(c *C) {

dbInfo, err := testSchemaInfo(d, "test_ddl")
c.Assert(err, IsNil)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d, dbInfo)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
col := &model.ColumnInfo{
Expand Down Expand Up @@ -752,7 +756,8 @@ func (s *testDDLSerialSuite) TestCancelJob(c *C) {
// Skip using sessPool. Make sure adding primary key can be successful.
partitionTblInfo.Columns[0].Flag |= mysql.NotNullFlag
// create table t (c1 int, c2 int, c3 int, c4 int, c5 int);
tblInfo := testTableInfo(c, d, "t", 5)
tblInfo, err := testTableInfo(d, "t", 5)
c.Assert(err, IsNil)
ctx := testNewContext(d)
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
Expand Down Expand Up @@ -897,7 +902,8 @@ func (s *testDDLSerialSuite) TestCancelJob(c *C) {
s.checkAddColumns(c, d, dbInfo.ID, tblInfo.ID, []string{addingColName}, true)

// for create table
tblInfo1 := testTableInfo(c, d, "t1", 2)
tblInfo1, err := testTableInfo(d, "t1", 2)
c.Assert(err, IsNil)
updateTest(&tests[8])
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo1.ID, model.ActionCreateTable, []interface{}{tblInfo1}, &cancelState)
c.Check(checkErr, IsNil)
Expand Down Expand Up @@ -1199,7 +1205,8 @@ func (s *testDDLSerialSuite) TestCancelJob(c *C) {

// test exchange partition failed caused by canceled
pt := testTableInfoWithPartition(c, d, "pt", 5)
nt := testTableInfo(c, d, "nt", 5)
nt, err := testTableInfo(d, "nt", 5)
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, dbInfo, pt)
testCreateTable(c, ctx, d, dbInfo, nt)

Expand Down Expand Up @@ -1480,7 +1487,8 @@ func (s *testDDLSuite) TestParallelDDL(c *C) {
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d, dbInfo1)
// create table t1 (c1 int, c2 int);
tblInfo1 := testTableInfo(c, d, "t1", 2)
tblInfo1, err := testTableInfo(d, "t1", 2)
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, dbInfo1, tblInfo1)
// insert t1 values (10, 10), (20, 20)
tbl1 := testGetTable(c, d, dbInfo1.ID, tblInfo1.ID)
Expand All @@ -1489,7 +1497,8 @@ func (s *testDDLSuite) TestParallelDDL(c *C) {
_, err = tbl1.AddRecord(ctx, types.MakeDatums(2, 2))
c.Assert(err, IsNil)
// create table t2 (c1 int primary key, c2 int, c3 int);
tblInfo2 := testTableInfo(c, d, "t2", 3)
tblInfo2, err := testTableInfo(d, "t2", 3)
c.Assert(err, IsNil)
tblInfo2.Columns[0].Flag = mysql.PriKeyFlag | mysql.NotNullFlag
tblInfo2.PKIsHandle = true
testCreateTable(c, ctx, d, dbInfo1, tblInfo2)
Expand All @@ -1506,7 +1515,8 @@ func (s *testDDLSuite) TestParallelDDL(c *C) {
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d, dbInfo2)
// create table t3 (c1 int, c2 int, c3 int, c4 int);
tblInfo3 := testTableInfo(c, d, "t3", 4)
tblInfo3, err := testTableInfo(d, "t3", 4)
c.Assert(err, IsNil)
testCreateTable(c, ctx, d, dbInfo2, tblInfo3)
// insert t3 values (11, 22, 33, 44)
tbl3 := testGetTable(c, d, dbInfo2.ID, tblInfo3.ID)
Expand Down
3 changes: 2 additions & 1 deletion ddl/fail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func (s *testColumnChangeSuite) TestFailBeforeDecodeArgs(c *C) {
c.Assert(err, IsNil)
}()
// create table t_fail (c1 int, c2 int);
tblInfo := testTableInfo(c, d, "t_fail", 2)
tblInfo, err := testTableInfo(d, "t_fail", 2)
c.Assert(err, IsNil)
ctx := testNewContext(d)
err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
Expand Down
3 changes: 2 additions & 1 deletion ddl/foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func (s *testForeignKeySuite) TestForeignKey(c *C) {
ctx := testNewContext(d)
s.ctx = ctx
testCreateSchema(c, ctx, d, s.dbInfo)
tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)

err = ctx.NewTxn(context.Background())
c.Assert(err, IsNil)
Expand Down
3 changes: 2 additions & 1 deletion ddl/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func (s *testIndexChangeSuite) TestIndexChange(c *C) {
c.Assert(err, IsNil)
}()
// create table t (c1 int primary key, c2 int);
tblInfo := testTableInfo(c, d, "t", 2)
tblInfo, err := testTableInfo(d, "t", 2)
c.Assert(err, IsNil)
tblInfo.Columns[0].Flag = mysql.PriKeyFlag | mysql.NotNullFlag
tblInfo.PKIsHandle = true
ctx := testNewContext(d)
Expand Down
9 changes: 6 additions & 3 deletions ddl/placement_policy_ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,22 @@ func (s *testDDLSuite) TestPlacementPolicyInUse(c *C) {
testCreatePlacementPolicy(c, sctx, d, p4)
testCreatePlacementPolicy(c, sctx, d, p5)

t1 := testTableInfo(c, d, "t1", 1)
t1, err := testTableInfo(d, "t1", 1)
c.Assert(err, IsNil)
t1.PlacementPolicyRef = &model.PolicyRefInfo{ID: p1.ID, Name: p1.Name}
testCreateTable(c, sctx, d, db1, t1)
t1.State = model.StatePublic
db1.Tables = append(db1.Tables, t1)

t2 := testTableInfo(c, d, "t2", 1)
t2, err := testTableInfo(d, "t2", 1)
c.Assert(err, IsNil)
t2.PlacementPolicyRef = &model.PolicyRefInfo{ID: p1.ID, Name: p1.Name}
testCreateTable(c, sctx, d, db2, t2)
t2.State = model.StatePublic
db2.Tables = append(db2.Tables, t2)

t3 := testTableInfo(c, d, "t3", 1)
t3, err := testTableInfo(d, "t3", 1)
c.Assert(err, IsNil)
t3.PlacementPolicyRef = &model.PolicyRefInfo{ID: p2.ID, Name: p2.Name}
testCreateTable(c, sctx, d, db1, t3)
t3.State = model.StatePublic
Expand Down
3 changes: 2 additions & 1 deletion ddl/reorg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ func (s *testDDLSuite) TestReorgOwner(c *C) {
c.Assert(err, IsNil)
testCreateSchema(c, ctx, d1, dbInfo)

tblInfo := testTableInfo(c, d1, "t", 3)
tblInfo, err := testTableInfo(d1, "t", 3)
c.Assert(err, IsNil)
testCreateTable(c, ctx, d1, dbInfo, tblInfo)
t := testGetTable(c, d1, dbInfo.ID, tblInfo.ID)

Expand Down
3 changes: 2 additions & 1 deletion ddl/restart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ func (s *testTableSuite) TestTableResume(c *C) {

testCheckOwner(c, d, true)

tblInfo := testTableInfo(c, d, "t1", 3)
tblInfo, err := testTableInfo(d, "t1", 3)
c.Assert(err, IsNil)
job := &model.Job{
SchemaID: s.dbInfo.ID,
TableID: tblInfo.ID,
Expand Down
6 changes: 4 additions & 2 deletions ddl/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ func (s *testSchemaSuite) TestSchema(c *C) {

/*** to drop the schema with two tables. ***/
// create table t with 100 records.
tblInfo1 := testTableInfo(c, d, "t", 3)
tblInfo1, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
tJob1 := testCreateTable(c, ctx, d, dbInfo, tblInfo1)
testCheckTableState(c, d, dbInfo, tblInfo1, model.StatePublic)
testCheckJobDone(c, d, tJob1, true)
Expand All @@ -162,7 +163,8 @@ func (s *testSchemaSuite) TestSchema(c *C) {
c.Assert(err, IsNil)
}
// create table t1 with 1034 records.
tblInfo2 := testTableInfo(c, d, "t1", 3)
tblInfo2, err := testTableInfo(d, "t1", 3)
c.Assert(err, IsNil)
tJob2 := testCreateTable(c, ctx, d, dbInfo, tblInfo2)
testCheckTableState(c, d, dbInfo, tblInfo2, model.StatePublic)
testCheckJobDone(c, d, tJob2, true)
Expand Down
3 changes: 2 additions & 1 deletion ddl/stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ func (s *testSerialStatSuite) TestDDLStatsInfo(c *C) {
dbInfo, err := testSchemaInfo(d, "test_stat")
c.Assert(err, IsNil)
testCreateSchema(c, testNewContext(d), d, dbInfo)
tblInfo := testTableInfo(c, d, "t", 2)
tblInfo, err := testTableInfo(d, "t", 2)
c.Assert(err, IsNil)
ctx := testNewContext(d)
testCreateTable(c, ctx, d, dbInfo, tblInfo)

Expand Down
27 changes: 18 additions & 9 deletions ddl/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type testTableSuite struct {
}

func testTableInfoWith2IndexOnFirstColumn(c *C, d *ddl, name string, num int) *model.TableInfo {
normalInfo := testTableInfo(c, d, name, num)
normalInfo, err := testTableInfo(d, name, num)
c.Assert(err, IsNil)
idxs := make([]*model.IndexInfo, 0, 2)
for i := range idxs {
idx := &model.IndexInfo{
Expand All @@ -58,12 +59,15 @@ func testTableInfoWith2IndexOnFirstColumn(c *C, d *ddl, name string, num int) *m
}

// testTableInfo creates a test table with num int columns and with no index.
func testTableInfo(c *C, d *ddl, name string, num int) *model.TableInfo {
func testTableInfo(d *ddl, name string, num int) (*model.TableInfo, error) {
tblInfo := &model.TableInfo{
Name: model.NewCIStr(name),
}
genIDs, err := d.genGlobalIDs(1)
c.Assert(err, IsNil)

if err != nil {
return nil, err
}
tblInfo.ID = genIDs[0]

cols := make([]*model.ColumnInfo, num)
Expand All @@ -82,12 +86,13 @@ func testTableInfo(c *C, d *ddl, name string, num int) *model.TableInfo {
tblInfo.Columns = cols
tblInfo.Charset = "utf8"
tblInfo.Collate = "utf8_bin"
return tblInfo
return tblInfo, nil
}

// testTableInfoWithPartition creates a test table with num int columns and with no index.
func testTableInfoWithPartition(c *C, d *ddl, name string, num int) *model.TableInfo {
tblInfo := testTableInfo(c, d, name, num)
tblInfo, err := testTableInfo(d, name, num)
c.Assert(err, IsNil)
genIDs, err := d.genGlobalIDs(1)
c.Assert(err, IsNil)
pid := genIDs[0]
Expand Down Expand Up @@ -375,13 +380,15 @@ func (s *testTableSuite) TestTable(c *C) {

ctx := testNewContext(d)

tblInfo := testTableInfo(c, d, "t", 3)
tblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
job := testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckTableState(c, d, s.dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(c, d, job, true)

// Create an existing table.
newTblInfo := testTableInfo(c, d, "t", 3)
newTblInfo, err := testTableInfo(d, "t", 3)
c.Assert(err, IsNil)
doDDLJobErr(c, s.dbInfo.ID, newTblInfo.ID, model.ActionCreateTable, []interface{}{newTblInfo}, ctx, d)

count := 2000
Expand All @@ -395,7 +402,8 @@ func (s *testTableSuite) TestTable(c *C) {
testCheckJobDone(c, d, job, false)

// for truncate table
tblInfo = testTableInfo(c, d, "tt", 3)
tblInfo, err = testTableInfo(d, "tt", 3)
c.Assert(err, IsNil)
job = testCreateTable(c, ctx, d, s.dbInfo, tblInfo)
testCheckTableState(c, d, s.dbInfo, tblInfo, model.StatePublic)
testCheckJobDone(c, d, job, true)
Expand Down Expand Up @@ -488,7 +496,8 @@ func testAlterNoCacheTable(c *C, ctx sessionctx.Context, d *ddl, newSchemaID int

// for drop indexes
func createTestTableForDropIndexes(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, name string, num int) *model.TableInfo {
tableInfo := testTableInfo(c, d, name, num)
tableInfo, err := testTableInfo(d, name, num)
c.Assert(err, IsNil)
var idxs []*model.IndexInfo
for i := 0; i < num; i++ {
idxName := model.NewCIStr(fmt.Sprintf("i%d", i+1))
Expand Down

0 comments on commit e47c1f3

Please sign in to comment.