Skip to content

Commit

Permalink
expression: improve UT coverage of expression package to 80% (#10641)
Browse files Browse the repository at this point in the history
  • Loading branch information
qw4990 authored May 30, 2019
1 parent 41f8612 commit ae58fd0
Show file tree
Hide file tree
Showing 6 changed files with 948 additions and 15 deletions.
8 changes: 8 additions & 0 deletions expression/builtin_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (s *testEvaluatorSuite) TestDatabase(c *C) {
d, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "test")
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())

// Test case for schema().
fc = funcs[ast.Schema]
Expand All @@ -51,6 +52,7 @@ func (s *testEvaluatorSuite) TestDatabase(c *C) {
d, err = evalBuiltinFunc(f, chunk.MutRowFromDatums(types.MakeDatums()).ToRow())
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "test")
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestFoundRows(c *C) {
Expand Down Expand Up @@ -79,6 +81,7 @@ func (s *testEvaluatorSuite) TestUser(c *C) {
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "root@localhost")
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestCurrentUser(c *C) {
Expand All @@ -93,6 +96,7 @@ func (s *testEvaluatorSuite) TestCurrentUser(c *C) {
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "root@localhost")
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestCurrentRole(c *C) {
Expand All @@ -109,6 +113,7 @@ func (s *testEvaluatorSuite) TestCurrentRole(c *C) {
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d.GetString(), Equals, "`r_1`@`%`,`r_2`@`localhost`")
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestConnectionID(c *C) {
Expand All @@ -123,6 +128,7 @@ func (s *testEvaluatorSuite) TestConnectionID(c *C) {
d, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(d.GetUint64(), Equals, uint64(1))
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestVersion(c *C) {
Expand All @@ -133,6 +139,7 @@ func (s *testEvaluatorSuite) TestVersion(c *C) {
v, err := evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(v.GetString(), Equals, mysql.ServerVersion)
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

func (s *testEvaluatorSuite) TestBenchMark(c *C) {
Expand Down Expand Up @@ -213,6 +220,7 @@ func (s *testEvaluatorSuite) TestRowCount(c *C) {
c.Assert(err, IsNil)
c.Assert(isNull, IsFalse)
c.Assert(intResult, Equals, int64(10))
c.Assert(f.Clone().PbCode(), Equals, f.PbCode())
}

// TestTiDBVersion for tidb_server().
Expand Down
78 changes: 78 additions & 0 deletions expression/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/testleak"
)
Expand Down Expand Up @@ -345,3 +347,79 @@ func (*testExpressionSuite) TestDeferredExprNullConstantFold(c *C) {
c.Assert(newConst.DeferredExpr.String(), Equals, tt.deferred, comment)
}
}

func (*testExpressionSuite) TestDeferredExprNotNull(c *C) {
defer testleak.AfterTest(c)()
m := &MockExpr{}
ctx := mock.NewContext()
cst := &Constant{DeferredExpr: m, RetType: newIntFieldType()}
m.i, m.err = nil, fmt.Errorf("ERROR")
_, _, err := cst.EvalInt(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalReal(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalDecimal(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalString(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalTime(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalDuration(ctx, chunk.Row{})
c.Assert(err, NotNil)
_, _, err = cst.EvalJSON(ctx, chunk.Row{})
c.Assert(err, NotNil)

m.i, m.err = nil, nil
_, isNull, err := cst.EvalInt(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalReal(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalDecimal(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalString(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalTime(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalDuration(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)
_, isNull, err = cst.EvalJSON(ctx, chunk.Row{})
c.Assert(err, IsNil)
c.Assert(isNull, IsTrue)

m.i = int64(2333)
xInt, _, _ := cst.EvalInt(ctx, chunk.Row{})
c.Assert(xInt, Equals, int64(2333))

m.i = float64(123.45)
xFlo, _, _ := cst.EvalReal(ctx, chunk.Row{})
c.Assert(xFlo, Equals, float64(123.45))

m.i = "abc"
xStr, _, _ := cst.EvalString(ctx, chunk.Row{})
c.Assert(xStr, Equals, "abc")

m.i = &types.MyDecimal{}
xDec, _, _ := cst.EvalDecimal(ctx, chunk.Row{})
c.Assert(xDec.Compare(m.i.(*types.MyDecimal)), Equals, 0)

m.i = types.Time{}
xTim, _, _ := cst.EvalTime(ctx, chunk.Row{})
c.Assert(xTim.Compare(m.i.(types.Time)), Equals, 0)

m.i = types.Duration{}
xDur, _, _ := cst.EvalDuration(ctx, chunk.Row{})
c.Assert(xDur.Compare(m.i.(types.Duration)), Equals, 0)

m.i = json.BinaryJSON{}
xJsn, _, _ := cst.EvalJSON(ctx, chunk.Row{})
c.Assert(m.i.(json.BinaryJSON).String(), Equals, xJsn.String())

cln := cst.Clone().(*Constant)
c.Assert(cln.DeferredExpr, Equals, cst.DeferredExpr)
}
Loading

0 comments on commit ae58fd0

Please sign in to comment.