From 071f12104e216f7d318b58fefea10ee821814dbd Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Mon, 6 May 2019 13:42:23 +0800 Subject: [PATCH] table: improve package unit test code coverage to 92% --- table/column.go | 2 +- table/column_test.go | 71 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/table/column.go b/table/column.go index 48ec41eff751a..0c05e823f0d1f 100644 --- a/table/column.go +++ b/table/column.go @@ -394,7 +394,7 @@ func getColDefaultValue(ctx sessionctx.Context, col *model.ColumnInfo, defaultVa return types.Datum{}, errGetDefaultFailed.GenWithStack("Field '%s' get default value fail - %s", col.Name, err) } - // If the column's default value is not ZeroDatetimeStr nor CurrentTimestamp, should convert the default value to the current session time zone. + // If the column's default value is not ZeroDatetimeStr or CurrentTimestamp, convert the default value to the current session time zone. if needChangeTimeZone { t := value.GetMysqlTime() err = t.ConvertTimeZone(sc.TimeZone, ctx.GetSessionVars().Location()) diff --git a/table/column_test.go b/table/column_test.go index ab48e27c2edb8..363b0efa36cee 100644 --- a/table/column_test.go +++ b/table/column_test.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/parser/mysql" "github.com/pingcap/tidb/sessionctx/stmtctx" "github.com/pingcap/tidb/types" + "github.com/pingcap/tidb/types/json" "github.com/pingcap/tidb/util/mock" "github.com/pingcap/tidb/util/testleak" ) @@ -108,6 +109,24 @@ func (t *testTableSuite) TestCheck(c *C) { CheckOnce([]*Column{}) } +func (t *testTableSuite) TestHandleBadNull(c *C) { + col := newCol("a") + sc := new(stmtctx.StatementContext) + d, err := col.HandleBadNull(types.Datum{}, sc) + c.Assert(err, IsNil) + cmp, err := d.CompareDatum(sc, &types.Datum{}) + c.Assert(err, IsNil) + c.Assert(cmp, Equals, 0) + + col.Flag |= mysql.NotNullFlag + d, err = col.HandleBadNull(types.Datum{}, sc) + c.Assert(err, NotNil) + + sc.BadNullAsWarning = true + d, err = col.HandleBadNull(types.Datum{}, sc) + c.Assert(err, IsNil) +} + func (t *testTableSuite) TestDesc(c *C) { defer testleak.AfterTest(c)() col := newCol("a") @@ -203,6 +222,19 @@ func (t *testTableSuite) TestGetZeroValue(c *C) { }, types.NewDatum(make([]byte, 2)), }, + { + &types.FieldType{ + Tp: mysql.TypeString, + Flen: 2, + Charset: charset.CharsetUTF8MB4, + Collate: charset.CollationBin, + }, + types.NewDatum(""), + }, + { + types.NewFieldType(mysql.TypeJSON), + types.NewDatum(json.CreateBinary(nil)), + }, } sc := new(stmtctx.StatementContext) for _, tt := range tests { @@ -249,11 +281,23 @@ func (t *testTableSuite) TestCastValue(c *C) { val, err = CastValue(ctx, types.NewDatum("test"), &colInfoS) c.Assert(err, IsNil) c.Assert(val, NotNil) + + colInfoS.Charset = mysql.UTF8Charset + _, err = CastValue(ctx, types.NewDatum([]byte{0xf0, 0x9f, 0x8c, 0x80}), &colInfoS) + c.Assert(err, NotNil) + + colInfoS.Charset = mysql.UTF8MB4Charset + _, err = CastValue(ctx, types.NewDatum([]byte{0xf0, 0x9f, 0x80}), &colInfoS) + c.Assert(err, NotNil) } func (t *testTableSuite) TestGetDefaultValue(c *C) { ctx := mock.NewContext() zeroTimestamp := types.ZeroTimestamp + timestampValue := types.Time{ + Time: types.FromDate(2019, 5, 6, 12, 48, 49, 0), + Type: mysql.TypeTimestamp, + } tests := []struct { colInfo *model.ColumnInfo strict bool @@ -319,6 +363,32 @@ func (t *testTableSuite) TestGetDefaultValue(c *C) { types.NewDatum(zeroTimestamp), nil, }, + { + &model.ColumnInfo{ + FieldType: types.FieldType{ + Tp: mysql.TypeTimestamp, + Flag: mysql.TimestampFlag, + }, + OriginDefaultValue: timestampValue.String(), + DefaultValue: timestampValue.String(), + }, + true, + types.NewDatum(timestampValue), + nil, + }, + { + &model.ColumnInfo{ + FieldType: types.FieldType{ + Tp: mysql.TypeTimestamp, + Flag: mysql.TimestampFlag, + }, + OriginDefaultValue: "not valid date", + DefaultValue: "not valid date", + }, + true, + types.NewDatum(zeroTimestamp), + errGetDefaultFailed, + }, { &model.ColumnInfo{ FieldType: types.FieldType{ @@ -362,7 +432,6 @@ func (t *testTableSuite) TestGetDefaultValue(c *C) { } c.Assert(val, DeepEquals, tt.val) } - } func newCol(name string) *Column {