github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/evaluator/builtin_test.go (about)

     1  // Copyright 2015 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package evaluator
    15  
    16  import (
    17  	"reflect"
    18  
    19  	. "github.com/insionng/yougam/libraries/pingcap/check"
    20  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak"
    21  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/testutil"
    22  	"github.com/insionng/yougam/libraries/pingcap/tidb/util/types"
    23  )
    24  
    25  // tblToDtbl is a util function for test.
    26  func tblToDtbl(i interface{}) []map[string][]types.Datum {
    27  	l := reflect.ValueOf(i).Len()
    28  	tbl := make([]map[string][]types.Datum, l)
    29  	for j := 0; j < l; j++ {
    30  		v := reflect.ValueOf(i).Index(j).Interface()
    31  		val := reflect.ValueOf(v)
    32  		t := reflect.TypeOf(v)
    33  		item := make(map[string][]types.Datum, val.NumField())
    34  		for k := 0; k < val.NumField(); k++ {
    35  			tmp := val.Field(k).Interface()
    36  			item[t.Field(k).Name] = makeDatums(tmp)
    37  		}
    38  		tbl[j] = item
    39  	}
    40  	return tbl
    41  }
    42  
    43  func makeDatums(i interface{}) []types.Datum {
    44  	if i != nil {
    45  		t := reflect.TypeOf(i)
    46  		val := reflect.ValueOf(i)
    47  		switch t.Kind() {
    48  		case reflect.Slice:
    49  			l := val.Len()
    50  			res := make([]types.Datum, l)
    51  			for j := 0; j < l; j++ {
    52  				res[j] = types.NewDatum(val.Index(j).Interface())
    53  			}
    54  			return res
    55  		}
    56  	}
    57  	return types.MakeDatums(i)
    58  }
    59  
    60  func (s *testEvaluatorSuite) TestCoalesce(c *C) {
    61  	defer testleak.AfterTest(c)()
    62  	args := types.MakeDatums(1, nil)
    63  	v, err := builtinCoalesce(args, nil)
    64  	c.Assert(err, IsNil)
    65  	c.Assert(v, testutil.DatumEquals, types.NewDatum(1))
    66  
    67  	args = types.MakeDatums(nil, nil)
    68  	v, err = builtinCoalesce(args, nil)
    69  	c.Assert(err, IsNil)
    70  	c.Assert(v, testutil.DatumEquals, types.NewDatum(nil))
    71  }
    72  
    73  func (s *testEvaluatorSuite) TestIsNullFunc(c *C) {
    74  	defer testleak.AfterTest(c)()
    75  
    76  	v, err := builtinIsNull(types.MakeDatums(1), nil)
    77  	c.Assert(err, IsNil)
    78  	c.Assert(v.GetInt64(), Equals, int64(0))
    79  
    80  	v, err = builtinIsNull(types.MakeDatums(nil), nil)
    81  	c.Assert(err, IsNil)
    82  	c.Assert(v.GetInt64(), Equals, int64(1))
    83  }