github.com/dolthub/go-mysql-server@v0.18.0/sql/collations_test.go (about) 1 // Copyright 2020-2021 Dolthub, 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sql 16 17 import ( 18 "fmt" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestParseCollation(t *testing.T) { 26 tests := []struct { 27 charset string 28 collation string 29 binaryAttribute bool 30 expectedCollation CollationID 31 expectedErr bool 32 }{ 33 {"", "", false, Collation_Unspecified, false}, 34 {"", "", true, Collation_Unspecified, false}, 35 {CharacterSet_big5.String(), "", false, CharacterSet_big5.DefaultCollation(), false}, 36 {CharacterSet_eucjpms.String(), "", true, CharacterSet_eucjpms.BinaryCollation(), false}, 37 {"", Collation_big5_chinese_ci.String(), false, Collation_big5_chinese_ci, false}, 38 {"", Collation_armscii8_general_ci.String(), true, Collation_armscii8_bin, false}, 39 {CharacterSet_sjis.String(), Collation_sjis_japanese_ci.String(), false, Collation_sjis_japanese_ci, false}, 40 {CharacterSet_gbk.String(), Collation_gbk_chinese_ci.String(), true, Collation_gbk_chinese_ci, false}, 41 42 {CharacterSet_armscii8.String(), Collation_cp1251_bin.String(), false, Collation_Default, true}, 43 {CharacterSet_eucjpms.String(), Collation_latin5_turkish_ci.String(), false, Collation_Default, true}, 44 {CharacterSet_binary.String(), Collation_utf8_bin.String(), false, Collation_Default, true}, 45 } 46 47 for _, test := range tests { 48 if test.charset == "" { 49 testParseCollation(t, nil, &test.collation, test.binaryAttribute, test.expectedCollation, test.expectedErr) 50 } 51 if test.collation == "" { 52 testParseCollation(t, &test.charset, nil, test.binaryAttribute, test.expectedCollation, test.expectedErr) 53 } 54 if test.charset == "" && test.collation == "" { 55 testParseCollation(t, nil, nil, test.binaryAttribute, test.expectedCollation, test.expectedErr) 56 } 57 testParseCollation(t, &test.charset, &test.collation, test.binaryAttribute, test.expectedCollation, test.expectedErr) 58 } 59 } 60 61 func testParseCollation(t *testing.T, charset *string, collation *string, binaryAttribute bool, expectedCollation CollationID, expectedErr bool) { 62 t.Run(fmt.Sprintf("%v %v %v", charset, collation, binaryAttribute), func(t *testing.T) { 63 col, err := ParseCollation(charset, collation, binaryAttribute) 64 if expectedErr { 65 assert.Error(t, err) 66 } else { 67 require.NoError(t, err) 68 assert.True(t, expectedCollation.Equals(col)) 69 } 70 }) 71 }