Skip to content

Commit

Permalink
testify for partition_test
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaoMiaoGarden committed Dec 22, 2021
1 parent d0fc70e commit 95013bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
6 changes: 6 additions & 0 deletions ddl/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ func testCreateStore(c *C, name string) kv.Storage {
return store
}

func testCreateStoreT(t *testing.T, name string) kv.Storage {
store, err := mockstore.NewMockStore()
require.NoError(t, err)
return store
}

func testNewContext(d *ddl) sessionctx.Context {
ctx := mock.NewContext()
ctx.Store = d.store
Expand Down
62 changes: 24 additions & 38 deletions ddl/partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,41 @@ package ddl

import (
"context"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/stretchr/testify/require"
)

var _ = SerialSuites(&testPartitionSuite{})

type testPartitionSuite struct {
store kv.Storage
}

func (s *testPartitionSuite) SetUpSuite(c *C) {
s.store = testCreateStore(c, "test_store")
}

func (s *testPartitionSuite) TearDownSuite(c *C) {
err := s.store.Close()
c.Assert(err, IsNil)
}

func (s *testPartitionSuite) TestDropAndTruncatePartition(c *C) {
func TestDropAndTruncatePartition(t *testing.T) {
store := testCreateStoreT(t, "test_store")
d, err := testNewDDLAndStart(
context.Background(),
WithStore(s.store),
WithStore(store),
WithLease(testLease),
)
c.Assert(err, IsNil)
require.NoError(t, err)
defer func() {
err := d.Stop()
c.Assert(err, IsNil)
require.NoError(t, err)
}()
dbInfo, err := testSchemaInfo(d, "test_partition")
c.Assert(err, IsNil)
testCreateSchema(c, testNewContext(d), d, dbInfo)
require.NoError(t, err)
testCreateSchemaT(t, testNewContext(d), d, dbInfo)
// generate 5 partition in tableInfo.
tblInfo, partIDs := buildTableInfoWithPartition(c, d)
tblInfo, partIDs := buildTableInfoWithPartition(t, d)
ctx := testNewContext(d)
testCreateTable(c, ctx, d, dbInfo, tblInfo)
testCreateTableT(t, ctx, d, dbInfo, tblInfo)

testDropPartition(c, ctx, d, dbInfo, tblInfo, []string{"p0", "p1"})
testDropPartition(t, ctx, d, dbInfo, tblInfo, []string{"p0", "p1"})

testTruncatePartition(c, ctx, d, dbInfo, tblInfo, []int64{partIDs[3], partIDs[4]})
testTruncatePartition(t, ctx, d, dbInfo, tblInfo, []int64{partIDs[3], partIDs[4]})
}

func buildTableInfoWithPartition(c *C, d *ddl) (*model.TableInfo, []int64) {
func buildTableInfoWithPartition(t *testing.T, d *ddl) (*model.TableInfo, []int64) {
tbl := &model.TableInfo{
Name: model.NewCIStr("t"),
}
Expand All @@ -76,14 +62,14 @@ func buildTableInfoWithPartition(c *C, d *ddl) (*model.TableInfo, []int64) {
ID: allocateColumnID(tbl),
}
genIDs, err := d.genGlobalIDs(1)
c.Assert(err, IsNil)
require.NoError(t, err)
tbl.ID = genIDs[0]
tbl.Columns = []*model.ColumnInfo{col}
tbl.Charset = "utf8"
tbl.Collate = "utf8_bin"

partIDs, err := d.genGlobalIDs(5)
c.Assert(err, IsNil)
require.NoError(t, err)
partInfo := &model.PartitionInfo{
Type: model.PartitionTypeRange,
Expr: tbl.Columns[0].Name.L,
Expand Down Expand Up @@ -130,12 +116,12 @@ func buildDropPartitionJob(dbInfo *model.DBInfo, tblInfo *model.TableInfo, partN
}
}

func testDropPartition(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, partNames []string) *model.Job {
func testDropPartition(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, partNames []string) *model.Job {
job := buildDropPartitionJob(dbInfo, tblInfo, partNames)
err := d.doDDLJob(ctx, job)
c.Assert(err, IsNil)
v := getSchemaVer(c, ctx)
checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
require.NoError(t, err)
v := getSchemaVerT(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}

Expand All @@ -149,11 +135,11 @@ func buildTruncatePartitionJob(dbInfo *model.DBInfo, tblInfo *model.TableInfo, p
}
}

func testTruncatePartition(c *C, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, pids []int64) *model.Job {
func testTruncatePartition(t *testing.T, ctx sessionctx.Context, d *ddl, dbInfo *model.DBInfo, tblInfo *model.TableInfo, pids []int64) *model.Job {
job := buildTruncatePartitionJob(dbInfo, tblInfo, pids)
err := d.doDDLJob(ctx, job)
c.Assert(err, IsNil)
v := getSchemaVer(c, ctx)
checkHistoryJobArgs(c, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
require.NoError(t, err)
v := getSchemaVerT(t, ctx)
checkHistoryJobArgsT(t, ctx, job.ID, &historyJobArgs{ver: v, tbl: tblInfo})
return job
}

0 comments on commit 95013bc

Please sign in to comment.