github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/column/column_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 column 15 16 import ( 17 "testing" 18 19 . "github.com/insionng/yougam/libraries/pingcap/check" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/mysql" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/util/types" 24 ) 25 26 var _ = Suite(&testColumnSuite{}) 27 28 func TestT(t *testing.T) { 29 TestingT(t) 30 } 31 32 type testColumnSuite struct{} 33 34 func (s *testColumnSuite) TestString(c *C) { 35 defer testleak.AfterTest(c)() 36 col := &Col{ 37 model.ColumnInfo{ 38 FieldType: *types.NewFieldType(mysql.TypeTiny), 39 State: model.StatePublic, 40 }, 41 } 42 col.Flen = 2 43 col.Decimal = 1 44 col.Charset = mysql.DefaultCharset 45 col.Collate = mysql.DefaultCollationName 46 col.Flag |= mysql.ZerofillFlag | mysql.UnsignedFlag | mysql.BinaryFlag | mysql.AutoIncrementFlag | mysql.NotNullFlag 47 48 cs := col.String() 49 c.Assert(len(cs), Greater, 0) 50 51 col.Tp = mysql.TypeEnum 52 col.Flag = 0 53 col.Elems = []string{"a", "b"} 54 55 c.Assert(col.GetTypeDesc(), Equals, "enum('a','b')") 56 57 col.Elems = []string{"'a'", "b"} 58 c.Assert(col.GetTypeDesc(), Equals, "enum('''a''','b')") 59 60 col.Tp = mysql.TypeFloat 61 col.Flen = 8 62 col.Decimal = -1 63 c.Assert(col.GetTypeDesc(), Equals, "float") 64 65 col.Decimal = 1 66 c.Assert(col.GetTypeDesc(), Equals, "float(8,1)") 67 68 col.Tp = mysql.TypeDatetime 69 col.Decimal = 6 70 c.Assert(col.GetTypeDesc(), Equals, "datetime(6)") 71 72 col.Decimal = 0 73 c.Assert(col.GetTypeDesc(), Equals, "datetime") 74 75 col.Decimal = -1 76 c.Assert(col.GetTypeDesc(), Equals, "datetime") 77 } 78 79 func (s *testColumnSuite) TestFind(c *C) { 80 defer testleak.AfterTest(c)() 81 cols := []*Col{ 82 newCol("a"), 83 newCol("b"), 84 newCol("c"), 85 } 86 FindCols(cols, []string{"a"}) 87 FindCols(cols, []string{"d"}) 88 cols[0].Flag |= mysql.OnUpdateNowFlag 89 FindOnUpdateCols(cols) 90 } 91 92 func (s *testColumnSuite) TestCheck(c *C) { 93 defer testleak.AfterTest(c)() 94 col := newCol("a") 95 col.Flag = mysql.AutoIncrementFlag 96 cols := []*Col{col, col} 97 CheckOnce(cols) 98 cols = cols[:1] 99 CheckNotNull(cols, types.MakeDatums(nil)) 100 cols[0].Flag |= mysql.NotNullFlag 101 CheckNotNull(cols, types.MakeDatums(nil)) 102 } 103 104 func (s *testColumnSuite) TestDesc(c *C) { 105 defer testleak.AfterTest(c)() 106 col := newCol("a") 107 col.Flag = mysql.AutoIncrementFlag | mysql.NotNullFlag | mysql.PriKeyFlag 108 NewColDesc(col) 109 col.Flag = mysql.MultipleKeyFlag 110 NewColDesc(col) 111 ColDescFieldNames(false) 112 ColDescFieldNames(true) 113 } 114 115 func newCol(name string) *Col { 116 return &Col{ 117 model.ColumnInfo{ 118 Name: model.NewCIStr(name), 119 State: model.StatePublic, 120 }, 121 } 122 }