github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/quotes/quotes_test.go (about) 1 // Copyright 2020 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 quotes 15 16 import ( 17 "testing" 18 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestQuoteSchema(t *testing.T) { 23 t.Parallel() 24 25 cases := []struct { 26 schema string 27 table string 28 expected string 29 }{ 30 {"testdb", "t1", "`testdb`.`t1`"}, 31 {"test-db-1`2", "t`1", "`test-db-1``2`.`t``1`"}, 32 } 33 for _, testCase := range cases { 34 name := QuoteSchema(testCase.schema, testCase.table) 35 require.Equal(t, testCase.expected, name) 36 } 37 } 38 39 func TestQuoteName(t *testing.T) { 40 t.Parallel() 41 42 cases := []struct { 43 name string 44 expected string 45 }{ 46 {"tbl", "`tbl`"}, 47 {"t`bl", "`t``bl`"}, 48 {"t``bl", "`t````bl`"}, 49 {"", "``"}, 50 } 51 for _, testCase := range cases { 52 escaped := QuoteName(testCase.name) 53 require.Equal(t, testCase.expected, escaped) 54 } 55 } 56 57 func TestEscapeName(t *testing.T) { 58 t.Parallel() 59 60 cases := []struct { 61 name string 62 expected string 63 }{ 64 {"tbl", "tbl"}, 65 {"t`bl", "t``bl"}, 66 {"t``bl", "t````bl"}, 67 {"`", "``"}, 68 {"", ""}, 69 } 70 for _, testCase := range cases { 71 escaped := EscapeName(testCase.name) 72 require.Equal(t, testCase.expected, escaped) 73 } 74 }