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

     1  // Copyright 2023 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 sqle
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/dolthub/vitess/go/vt/proto/query"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/expression"
    26  	"github.com/dolthub/go-mysql-server/sql/types"
    27  )
    28  
    29  func TestBindingsToExprs(t *testing.T) {
    30  	type tc struct {
    31  		Name     string
    32  		Bindings map[string]*query.BindVariable
    33  		Result   map[string]sql.Expression
    34  		Err      bool
    35  	}
    36  
    37  	cases := []tc{
    38  		{
    39  			"Empty",
    40  			map[string]*query.BindVariable{},
    41  			map[string]sql.Expression{},
    42  			false,
    43  		},
    44  		{
    45  			"BadInt",
    46  			map[string]*query.BindVariable{
    47  				"v1": &query.BindVariable{Type: query.Type_INT8, Value: []byte("axqut")},
    48  			},
    49  			nil,
    50  			true,
    51  		},
    52  		{
    53  			"BadUint",
    54  			map[string]*query.BindVariable{
    55  				"v1": &query.BindVariable{Type: query.Type_UINT8, Value: []byte("-12")},
    56  			},
    57  			nil,
    58  			true,
    59  		},
    60  		{
    61  			"BadDecimal",
    62  			map[string]*query.BindVariable{
    63  				"v1": &query.BindVariable{Type: query.Type_DECIMAL, Value: []byte("axqut")},
    64  			},
    65  			nil,
    66  			true,
    67  		},
    68  		{
    69  			"BadBit",
    70  			map[string]*query.BindVariable{
    71  				"v1": &query.BindVariable{Type: query.Type_BIT, Value: []byte{byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0), byte(0)}},
    72  			},
    73  			nil,
    74  			true,
    75  		},
    76  		{
    77  			"BadDate",
    78  			map[string]*query.BindVariable{
    79  				"v1": &query.BindVariable{Type: query.Type_DATE, Value: []byte("00000000")},
    80  			},
    81  			nil,
    82  			true,
    83  		},
    84  		{
    85  			"BadYear",
    86  			map[string]*query.BindVariable{
    87  				"v1": &query.BindVariable{Type: query.Type_YEAR, Value: []byte("asdf")},
    88  			},
    89  			nil,
    90  			true,
    91  		},
    92  		{
    93  			"BadDatetime",
    94  			map[string]*query.BindVariable{
    95  				"v1": &query.BindVariable{Type: query.Type_DATETIME, Value: []byte("0000")},
    96  			},
    97  			nil,
    98  			true,
    99  		},
   100  		{
   101  			"BadTimestamp",
   102  			map[string]*query.BindVariable{
   103  				"v1": &query.BindVariable{Type: query.Type_TIMESTAMP, Value: []byte("0000")},
   104  			},
   105  			nil,
   106  			true,
   107  		},
   108  		{
   109  			"SomeTypes",
   110  			map[string]*query.BindVariable{
   111  				"i8":        &query.BindVariable{Type: query.Type_INT8, Value: []byte("12")},
   112  				"u64":       &query.BindVariable{Type: query.Type_UINT64, Value: []byte("4096")},
   113  				"bin":       &query.BindVariable{Type: query.Type_VARBINARY, Value: []byte{byte(0xC0), byte(0x00), byte(0x10)}},
   114  				"text":      &query.BindVariable{Type: query.Type_TEXT, Value: []byte("four score and seven years ago...")},
   115  				"bit":       &query.BindVariable{Type: query.Type_BIT, Value: []byte{byte(0x0f)}},
   116  				"date":      &query.BindVariable{Type: query.Type_DATE, Value: []byte("2020-10-20")},
   117  				"year":      &query.BindVariable{Type: query.Type_YEAR, Value: []byte("2020")},
   118  				"datetime":  &query.BindVariable{Type: query.Type_DATETIME, Value: []byte("2020-10-20T12:00:00Z")},
   119  				"timestamp": &query.BindVariable{Type: query.Type_TIMESTAMP, Value: []byte("2020-10-20T12:00:00Z")},
   120  			},
   121  			map[string]sql.Expression{
   122  				"i8":        expression.NewLiteral(int64(12), types.Int64),
   123  				"u64":       expression.NewLiteral(uint64(4096), types.Uint64),
   124  				"bin":       expression.NewLiteral([]byte{byte(0xC0), byte(0x00), byte(0x10)}, types.MustCreateBinary(query.Type_VARBINARY, int64(3))),
   125  				"text":      expression.NewLiteral("four score and seven years ago...", types.MustCreateStringWithDefaults(query.Type_TEXT, 33)),
   126  				"bit":       expression.NewLiteral(uint64(0x0f), types.MustCreateBitType(types.BitTypeMaxBits)),
   127  				"date":      expression.NewLiteral(time.Date(2020, time.Month(10), 20, 0, 0, 0, 0, time.UTC), types.Date),
   128  				"year":      expression.NewLiteral(int16(2020), types.Year),
   129  				"datetime":  expression.NewLiteral(time.Date(2020, time.Month(10), 20, 12, 0, 0, 0, time.UTC), types.MustCreateDatetimeType(query.Type_DATETIME, 6)),
   130  				"timestamp": expression.NewLiteral(time.Date(2020, time.Month(10), 20, 12, 0, 0, 0, time.UTC), types.MustCreateDatetimeType(query.Type_TIMESTAMP, 6)),
   131  			},
   132  			false,
   133  		},
   134  	}
   135  
   136  	for _, c := range cases {
   137  		t.Run(c.Name, func(t *testing.T) {
   138  			res, err := bindingsToExprs(c.Bindings)
   139  			if !c.Err {
   140  				require.NoError(t, err)
   141  				require.Equal(t, c.Result, res)
   142  			} else {
   143  				require.Error(t, err, "%v", res)
   144  			}
   145  		})
   146  	}
   147  }