vitess.io/vitess@v0.16.2/go/mysql/collations/integration/weight_string_test.go (about) 1 /* 2 Copyright 2021 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package integration 18 19 import ( 20 "os" 21 "path/filepath" 22 "sort" 23 "strings" 24 "testing" 25 26 "vitess.io/vitess/go/mysql/collations" 27 "vitess.io/vitess/go/mysql/collations/internal/charset" 28 "vitess.io/vitess/go/mysql/collations/internal/testutil" 29 "vitess.io/vitess/go/mysql/collations/remote" 30 ) 31 32 func TestFastIterators(t *testing.T) { 33 input := make([]byte, 128) 34 for n := range input { 35 input[n] = byte(n) 36 } 37 input[0] = 'A' 38 39 testRemoteWeights(t, nil, []testweight{ 40 {"utf8mb4_0900_as_cs", input}, 41 {"utf8mb4_0900_as_ci", input}, 42 {"utf8mb4_0900_ai_ci", input}, 43 }) 44 } 45 46 func TestWeightStringsComprehensive(t *testing.T) { 47 type collationsForCharset struct { 48 charset charset.Charset 49 locals []collations.Collation 50 remotes []*remote.Collation 51 } 52 var charsetMap = make(map[string]*collationsForCharset) 53 54 golden := &testutil.GoldenTest{} 55 if err := golden.DecodeFromFile("../testdata/wiki_416c626572742045696e737465696e.gob.gz"); err != nil { 56 t.Fatal(err) 57 } 58 59 conn := mysqlconn(t) 60 defer conn.Close() 61 62 allCollations := collations.Local().AllCollations() 63 sort.Slice(allCollations, func(i, j int) bool { 64 return allCollations[i].ID() < allCollations[j].ID() 65 }) 66 for _, coll := range allCollations { 67 cs := coll.Charset() 68 c4cs := charsetMap[cs.Name()] 69 if c4cs == nil { 70 c4cs = &collationsForCharset{charset: cs} 71 charsetMap[cs.Name()] = c4cs 72 } 73 74 c4cs.locals = append(c4cs.locals, coll) 75 c4cs.remotes = append(c4cs.remotes, remote.NewCollation(conn, coll.Name())) 76 } 77 78 var allCharsets []*collationsForCharset 79 for _, c4cs := range charsetMap { 80 allCharsets = append(allCharsets, c4cs) 81 } 82 sort.Slice(allCharsets, func(i, j int) bool { 83 return allCharsets[i].charset.Name() < allCharsets[j].charset.Name() 84 }) 85 86 for _, c4cs := range allCharsets { 87 var tested int 88 for _, goldencase := range golden.Cases { 89 text := []byte(string([]rune(string(goldencase.Text))[:64])) 90 if trans := verifyTranscoding(t, c4cs.locals[0], c4cs.remotes[0], text); trans != nil { 91 for i := range c4cs.locals { 92 verifyWeightString(t, c4cs.locals[i], c4cs.remotes[i], trans) 93 tested++ 94 } 95 } 96 } 97 t.Logf("%q: %d collations, %d test strings = %d tests", 98 c4cs.charset.Name(), len(c4cs.locals), tested, len(c4cs.locals)*tested) 99 } 100 } 101 102 func TestCJKWeightStrings(t *testing.T) { 103 conn := mysqlconn(t) 104 defer conn.Close() 105 106 allCollations := collations.Local().AllCollations() 107 testdata, _ := filepath.Glob("../internal/charset/testdata/*.txt") 108 for _, testfile := range testdata { 109 charset := filepath.Base(testfile) 110 charset = strings.TrimSuffix(charset, ".txt") 111 charset = charset[strings.LastIndexByte(charset, '-')+1:] 112 113 var valid []collations.Collation 114 for _, coll := range allCollations { 115 if coll.Charset().Name() == charset { 116 valid = append(valid, coll) 117 t.Logf("%s -> %s", testfile, coll.Name()) 118 } 119 } 120 if len(valid) == 0 { 121 continue 122 } 123 text, err := os.ReadFile(testfile) 124 if err != nil { 125 t.Fatal(err) 126 } 127 for _, coll := range valid { 128 verifyWeightString(t, coll, remote.NewCollation(conn, coll.Name()), text) 129 } 130 } 131 }