github.com/blend/go-sdk@v1.20220411.3/db/dbutil/validate_database_name_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package dbutil
     9  
    10  import (
    11  	"fmt"
    12  	"strings"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  	"github.com/blend/go-sdk/ex"
    17  )
    18  
    19  func Test_ValidateDatabaseName(t *testing.T) {
    20  	its := assert.New(t)
    21  
    22  	testCases := [...]struct {
    23  		Input string
    24  		Err   error
    25  	}{
    26  		{Input: "my_table"},
    27  		{Input: "my_2nd_table"},
    28  		{Input: "échéanciers"},
    29  		{Input: "", Err: ErrDatabaseNameEmpty},
    30  		{Input: strings.Repeat("a", DatabaseNameMaxLength+1), Err: ErrDatabaseNameTooLong},
    31  		{Input: "2nd_table", Err: ErrDatabaseNameInvalidFirstRune},
    32  		{Input: `"2nd_table"`, Err: ErrDatabaseNameInvalidFirstRune},
    33  		{Input: "invalid-charater", Err: ErrDatabaseNameInvalid},
    34  		{Input: "invalid'; DROP DB postgres; --", Err: ErrDatabaseNameInvalid},
    35  		{Input: "postgres", Err: ErrDatabaseNameReserved},
    36  		{Input: "template0", Err: ErrDatabaseNameReserved},
    37  		{Input: "template1", Err: ErrDatabaseNameReserved},
    38  		{Input: "defaultdb", Err: ErrDatabaseNameReserved},
    39  	}
    40  
    41  	var err error
    42  	for _, tc := range testCases {
    43  		err = ValidateDatabaseName(tc.Input)
    44  		if tc.Err != nil && err != nil {
    45  			its.Equal(tc.Err, ex.ErrClass(err))
    46  		} else if tc.Err == nil && err != nil {
    47  			its.FailNow(fmt.Sprintf("expected input %q not to produce an error, actual: %v", tc.Input, err))
    48  		}
    49  	}
    50  }