github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/doltdb/table_test.go (about)

     1  // Copyright 2022 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 doltdb
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  type isValidIndexNameTest struct {
    24  	name      string
    25  	indexName string
    26  	valid     bool
    27  }
    28  
    29  var isValidIndexNameTests = []isValidIndexNameTest{
    30  	{
    31  		name:      "all valid ASCII characters",
    32  		indexName: " ~!@#$%^&*()_+`-=[]{}|;':\",./<>?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
    33  		valid:     true,
    34  	},
    35  	{
    36  		name:      "single char",
    37  		indexName: "_",
    38  		valid:     true,
    39  	},
    40  	{
    41  		name:      "backticks in prefix/suffix",
    42  		indexName: "``index``",
    43  		valid:     true,
    44  	},
    45  	{
    46  		name:      "backtick in middle",
    47  		indexName: "in`dex",
    48  		valid:     true,
    49  	},
    50  	{
    51  		name:      "numeric",
    52  		indexName: "1234",
    53  		valid:     true,
    54  	},
    55  	{
    56  		name:      "valid @ char",
    57  		indexName: "as@df",
    58  		valid:     true,
    59  	},
    60  	{
    61  		name:      "valid [] chars",
    62  		indexName: "[asdf]",
    63  		valid:     true,
    64  	},
    65  	{
    66  		name:      "spaces allowed",
    67  		indexName: "    s p a c e s    ",
    68  		valid:     true,
    69  	},
    70  	{
    71  		name:      "empty string",
    72  		indexName: "",
    73  		valid:     false,
    74  	},
    75  }
    76  
    77  func TestIsValidIndexName(t *testing.T) {
    78  	for _, test := range isValidIndexNameTests {
    79  		t.Run(test.name, func(t *testing.T) {
    80  			valid := IsValidIdentifier(test.indexName)
    81  			require.Equal(t, test.valid, valid)
    82  		})
    83  	}
    84  }