Skip to content

Commit

Permalink
planner: migrate indexmerge_test to testify (pingcap#29337)
Browse files Browse the repository at this point in the history
  • Loading branch information
cr0rc authored Nov 11, 2021
1 parent 9f69dab commit 8e2f1c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 46 deletions.
68 changes: 23 additions & 45 deletions planner/core/indexmerge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,17 @@ package core

import (
"context"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/planner/util"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/testkit/testdata"
"github.com/pingcap/tidb/util/hint"
"github.com/pingcap/tidb/util/testleak"
"github.com/pingcap/tidb/util/testutil"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testIndexMergeSuite{})

type testIndexMergeSuite struct {
*parser.Parser

is infoschema.InfoSchema
ctx sessionctx.Context

testdata testutil.TestData
}

func (s *testIndexMergeSuite) SetUpSuite(c *C) {
s.is = infoschema.MockInfoSchema([]*model.TableInfo{MockSignedTable(), MockView()})
s.ctx = MockContext()
s.Parser = parser.New()
var err error
s.testdata, err = testutil.LoadTestSuiteData("testdata", "index_merge_suite")
c.Assert(err, IsNil)
}

func (s *testIndexMergeSuite) TearDownSuite(c *C) {
c.Assert(s.testdata.GenerateOutputIfNeeded(), IsNil)
}

func getIndexMergePathDigest(paths []*util.AccessPath, startIndex int) string {
if len(paths) == startIndex {
return "[]"
Expand Down Expand Up @@ -82,31 +57,34 @@ func getIndexMergePathDigest(paths []*util.AccessPath, startIndex int) string {
return idxMergeDisgest
}

func (s *testIndexMergeSuite) TestIndexMergePathGeneration(c *C) {
defer testleak.AfterTest(c)()
func TestIndexMergePathGeneration(t *testing.T) {
t.Parallel()
var input, output []string
s.testdata.GetTestCases(c, &input, &output)
indexMergeSuiteData.GetTestCases(t, &input, &output)
ctx := context.TODO()
sctx := MockContext()
is := infoschema.MockInfoSchema([]*model.TableInfo{MockSignedTable(), MockView()})

parser := parser.New()

for i, tc := range input {
comment := Commentf("case:%v sql:%s", i, tc)
stmt, err := s.ParseOneStmt(tc, "", "")
c.Assert(err, IsNil, comment)
err = Preprocess(s.ctx, stmt, WithPreprocessorReturn(&PreprocessorReturn{InfoSchema: s.is}))
c.Assert(err, IsNil)
builder, _ := NewPlanBuilder().Init(MockContext(), s.is, &hint.BlockHintProcessor{})
stmt, err := parser.ParseOneStmt(tc, "", "")
require.NoErrorf(t, err, "case:%v sql:%s", i, tc)
err = Preprocess(sctx, stmt, WithPreprocessorReturn(&PreprocessorReturn{InfoSchema: is}))
require.NoError(t, err)
builder, _ := NewPlanBuilder().Init(MockContext(), is, &hint.BlockHintProcessor{})
p, err := builder.Build(ctx, stmt)
if err != nil {
s.testdata.OnRecord(func() {
testdata.OnRecord(func() {
output[i] = err.Error()
})
c.Assert(err.Error(), Equals, output[i], comment)
require.Equal(t, output[i], err.Error(), "case:%v sql:%s", i, tc)
continue
}
c.Assert(err, IsNil)
require.NoError(t, err)
p, err = logicalOptimize(ctx, builder.optFlag, p.(LogicalPlan))
c.Assert(err, IsNil)
require.NoError(t, err)
lp := p.(LogicalPlan)
c.Assert(err, IsNil)
var ds *DataSource
for ds == nil {
switch v := lp.(type) {
Expand All @@ -119,11 +97,11 @@ func (s *testIndexMergeSuite) TestIndexMergePathGeneration(c *C) {
ds.ctx.GetSessionVars().SetEnableIndexMerge(true)
idxMergeStartIndex := len(ds.possibleAccessPaths)
_, err = lp.recursiveDeriveStats(nil)
c.Assert(err, IsNil)
require.NoError(t, err)
result := getIndexMergePathDigest(ds.possibleAccessPaths, idxMergeStartIndex)
s.testdata.OnRecord(func() {
testdata.OnRecord(func() {
output[i] = result
})
c.Assert(result, Equals, output[i], comment)
require.Equalf(t, output[i], result, "case:%v sql:%s", i, tc)
}
}
5 changes: 4 additions & 1 deletion planner/core/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ import (
"go.uber.org/goleak"
)

var testDataMap = make(testdata.BookKeeper, 1)
var testDataMap = make(testdata.BookKeeper, 2)
var indexMergeSuiteData testdata.TestData

func TestMain(m *testing.M) {
testbridge.WorkaroundGoCheckFlags()

flag.Parse()

testDataMap.LoadTestSuiteData("testdata", "integration_partition_suite")
testDataMap.LoadTestSuiteData("testdata", "index_merge_suite")
indexMergeSuiteData = testDataMap["index_merge_suite"]

opts := []goleak.Option{
goleak.IgnoreTopFunction("go.etcd.io/etcd/pkg/logutil.(*MergeLogger).outputLoop"),
Expand Down

0 comments on commit 8e2f1c7

Please sign in to comment.