Skip to content

Commit

Permalink
util: migrate test-infra to testify for dbutil,filter,table-filter (#…
Browse files Browse the repository at this point in the history
…33465)

close #33454, ref #33456, ref #33458, ref #33459
  • Loading branch information
hawkingrei authored Mar 26, 2022
1 parent 02545c8 commit d7c157d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 156 deletions.
51 changes: 26 additions & 25 deletions util/dbutil/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@ package dbutil

import (
"context"
"testing"
"time"

sqlmock "github.com/DATA-DOG/go-sqlmock"
"github.com/go-sql-driver/mysql"
. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/parser/model"
pmysql "github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/types"
"github.com/stretchr/testify/require"
)

func (*testDBSuite) TestReplacePlaceholder(c *C) {
func TestReplacePlaceholder(t *testing.T) {
testCases := []struct {
originStr string
args []string
Expand All @@ -47,12 +48,12 @@ func (*testDBSuite) TestReplacePlaceholder(c *C) {

for _, testCase := range testCases {
str := ReplacePlaceholder(testCase.originStr, testCase.args)
c.Assert(str, Equals, testCase.expectStr)
require.Equal(t, testCase.expectStr, str)
}

}

func (*testDBSuite) TestTableName(c *C) {
func TestTableName(t *testing.T) {
testCases := []struct {
schema string
table string
Expand All @@ -77,11 +78,11 @@ func (*testDBSuite) TestTableName(c *C) {

for _, testCase := range testCases {
tableName := TableName(testCase.schema, testCase.table)
c.Assert(tableName, Equals, testCase.expectTableName)
require.Equal(t, testCase.expectTableName, tableName)
}
}

func (*testDBSuite) TestColumnName(c *C) {
func TestColumnName(t *testing.T) {
testCases := []struct {
column string
expectColName string
Expand All @@ -102,7 +103,7 @@ func (*testDBSuite) TestColumnName(c *C) {

for _, testCase := range testCases {
colName := ColumnName(testCase.column)
c.Assert(colName, Equals, testCase.expectColName)
require.Equal(t, testCase.expectColName, colName)
}
}

Expand All @@ -113,7 +114,7 @@ func newMysqlErr(number uint16, message string) *mysql.MySQLError {
}
}

func (s *testDBSuite) TestIsIgnoreError(c *C) {
func TestIsIgnoreError(t *testing.T) {
cases := []struct {
err error
canIgnore bool
Expand All @@ -129,29 +130,29 @@ func (s *testDBSuite) TestIsIgnoreError(c *C) {
{errors.New("unknown error"), false},
}

for _, t := range cases {
c.Logf("err %v, expected %v", t.err, t.canIgnore)
c.Assert(ignoreError(t.err), Equals, t.canIgnore)
for _, tt := range cases {
t.Logf("err %v, expected %v", tt.err, tt.canIgnore)
require.Equal(t, tt.canIgnore, ignoreError(tt.err))
}
}

func (s *testDBSuite) TestDeleteRows(c *C) {
func TestDeleteRows(t *testing.T) {
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
require.NoError(t, err)

// delete twice
mock.ExpectExec("DELETE FROM").WillReturnResult(sqlmock.NewResult(0, DefaultDeleteRowsNum))
mock.ExpectExec("DELETE FROM").WillReturnResult(sqlmock.NewResult(0, DefaultDeleteRowsNum-1))

err = DeleteRows(context.Background(), db, "test", "t", "", nil)
c.Assert(err, IsNil)
require.NoError(t, err)

if err := mock.ExpectationsWereMet(); err != nil {
c.Errorf("there were unfulfilled expectations: %s", err)
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

func (s *testDBSuite) TestGetParser(c *C) {
func TestGetParser(t *testing.T) {
testCases := []struct {
sqlModeStr string
hasErr bool
Expand All @@ -177,15 +178,15 @@ func (s *testDBSuite) TestGetParser(c *C) {
for _, testCase := range testCases {
parser, err := getParser(testCase.sqlModeStr)
if testCase.hasErr {
c.Assert(err, NotNil)
require.Error(t, err)
} else {
c.Assert(err, IsNil)
c.Assert(parser, NotNil)
require.NoError(t, err)
require.NotNil(t, parser)
}
}
}

func (s *testDBSuite) TestAnalyzeValuesFromBuckets(c *C) {
func TestAnalyzeValuesFromBuckets(t *testing.T) {
cases := []struct {
value string
col *model.ColumnInfo
Expand Down Expand Up @@ -224,13 +225,13 @@ func (s *testDBSuite) TestAnalyzeValuesFromBuckets(c *C) {
}
for _, ca := range cases {
val, err := AnalyzeValuesFromBuckets(ca.value, []*model.ColumnInfo{ca.col})
c.Assert(err, IsNil)
c.Assert(val, HasLen, 1)
c.Assert(val[0], Equals, ca.expect)
require.NoError(t, err)
require.Len(t, val, 1)
require.Equal(t, ca.expect, val[0])
}
}

func (s *testDBSuite) TestFormatTimeZoneOffset(c *C) {
func TestFormatTimeZoneOffset(t *testing.T) {
cases := map[string]time.Duration{
"+00:00": 0,
"+01:00": time.Hour,
Expand All @@ -241,6 +242,6 @@ func (s *testDBSuite) TestFormatTimeZoneOffset(c *C) {

for k, v := range cases {
offset := FormatTimeZoneOffset(v)
c.Assert(k, Equals, offset)
require.Equal(t, offset, k)
}
}
12 changes: 7 additions & 5 deletions util/dbutil/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
package dbutil

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

"github.com/pingcap/tidb/parser"
"github.com/stretchr/testify/require"
)

func (*testDBSuite) TestIndex(c *C) {
func TestIndex(t *testing.T) {
testCases := []struct {
sql string
indices []string
Expand Down Expand Up @@ -84,16 +86,16 @@ func (*testDBSuite) TestIndex(c *C) {

for _, testCase := range testCases {
tableInfo, err := GetTableInfoBySQL(testCase.sql, parser.New())
c.Assert(err, IsNil)
require.NoError(t, err)

indices := FindAllIndex(tableInfo)
for i, index := range indices {
c.Assert(index.Name.O, Equals, testCase.indices[i])
require.Equal(t, testCase.indices[i], index.Name.O)
}

cols := FindAllColumnWithIndex(tableInfo)
for j, col := range cols {
c.Assert(col.Name.O, Equals, testCase.cols[j])
require.Equal(t, testCase.cols[j], col.Name.O)
}
}
}
11 changes: 4 additions & 7 deletions util/dbutil/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ package dbutil
import (
"database/sql/driver"
"errors"
"testing"

"github.com/go-sql-driver/mysql"
. "github.com/pingcap/check"
"github.com/pingcap/tidb/errno"
"github.com/stretchr/testify/require"
)

var _ = Suite(&testRetrySuite{})

type testRetrySuite struct{}

func (t *testRetrySuite) TestIsRetryableError(c *C) {
func TestIsRetryableError(t *testing.T) {
cases := []struct {
err error
retryable bool
Expand Down Expand Up @@ -126,6 +123,6 @@ func (t *testRetrySuite) TestIsRetryableError(c *C) {
}

for _, cs := range cases {
c.Assert(IsRetryableError(cs.err), Equals, cs.retryable)
require.Equal(t, cs.retryable, IsRetryableError(cs.err))
}
}
42 changes: 17 additions & 25 deletions util/dbutil/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,13 @@ package dbutil
import (
"testing"

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

func TestClient(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testDBSuite{})

type testDBSuite struct{}

type testCase struct {
sql string
columns []string
Expand All @@ -41,7 +33,7 @@ type testCase struct {
fineCol bool
}

func (*testDBSuite) TestTable(c *C) {
func TestTable(t *testing.T) {
testCases := []*testCase{
{
`
Expand Down Expand Up @@ -123,51 +115,51 @@ func (*testDBSuite) TestTable(c *C) {

for _, testCase := range testCases {
tableInfo, err := GetTableInfoBySQL(testCase.sql, parser.New())
c.Assert(err, IsNil)
require.NoError(t, err)
for i, column := range tableInfo.Columns {
c.Assert(testCase.columns[i], Equals, column.Name.O)
require.Equal(t, column.Name.O, testCase.columns[i])
}

c.Assert(tableInfo.Indices, HasLen, len(testCase.indexs))
require.Len(t, tableInfo.Indices, len(testCase.indexs))
for j, index := range tableInfo.Indices {
c.Assert(testCase.indexs[j], Equals, index.Name.O)
require.Equal(t, index.Name.O, testCase.indexs[j])
for k, indexCol := range index.Columns {
c.Assert(indexCol.Length, Equals, testCase.colLen[j][k])
require.Equal(t, testCase.colLen[j][k], indexCol.Length)
}
}

col := FindColumnByName(tableInfo.Columns, testCase.colName)
c.Assert(testCase.fineCol, Equals, col != nil)
require.Equal(t, col != nil, testCase.fineCol)
}
}

func (*testDBSuite) TestTableStructEqual(c *C) {
func TestTableStructEqual(t *testing.T) {
createTableSQL1 := "CREATE TABLE `test`.`atest` (`id` int(24), `name` varchar(24), `birthday` datetime, `update_time` time, `money` decimal(20,2), primary key(`id`))"
tableInfo1, err := GetTableInfoBySQL(createTableSQL1, parser.New())
c.Assert(err, IsNil)
require.NoError(t, err)

createTableSQL2 := "CREATE TABLE `test`.`atest` (`id` int(24) NOT NULL, `name` varchar(24), `birthday` datetime, `update_time` time, `money` decimal(20,2), primary key(`id`))"
tableInfo2, err := GetTableInfoBySQL(createTableSQL2, parser.New())
c.Assert(err, IsNil)
require.NoError(t, err)

createTableSQL3 := `CREATE TABLE "test"."atest" ("id" int(24), "name" varchar(24), "birthday" datetime, "update_time" time, "money" decimal(20,2), unique key("id"))`
p := parser.New()
p.SetSQLMode(mysql.ModeANSIQuotes)
tableInfo3, err := GetTableInfoBySQL(createTableSQL3, p)
c.Assert(err, IsNil)
require.NoError(t, err)

equal, _ := EqualTableInfo(tableInfo1, tableInfo2)
c.Assert(equal, Equals, true)
require.Equal(t, true, equal)

equal, _ = EqualTableInfo(tableInfo1, tableInfo3)
c.Assert(equal, Equals, false)
require.Equal(t, false, equal)
}

func (*testDBSuite) TestSchemacmpEncode(c *C) {
func TestSchemacmpEncode(t *testing.T) {
createTableSQL := "CREATE TABLE `test`.`atest` (`id` int(24), primary key(`id`))"
tableInfo, err := GetTableInfoBySQL(createTableSQL, parser.New())
c.Assert(err, IsNil)
require.NoError(t, err)

table := schemacmp.Encode(tableInfo)
c.Assert(table.String(), Equals, "CREATE TABLE `tbl`(`id` INT(24) NOT NULL, PRIMARY KEY (`id`)) CHARSET UTF8MB4 COLLATE UTF8MB4_BIN")
require.Equal(t, "CREATE TABLE `tbl`(`id` INT(24) NOT NULL, PRIMARY KEY (`id`)) CHARSET UTF8MB4 COLLATE UTF8MB4_BIN", table.String())
}
35 changes: 18 additions & 17 deletions util/dbutil/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ package dbutil

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
)

func (*testDBSuite) TestShowGrants(c *C) {
func TestShowGrants(t *testing.T) {
ctx := context.Background()
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
require.NoError(t, err)

mockGrants := []string{
"GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION",
Expand All @@ -37,15 +38,15 @@ func (*testDBSuite) TestShowGrants(c *C) {
mock.ExpectQuery("SHOW GRANTS").WillReturnRows(rows)

grants, err := ShowGrants(ctx, db, "", "")
c.Assert(err, IsNil)
c.Assert(grants, DeepEquals, mockGrants)
c.Assert(mock.ExpectationsWereMet(), IsNil)
require.NoError(t, err)
require.Equal(t, mockGrants, grants)
require.Nil(t, mock.ExpectationsWereMet())
}

func (*testDBSuite) TestShowGrantsWithRoles(c *C) {
func TestShowGrantsWithRoles(t *testing.T) {
ctx := context.Background()
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
require.NoError(t, err)

mockGrantsWithoutRoles := []string{
"GRANT USAGE ON *.* TO `u1`@`localhost`",
Expand All @@ -69,15 +70,15 @@ func (*testDBSuite) TestShowGrantsWithRoles(c *C) {
mock.ExpectQuery("SHOW GRANTS").WillReturnRows(rows2)

grants, err := ShowGrants(ctx, db, "", "")
c.Assert(err, IsNil)
c.Assert(grants, DeepEquals, mockGrantsWithRoles)
c.Assert(mock.ExpectationsWereMet(), IsNil)
require.NoError(t, err)
require.Equal(t, mockGrantsWithRoles, grants)
require.Nil(t, mock.ExpectationsWereMet())
}

func (*testDBSuite) TestShowGrantsPasswordMasked(c *C) {
func TestShowGrantsPasswordMasked(t *testing.T) {
ctx := context.Background()
db, mock, err := sqlmock.New()
c.Assert(err, IsNil)
require.NoError(t, err)

cases := []struct {
original string
Expand Down Expand Up @@ -107,9 +108,9 @@ func (*testDBSuite) TestShowGrantsPasswordMasked(c *C) {
mock.ExpectQuery("SHOW GRANTS").WillReturnRows(rows)

grants, err := ShowGrants(ctx, db, "", "")
c.Assert(err, IsNil)
c.Assert(grants, HasLen, 1)
c.Assert(grants[0], DeepEquals, ca.expected)
c.Assert(mock.ExpectationsWereMet(), IsNil)
require.NoError(t, err)
require.Len(t, grants, 1)
require.Equal(t, ca.expected, grants[0])
require.Nil(t, mock.ExpectationsWereMet())
}
}
Loading

0 comments on commit d7c157d

Please sign in to comment.