Skip to content

Commit

Permalink
util: migrate test-infra to testify for util/schemacmp pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Weizhen Wang <[email protected]>
  • Loading branch information
hawkingrei committed Mar 26, 2022
1 parent e5d3e22 commit 36368a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 62 deletions.
53 changes: 23 additions & 30 deletions util/schemacmp/lattice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ package schemacmp_test

import (
"bytes"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/parser/mysql"
. "github.com/pingcap/tidb/util/schemacmp"
"github.com/stretchr/testify/require"
)

// eqBytes is a sample type used for testing EqualitySingleton.
Expand Down Expand Up @@ -71,11 +72,7 @@ func (uintMap) ShouldDeleteIncompatibleJoin() bool {
return true
}

type latticeSchema struct{}

var _ = Suite(&latticeSchema{})

func (*latticeSchema) TestCompatibilities(c *C) {
func TestCompatibilities(t *testing.T) {
testCases := []struct {
a Lattice
b Lattice
Expand Down Expand Up @@ -553,53 +550,49 @@ func (*latticeSchema) TestCompatibilities(c *C) {
}

for _, tc := range testCases {
assert := func(obtained interface{}, checker Checker, args ...interface{}) {
args = append(args, Commentf("test case = %+v", tc))
c.Assert(obtained, checker, args...)
}

cmp, err := tc.a.Compare(tc.b)
if len(tc.compareError) != 0 {
assert(err, FitsTypeOf, &IncompatibleError{})
assert(err, ErrorMatches, tc.compareError)
require.IsType(t, &IncompatibleError{}, err)
require.Regexp(t, tc.compareError, err)
} else {
assert(err, IsNil)
assert(cmp, Equals, tc.compareResult)
require.NoError(t, err)
require.Equal(t, tc.compareResult, cmp)
}

cmp, err = tc.b.Compare(tc.a)
if len(tc.compareError) != 0 {
assert(err, FitsTypeOf, &IncompatibleError{})
assert(err, ErrorMatches, tc.compareError)
require.IsType(t, &IncompatibleError{}, err)
require.Regexp(t, tc.compareError, err)
} else {
assert(err, IsNil)
assert(cmp, Equals, -tc.compareResult)
require.NoError(t, err)
require.Equal(t, -tc.compareResult, cmp)
}

join, err := tc.a.Join(tc.b)
if len(tc.joinError) != 0 {
assert(err, FitsTypeOf, &IncompatibleError{})
assert(err, ErrorMatches, tc.joinError)
require.IsType(t, &IncompatibleError{}, err)
require.Regexp(t, tc.joinError, err)
} else {
assert(err, IsNil)
assert(tc.join, DeepEquals, join)
require.NoError(t, err)
require.Equal(t, tc.join, join)
}

join, err = tc.b.Join(tc.a)
if len(tc.joinError) != 0 {
assert(err, FitsTypeOf, &IncompatibleError{})
assert(err, ErrorMatches, tc.joinError)
require.IsType(t, &IncompatibleError{}, err)
require.Regexp(t, tc.joinError, err)
} else {
assert(err, IsNil)
assert(tc.join, DeepEquals, join)
require.NoError(t, err)
require.Equal(t, tc.join, join)

cmp, err = join.Compare(tc.a)
assert(err, IsNil)
assert(cmp, GreaterEqual, 0)
require.NoError(t, err)
require.GreaterOrEqual(t, cmp, 0)

cmp, err = join.Compare(tc.b)
assert(err, IsNil)
assert(cmp, GreaterEqual, 0)
require.NoError(t, err)
require.GreaterOrEqual(t, cmp, 0)
}
}
}
54 changes: 22 additions & 32 deletions util/schemacmp/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
package schemacmp_test

import (
. "github.com/pingcap/check"
"testing"

"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/types"
. "github.com/pingcap/tidb/util/schemacmp"
"github.com/stretchr/testify/require"
)

type typeSchema struct{}

var _ = Suite(&typeSchema{})

const binary = "binary"

var (
Expand Down Expand Up @@ -315,7 +313,7 @@ var (
}
)

func (*typeSchema) TestTypeUnwrap(c *C) {
func TestTypeUnwrap(t *testing.T) {
testCases := []*types.FieldType{
typeInt,
typeIntNotNull,
Expand Down Expand Up @@ -346,15 +344,12 @@ func (*typeSchema) TestTypeUnwrap(c *C) {
}

for _, tc := range testCases {
assert := func(expected interface{}, checker Checker, args ...interface{}) {
c.Assert(expected, checker, append(args, Commentf("tc = %s", tc))...)
}
t := Type(tc)
assert(t.Unwrap(), DeepEquals, tc)
tt := Type(tc)
require.EqualValues(t, tc, tt.Unwrap())
}
}

func (*typeSchema) TestTypeCompareJoin(c *C) {
func TestTypeCompareJoin(t *testing.T) {
testCases := []struct {
a *types.FieldType
b *types.FieldType
Expand Down Expand Up @@ -445,46 +440,41 @@ func (*typeSchema) TestTypeCompareJoin(c *C) {
}

for _, tc := range testCases {
assert := func(expected interface{}, checker Checker, args ...interface{}) {
args = append(args, Commentf("a = %v %#x, b = %v %#x", tc.a, tc.a.Flag, tc.b, tc.b.Flag))
c.Assert(expected, checker, args...)
}

a := Type(tc.a)
b := Type(tc.b)
cmp, err := a.Compare(b)
if len(tc.compareError) != 0 {
if err == nil {
c.Log(cmp)
t.Log(cmp)
}
assert(err, ErrorMatches, tc.compareError)
require.Regexp(t, tc.compareError, err)

} else {
assert(err, IsNil)
assert(cmp, Equals, tc.compareResult)
require.NoError(t, err)
require.Equal(t, tc.compareResult, cmp)
}

cmp, err = b.Compare(a)
if len(tc.compareError) != 0 {
assert(err, ErrorMatches, tc.compareError)
require.Regexp(t, tc.compareError, err)
} else {
assert(err, IsNil)
assert(cmp, Equals, -tc.compareResult)
require.NoError(t, err)
require.Equal(t, -tc.compareResult, cmp)
}

wrappedJoin, err := a.Join(b)
if len(tc.joinError) != 0 {
assert(err, ErrorMatches, tc.joinError)
require.Regexp(t, tc.joinError, err)
} else {
assert(err, IsNil)
assert(wrappedJoin.Unwrap(), DeepEquals, tc.join)

require.NoError(t, err)
require.EqualValues(t, tc.join, wrappedJoin.Unwrap())
cmp, err = wrappedJoin.Compare(a)
assert(err, IsNil)
assert(cmp, GreaterEqual, 0)
require.NoError(t, err)
require.GreaterOrEqual(t, cmp, 0)

cmp, err = wrappedJoin.Compare(b)
assert(err, IsNil)
assert(cmp, GreaterEqual, 0)
require.NoError(t, err)
require.GreaterOrEqual(t, cmp, 0)
}
}
}

0 comments on commit 36368a6

Please sign in to comment.