Skip to content

Commit

Permalink
executor/split_test.go: migrate test-infra to testify (#30485)
Browse files Browse the repository at this point in the history
close #28617
  • Loading branch information
zach030 authored Dec 29, 2021
1 parent 7834328 commit 8193c46
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions executor/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"math"
"math/rand"
"sort"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/model"
Expand All @@ -33,20 +33,10 @@ import (
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mock"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testSplitIndex{})

type testSplitIndex struct {
}

func (s *testSplitIndex) SetUpSuite(c *C) {
}

func (s *testSplitIndex) TearDownSuite(c *C) {
}

func (s *testSplitIndex) TestLongestCommonPrefixLen(c *C) {
func TestLongestCommonPrefixLen(t *testing.T) {
cases := []struct {
s1 string
s2 string
Expand All @@ -64,11 +54,11 @@ func (s *testSplitIndex) TestLongestCommonPrefixLen(c *C) {

for _, ca := range cases {
re := longestCommonPrefixLen([]byte(ca.s1), []byte(ca.s2))
c.Assert(re, Equals, ca.l)
require.Equal(t, ca.l, re)
}
}

func (s *testSplitIndex) TestgetStepValue(c *C) {
func TestGetStepValue(t *testing.T) {
cases := []struct {
lower []byte
upper []byte
Expand All @@ -86,13 +76,13 @@ func (s *testSplitIndex) TestgetStepValue(c *C) {

for _, ca := range cases {
l := longestCommonPrefixLen(ca.lower, ca.upper)
c.Assert(l, Equals, ca.l)
require.Equal(t, ca.l, l)
v0 := getStepValue(ca.lower[l:], ca.upper[l:], 1)
c.Assert(v0, Equals, ca.v)
require.Equal(t, v0, ca.v)
}
}

func (s *testSplitIndex) TestSplitIndex(c *C) {
func TestSplitIndex(t *testing.T) {
tbInfo := &model.TableInfo{
Name: model.NewCIStr("t1"),
ID: rand.Int63(),
Expand Down Expand Up @@ -144,8 +134,8 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {
}
valueList, err := e.getSplitIdxKeys()
sort.Slice(valueList, func(i, j int) bool { return bytes.Compare(valueList[i], valueList[j]) < 0 })
c.Assert(err, IsNil)
c.Assert(len(valueList), Equals, e.num+1)
require.NoError(t, err)
require.Len(t, valueList, e.num+1)

cases := []struct {
value int
Expand Down Expand Up @@ -173,15 +163,15 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {
for _, ca := range cases {
// test for minInt64 handle
idxValue, _, err := index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(ca.value)}, kv.IntHandle(math.MinInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx := searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)

// Test for max int64 handle.
idxValue, _, err = index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(ca.value)}, kv.IntHandle(math.MaxInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx = searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)
}
// Test for varchar index.
// range is a ~ z, and split into 26 region.
Expand All @@ -200,8 +190,8 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {

valueList, err = e.getSplitIdxKeys()
sort.Slice(valueList, func(i, j int) bool { return bytes.Compare(valueList[i], valueList[j]) < 0 })
c.Assert(err, IsNil)
c.Assert(len(valueList), Equals, e.num+1)
require.NoError(t, err)
require.Len(t, valueList, e.num+1)

cases2 := []struct {
value string
Expand All @@ -221,15 +211,15 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {
for _, ca := range cases2 {
// test for minInt64 handle
idxValue, _, err := index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(ca.value)}, kv.IntHandle(math.MinInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx := searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)

// Test for max int64 handle.
idxValue, _, err = index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(ca.value)}, kv.IntHandle(math.MaxInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx = searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)
}

// Test for timestamp index.
Expand All @@ -252,8 +242,8 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {

valueList, err = e.getSplitIdxKeys()
sort.Slice(valueList, func(i, j int) bool { return bytes.Compare(valueList[i], valueList[j]) < 0 })
c.Assert(err, IsNil)
c.Assert(len(valueList), Equals, e.num+1)
require.NoError(t, err)
require.Len(t, valueList, e.num+1)

cases3 := []struct {
value types.CoreTime
Expand All @@ -279,19 +269,19 @@ func (s *testSplitIndex) TestSplitIndex(c *C) {
value := types.NewTime(ca.value, mysql.TypeTimestamp, types.DefaultFsp)
// test for min int64 handle
idxValue, _, err := index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(value)}, kv.IntHandle(math.MinInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx := searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)

// Test for max int64 handle.
idxValue, _, err = index.GenIndexKey(ctx.GetSessionVars().StmtCtx, []types.Datum{types.NewDatum(value)}, kv.IntHandle(math.MaxInt64), nil)
c.Assert(err, IsNil)
require.NoError(t, err)
idx = searchLessEqualIdx(valueList, idxValue)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)
}
}

func (s *testSplitIndex) TestSplitTable(c *C) {
func TestSplitTable(t *testing.T) {
tbInfo := &model.TableInfo{
Name: model.NewCIStr("t1"),
ID: rand.Int63(),
Expand Down Expand Up @@ -332,8 +322,8 @@ func (s *testSplitIndex) TestSplitTable(c *C) {
num: 10,
}
valueList, err := e.getSplitTableKeys()
c.Assert(err, IsNil)
c.Assert(len(valueList), Equals, e.num-1)
require.NoError(t, err)
require.Len(t, valueList, e.num-1)

cases := []struct {
value int
Expand Down Expand Up @@ -361,13 +351,13 @@ func (s *testSplitIndex) TestSplitTable(c *C) {
for _, ca := range cases {
// test for minInt64 handle
key := tablecodec.EncodeRecordKey(recordPrefix, kv.IntHandle(ca.value))
c.Assert(err, IsNil)
require.NoError(t, err)
idx := searchLessEqualIdx(valueList, key)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)
}
}

func (s *testSplitIndex) TestClusterIndexSplitTable(c *C) {
func TestClusterIndexSplitTable(t *testing.T) {
tbInfo := &model.TableInfo{
Name: model.NewCIStr("t"),
ID: 1,
Expand Down Expand Up @@ -423,8 +413,8 @@ func (s *testSplitIndex) TestClusterIndexSplitTable(c *C) {
num: 10,
}
valueList, err := e.getSplitTableKeys()
c.Assert(err, IsNil)
c.Assert(len(valueList), Equals, e.num-1)
require.NoError(t, err)
require.Len(t, valueList, e.num-1)

cases := []struct {
value []types.Datum
Expand Down Expand Up @@ -453,11 +443,11 @@ func (s *testSplitIndex) TestClusterIndexSplitTable(c *C) {
recordPrefix := tablecodec.GenTableRecordPrefix(e.tableInfo.ID)
for _, ca := range cases {
h, err := e.handleCols.BuildHandleByDatums(ca.value)
c.Assert(err, IsNil)
require.NoError(t, err)
key := tablecodec.EncodeRecordKey(recordPrefix, h)
c.Assert(err, IsNil)
require.NoError(t, err)
idx := searchLessEqualIdx(valueList, key)
c.Assert(idx, Equals, ca.lessEqualIdx, Commentf("%#v", ca))
require.Equal(t, idx, ca.lessEqualIdx)
}
}

Expand Down

0 comments on commit 8193c46

Please sign in to comment.