github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/changefeedccl/name_test.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Licensed as a CockroachDB Enterprise file under the Cockroach Community
     4  // License (the "License"); you may not use this file except in compliance with
     5  // the License. You may obtain a copy of the License at
     6  //
     7  //     https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
     8  
     9  package changefeedccl
    10  
    11  import (
    12  	"testing"
    13  	"unicode/utf8"
    14  
    15  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestSQLNameToKafkaName(t *testing.T) {
    20  	defer leaktest.AfterTest(t)()
    21  
    22  	tests := []struct {
    23  		sql, kafka string
    24  	}{
    25  		{`foo`, `foo`},
    26  		{`abcdefghijklmnopqrstuvwxyz`, `abcdefghijklmnopqrstuvwxyz`},
    27  		{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, `ABCDEFGHIJKLMNOPQRSTUVWXYZ`},
    28  		{`0123456789_-.`, `0123456789_-.`},
    29  		{`!`, `_u0021_`},
    30  		{`!@#$%^&*()`, `_u0021__u0040__u0023__u0024__u0025__u005e__u0026__u002a__u0028__u0029_`},
    31  		{`foo!`, `foo_u0021_`},
    32  		{`!bar`, `_u0021_bar`},
    33  		{`foo!bar`, `foo_u0021_bar`},
    34  		{`foo_u0021_bar`, `foo_u005f__u0075__u0030__u0030__u0032__u0031__u005f_bar`},
    35  		{`/`, `_u002f_`},
    36  		{`☃`, `_u2603_`},
    37  		{"\x00", `_u0000_`},
    38  		{string(utf8.RuneSelf), `_u0080_`},
    39  		{string(utf8.MaxRune), `_u0010ffff_`},
    40  		// special case: exact match of . and .. are disallowed by kafka
    41  		{`.`, `_u002e_`},
    42  		{`..`, `_u002e__u002e_`},
    43  	}
    44  	for i, test := range tests {
    45  		if k := SQLNameToKafkaName(test.sql); k != test.kafka {
    46  			t.Errorf(`%d: %s did not escape to %s got %s`, i, test.sql, test.kafka, k)
    47  		}
    48  		if s := KafkaNameToSQLName(test.kafka); s != test.sql {
    49  			t.Errorf(`%d: %s did not unescape to %s got %s`, i, test.kafka, test.sql, s)
    50  		}
    51  	}
    52  	// We don't produce capital letters in escapes but check them anyway.
    53  	require.Equal(t, `/`, KafkaNameToSQLName(`_u2F_`))
    54  }
    55  
    56  func TestSQLNameToAvroName(t *testing.T) {
    57  	defer leaktest.AfterTest(t)()
    58  
    59  	tests := []struct {
    60  		sql, avro string
    61  	}{
    62  		{`foo`, `foo`},
    63  		{`abcdefghijklmnopqrstuvwxyz`, `abcdefghijklmnopqrstuvwxyz`},
    64  		{`ABCDEFGHIJKLMNOPQRSTUVWXYZ`, `ABCDEFGHIJKLMNOPQRSTUVWXYZ`},
    65  		// special case: avro disallows 0-9 in the first character, but allows them otherwise
    66  		{`0123456789_-.`, `_u0030_123456789__u002d__u002e_`},
    67  		{`99`, `_u0039_9`},
    68  		{`!`, `_u0021_`},
    69  		{`!@#$%^&*()`, `_u0021__u0040__u0023__u0024__u0025__u005e__u0026__u002a__u0028__u0029_`},
    70  		{`foo!`, `foo_u0021_`},
    71  		{`!bar`, `_u0021_bar`},
    72  		{`foo!bar`, `foo_u0021_bar`},
    73  		{`foo_u0021_bar`, `foo_u005f__u0075__u0030__u0030__u0032__u0031__u005f_bar`},
    74  		{`/`, `_u002f_`},
    75  		{`☃`, `_u2603_`},
    76  		{"\x00", `_u0000_`},
    77  		{string(utf8.RuneSelf), `_u0080_`},
    78  		{string(utf8.MaxRune), `_u0010ffff_`},
    79  	}
    80  	for i, test := range tests {
    81  		if a := SQLNameToAvroName(test.sql); a != test.avro {
    82  			t.Errorf(`%d: %s did not escape to %s got %s`, i, test.sql, test.avro, a)
    83  		}
    84  		if s := AvroNameToSQLName(test.avro); s != test.sql {
    85  			t.Errorf(`%d: %s did not unescape to %s got %s`, i, test.avro, test.sql, s)
    86  		}
    87  	}
    88  	// We don't produce capital letters in escapes but check them anyway.
    89  	require.Equal(t, `/`, KafkaNameToSQLName(`_u2F_`))
    90  }