Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table: improve package unit test code coverage to 92% #10360

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion table/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
71 changes: 70 additions & 1 deletion table/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -362,7 +432,6 @@ func (t *testTableSuite) TestGetDefaultValue(c *C) {
}
c.Assert(val, DeepEquals, tt.val)
}

}

func newCol(name string) *Column {
Expand Down