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

     1  // Copyright 2024 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 function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/shopspring/decimal"
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/expression"
    25  	"github.com/dolthub/go-mysql-server/sql/types"
    26  )
    27  
    28  func TestChar(t *testing.T) {
    29  	tests := []struct {
    30  		name string
    31  		args []sql.Expression
    32  		exp  interface{}
    33  		err  bool
    34  		skip bool
    35  	}{
    36  		{
    37  			name: "null",
    38  			args: []sql.Expression{
    39  				nil,
    40  			},
    41  			exp: []byte{},
    42  		},
    43  		{
    44  			name: "null literal",
    45  			args: []sql.Expression{
    46  				expression.NewLiteral(nil, types.Null),
    47  			},
    48  			exp: []byte{},
    49  		},
    50  		{
    51  			name: "nulls are skipped",
    52  			args: []sql.Expression{
    53  				expression.NewLiteral(int32(1), types.Int32),
    54  				expression.NewLiteral(nil, types.Null),
    55  				expression.NewLiteral(int32(300), types.Int32),
    56  				expression.NewLiteral(int32(4000), types.Int32),
    57  			},
    58  			exp: []byte{0x1, 0x01, 0x2c, 0xf, 0xa0},
    59  		},
    60  		{
    61  			name: "-1",
    62  			args: []sql.Expression{
    63  				expression.NewLiteral(int32(-1), types.Int32),
    64  			},
    65  			exp: []byte{0xff, 0xff, 0xff, 0xff},
    66  		},
    67  		{
    68  			name: "256",
    69  			args: []sql.Expression{
    70  				expression.NewLiteral(int32(256), types.Int32),
    71  			},
    72  			exp: []byte{0x1, 0x0},
    73  		},
    74  		{
    75  			name: "512",
    76  			args: []sql.Expression{
    77  				expression.NewLiteral(int32(512), types.Int32),
    78  			},
    79  			exp: []byte{0x2, 0x0},
    80  		},
    81  		{
    82  			name: "256 * 256",
    83  			args: []sql.Expression{
    84  				expression.NewLiteral(int32(256*256), types.Int32),
    85  			},
    86  			exp: []byte{0x1, 0x0, 0x0},
    87  		},
    88  		{
    89  			name: "1 2 3 4",
    90  			args: []sql.Expression{
    91  				expression.NewLiteral(int32(1), types.Int32),
    92  				expression.NewLiteral(int32(2), types.Int32),
    93  				expression.NewLiteral(int32(3), types.Int32),
    94  				expression.NewLiteral(int32(4), types.Int32),
    95  			},
    96  			exp: []byte{0x1, 0x2, 0x3, 0x4},
    97  		},
    98  		{
    99  			name: "1 20 300 4000",
   100  			args: []sql.Expression{
   101  				expression.NewLiteral(int32(1), types.Int32),
   102  				expression.NewLiteral(int32(20), types.Int32),
   103  				expression.NewLiteral(int32(300), types.Int32),
   104  				expression.NewLiteral(int32(4000), types.Int32),
   105  			},
   106  			exp: []byte{0x1, 0x14, 0x1, 0x2c, 0xf, 0xa0},
   107  		},
   108  		{
   109  			name: "float32 1.99",
   110  			args: []sql.Expression{
   111  				expression.NewLiteral(float32(1.99), types.Float32),
   112  			},
   113  			exp: []byte{0x2},
   114  		},
   115  		{
   116  			name: "float64 1.99",
   117  			args: []sql.Expression{
   118  				expression.NewLiteral(1.99, types.Float64),
   119  			},
   120  			exp: []byte{0x2},
   121  		},
   122  		{
   123  			name: "decimal 1.99",
   124  			args: []sql.Expression{
   125  				expression.NewLiteral(decimal.NewFromFloat(1.99), types.DecimalType_{}),
   126  			},
   127  			exp: []byte{0x2},
   128  		},
   129  		{
   130  			name: "good string",
   131  			args: []sql.Expression{
   132  				expression.NewLiteral("12", types.Text),
   133  			},
   134  			exp: []byte{0x0C},
   135  		},
   136  		{
   137  			name: "bad string",
   138  			args: []sql.Expression{
   139  				expression.NewLiteral("abc", types.Text),
   140  			},
   141  			exp: []byte{0x0},
   142  		},
   143  		{
   144  			name: "mix types",
   145  			args: []sql.Expression{
   146  				expression.NewLiteral(1, types.Int32),
   147  				expression.NewLiteral(9999, types.Int32),
   148  				expression.NewLiteral(1.23, types.Int32),
   149  				expression.NewLiteral("78", types.Text),
   150  				expression.NewLiteral("abc", types.Text),
   151  			},
   152  			exp: []byte{0x01, 0x27, 0x0F, 0x01, 0x4E, 0x0},
   153  		},
   154  	}
   155  
   156  	for _, tt := range tests {
   157  		t.Run(tt.name, func(t *testing.T) {
   158  			if tt.skip {
   159  				t.Skip()
   160  			}
   161  
   162  			ctx := sql.NewEmptyContext()
   163  			f, err := NewChar(tt.args...)
   164  			require.NoError(t, err)
   165  
   166  			res, err := f.Eval(ctx, nil)
   167  			if tt.err {
   168  				require.Error(t, err)
   169  				return
   170  			}
   171  
   172  			require.NoError(t, err)
   173  			require.Equal(t, tt.exp, res)
   174  		})
   175  	}
   176  }