github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/mysql/set_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 mysql 15 16 import ( 17 . "github.com/insionng/yougam/libraries/pingcap/check" 18 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 19 ) 20 21 var _ = Suite(&testSetSuite{}) 22 23 type testSetSuite struct { 24 } 25 26 func (s *testSetSuite) TestSet(c *C) { 27 defer testleak.AfterTest(c)() 28 elems := []string{"a", "b", "c", "d"} 29 tbl := []struct { 30 Name string 31 ExpectedValue uint64 32 ExpectedName string 33 }{ 34 {"a", 1, "a"}, 35 {"a,b,a", 3, "a,b"}, 36 {"b,a", 3, "a,b"}, 37 {"a,b,c,d", 15, "a,b,c,d"}, 38 {"d", 8, "d"}, 39 {"", 0, ""}, 40 {"0", 0, ""}, 41 } 42 43 for _, t := range tbl { 44 e, err := ParseSetName(elems, t.Name) 45 c.Assert(err, IsNil) 46 c.Assert(e.ToNumber(), Equals, float64(t.ExpectedValue)) 47 c.Assert(e.String(), Equals, t.ExpectedName) 48 } 49 50 tblNumber := []struct { 51 Number uint64 52 ExpectedName string 53 }{ 54 {0, ""}, 55 {1, "a"}, 56 {3, "a,b"}, 57 {9, "a,d"}, 58 } 59 60 for _, t := range tblNumber { 61 e, err := ParseSetValue(elems, t.Number) 62 c.Assert(err, IsNil) 63 c.Assert(e.String(), Equals, t.ExpectedName) 64 c.Assert(e.ToNumber(), Equals, float64(t.Number)) 65 } 66 67 tblErr := []string{ 68 "a.e", 69 "e.f", 70 } 71 for _, t := range tblErr { 72 _, err := ParseSetName(elems, t) 73 c.Assert(err, NotNil) 74 } 75 76 tblNumberErr := []uint64{ 77 100, 16, 64, 78 } 79 for _, t := range tblNumberErr { 80 _, err := ParseSetValue(elems, t) 81 c.Assert(err, NotNil) 82 } 83 }