// Copyright 2015 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // See the License for the specific language governing permissions and // limitations under the License. package column import ( "testing" . "github.com/pingcap/check" "github.com/pingcap/tidb/model" "github.com/pingcap/tidb/mysql" "github.com/pingcap/tidb/util/types" ) var _ = Suite(&testColumnSuite{}) func TestT(t *testing.T) { TestingT(t) } type testColumnSuite struct{} func (s *testColumnSuite) TestString(c *C) { col := &Col{ model.ColumnInfo{ FieldType: *types.NewFieldType(mysql.TypeTiny), }, } col.Flen = 2 col.Decimal = 1 col.Charset = mysql.DefaultCharset col.Collate = mysql.DefaultCollationName col.Flag |= mysql.ZerofillFlag | mysql.UnsignedFlag | mysql.BinaryFlag | mysql.AutoIncrementFlag | mysql.NotNullFlag cs := col.String() c.Assert(len(cs), Greater, 0) col.Tp = mysql.TypeEnum col.Flag = 0 col.Elems = []string{"a", "b"} c.Assert(col.GetTypeDesc(), Equals, "enum('a','b')") col.Elems = []string{"'a'", "b"} c.Assert(col.GetTypeDesc(), Equals, "enum('''a''','b')") col.Tp = mysql.TypeFloat col.Flen = 8 col.Decimal = -1 c.Assert(col.GetTypeDesc(), Equals, "float") col.Decimal = 1 c.Assert(col.GetTypeDesc(), Equals, "float(8,1)") col.Tp = mysql.TypeDatetime col.Decimal = 6 c.Assert(col.GetTypeDesc(), Equals, "datetime(6)") col.Decimal = 0 c.Assert(col.GetTypeDesc(), Equals, "datetime") col.Decimal = -1 c.Assert(col.GetTypeDesc(), Equals, "datetime") } func (s *testColumnSuite) TestFind(c *C) { cols := []*Col{ newCol("a"), newCol("b"), newCol("c"), } FindCols(cols, []string{"a"}) FindCols(cols, []string{"d"}) cols[0].Flag |= mysql.OnUpdateNowFlag FindOnUpdateCols(cols) } func (s *testColumnSuite) TestCheck(c *C) { col := newCol("a") col.Flag = mysql.AutoIncrementFlag cols := []*Col{col, col} CheckOnce(cols) cols = cols[:1] CheckNotNull(cols, []interface{}{nil}) cols[0].Flag |= mysql.NotNullFlag CheckNotNull(cols, []interface{}{nil}) } func (s *testColumnSuite) TestDesc(c *C) { col := newCol("a") col.Flag = mysql.AutoIncrementFlag | mysql.NotNullFlag | mysql.PriKeyFlag NewColDesc(col) col.Flag = mysql.MultipleKeyFlag NewColDesc(col) ColDescFieldNames(false) ColDescFieldNames(true) } func newCol(name string) *Col { return &Col{ model.ColumnInfo{ Name: model.NewCIStr(name), }, } }