github.com/XiaoMi/Gaea@v1.2.5/mysql/charset_tidb_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  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  
    21  	"github.com/XiaoMi/Gaea/util/testleak"
    22  )
    23  
    24  func TestT(t *testing.T) {
    25  	check.CustomVerboseFlag = true
    26  	check.TestingT(t)
    27  }
    28  
    29  var _ = check.Suite(&testCharsetSuite{})
    30  
    31  type testCharsetSuite struct {
    32  }
    33  
    34  func testValidCharset(c *check.C, charset string, collation string, expect bool) {
    35  	b := ValidCharsetAndCollation(charset, collation)
    36  	c.Assert(b, check.Equals, expect)
    37  }
    38  
    39  func (s *testCharsetSuite) TestValidCharset(c *check.C) {
    40  	defer testleak.AfterTest(c)()
    41  	tests := []struct {
    42  		cs   string
    43  		co   string
    44  		succ bool
    45  	}{
    46  		{"utf8", "utf8_general_ci", true},
    47  		{"", "utf8_general_ci", true},
    48  		{"utf8mb4", "utf8mb4_bin", true},
    49  		{"latin1", "latin1_bin", true},
    50  		{"utf8", "utf8_invalid_ci", false},
    51  		{"utf16", "utf16_bin", false},
    52  		{"gb2312", "gb2312_chinese_ci", false},
    53  		{"UTF8", "UTF8_BIN", true},
    54  		{"UTF8", "utf8_bin", true},
    55  		{"UTF8MB4", "utf8mb4_bin", true},
    56  		{"UTF8MB4", "UTF8MB4_bin", true},
    57  		{"UTF8MB4", "UTF8MB4_general_ci", true},
    58  		{"Utf8", "uTf8_bIN", true},
    59  	}
    60  	for _, tt := range tests {
    61  		testValidCharset(c, tt.cs, tt.co, tt.succ)
    62  	}
    63  }
    64  
    65  func (s *testCharsetSuite) TestGetAllCharsets(c *check.C) {
    66  	defer testleak.AfterTest(c)()
    67  	charset := &Charset{"test", "test_bin", nil, "Test", 5}
    68  	charsetInfos = append(charsetInfos, charset)
    69  	descs := GetAllCharsets()
    70  	c.Assert(len(descs), check.Equals, len(charsetInfos)-1)
    71  }
    72  
    73  func testGetDefaultCollation(c *check.C, charset string, expectCollation string, succ bool) {
    74  	b, err := GetDefaultCollation(charset)
    75  	if !succ {
    76  		c.Assert(err, check.NotNil)
    77  		return
    78  	}
    79  	c.Assert(b, check.Equals, expectCollation)
    80  }
    81  
    82  func (s *testCharsetSuite) TestGetDefaultCollation(c *check.C) {
    83  	defer testleak.AfterTest(c)()
    84  	tests := []struct {
    85  		cs   string
    86  		co   string
    87  		succ bool
    88  	}{
    89  		{"utf8", "utf8_bin", true},
    90  		{"UTF8", "utf8_bin", true},
    91  		{"utf8mb4", "utf8mb4_bin", true},
    92  		{"ascii", "ascii_bin", true},
    93  		{"binary", "binary", true},
    94  		{"latin1", "latin1_bin", true},
    95  		{"invalid_cs", "", false},
    96  		{"", "utf8_bin", false},
    97  	}
    98  	for _, tt := range tests {
    99  		testGetDefaultCollation(c, tt.cs, tt.co, tt.succ)
   100  	}
   101  }