github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/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/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  )
    22  
    23  func Test(t *testing.T) { check.TestingT(t) }
    24  
    25  type quotesSuite struct{}
    26  
    27  var _ = check.Suite(&quotesSuite{})
    28  
    29  func (s *quotesSuite) TestQuoteSchema(c *check.C) {
    30  	defer testleak.AfterTest(c)()
    31  	cases := []struct {
    32  		schema   string
    33  		table    string
    34  		expected string
    35  	}{
    36  		{"testdb", "t1", "`testdb`.`t1`"},
    37  		{"test-db-1`2", "t`1", "`test-db-1``2`.`t``1`"},
    38  	}
    39  	for _, testCase := range cases {
    40  		name := QuoteSchema(testCase.schema, testCase.table)
    41  		c.Assert(name, check.Equals, testCase.expected)
    42  	}
    43  }
    44  
    45  func (s *quotesSuite) TestQuoteName(c *check.C) {
    46  	defer testleak.AfterTest(c)()
    47  	cases := []struct {
    48  		name     string
    49  		expected string
    50  	}{
    51  		{"tbl", "`tbl`"},
    52  		{"t`bl", "`t``bl`"},
    53  		{"t``bl", "`t````bl`"},
    54  		{"", "``"},
    55  	}
    56  	for _, testCase := range cases {
    57  		escaped := QuoteName(testCase.name)
    58  		c.Assert(escaped, check.Equals, testCase.expected)
    59  	}
    60  }
    61  
    62  func (s *quotesSuite) TestEscapeName(c *check.C) {
    63  	defer testleak.AfterTest(c)()
    64  	cases := []struct {
    65  		name     string
    66  		expected string
    67  	}{
    68  		{"tbl", "tbl"},
    69  		{"t`bl", "t``bl"},
    70  		{"t``bl", "t````bl"},
    71  		{"`", "``"},
    72  		{"", ""},
    73  	}
    74  	for _, testCase := range cases {
    75  		escaped := EscapeName(testCase.name)
    76  		c.Assert(escaped, check.Equals, testCase.expected)
    77  	}
    78  }