github.com/dolthub/go-mysql-server@v0.18.0/sql/errors_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/dolthub/vitess/go/mysql"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestSQLErrorCast(t *testing.T) {
    14  
    15  	tests := []struct {
    16  		err  error
    17  		code int
    18  	}{
    19  		{ErrTableNotFound.New("table not found err"), mysql.ERNoSuchTable},
    20  		{ErrInvalidType.New("unhandled mysql error"), mysql.ERUnknownError},
    21  		{fmt.Errorf("generic error"), mysql.ERUnknownError},
    22  		{nil, mysql.ERUnknownError},
    23  	}
    24  
    25  	for _, test := range tests {
    26  		var nilErr *mysql.SQLError = nil
    27  		t.Run(fmt.Sprintf("%v %v", test.err, test.code), func(t *testing.T) {
    28  			err := CastSQLError(test.err)
    29  			if err != nil {
    30  				require.Error(t, err)
    31  				assert.Equal(t, err.Number(), test.code)
    32  			} else {
    33  				assert.Equal(t, err, nilErr)
    34  			}
    35  		})
    36  	}
    37  }