github.com/dolthub/go-mysql-server@v0.18.0/sql/types/tuple_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 types
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	"github.com/dolthub/vitess/go/sqltypes"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"github.com/dolthub/go-mysql-server/sql"
    26  )
    27  
    28  func TestTuple(t *testing.T) {
    29  	require := require.New(t)
    30  
    31  	typ := CreateTuple(Int32, LongText, Int64)
    32  	_, _, err := typ.Convert("foo")
    33  	require.Error(err)
    34  	require.True(sql.ErrNotTuple.Is(err))
    35  
    36  	_, _, err = typ.Convert([]interface{}{1, 2})
    37  	require.Error(err)
    38  	require.True(sql.ErrInvalidColumnNumber.Is(err))
    39  
    40  	conVal, _, err := typ.Convert([]interface{}{1, 2, 3})
    41  	require.NoError(err)
    42  	assert.Equal(t, []interface{}{int32(1), "2", int64(3)}, conVal)
    43  
    44  	_, err = typ.SQL(sql.NewEmptyContext(), nil, nil)
    45  	require.Error(err)
    46  
    47  	require.Equal(sqltypes.Expression, typ.Type())
    48  
    49  	comparisons := []struct {
    50  		val1        []interface{}
    51  		val2        []interface{}
    52  		expectedCmp int
    53  	}{
    54  		{[]interface{}{1, 2, 3}, []interface{}{2, 2, 3}, -1},
    55  		{[]interface{}{1, 2, 3}, []interface{}{1, 3, 3}, -1},
    56  		{[]interface{}{1, 2, 3}, []interface{}{1, 2, 4}, -1},
    57  		{[]interface{}{1, 2, 3}, []interface{}{1, 2, 3}, 0},
    58  		{[]interface{}{2, 2, 3}, []interface{}{1, 2, 3}, 1},
    59  		{[]interface{}{1, 3, 3}, []interface{}{1, 2, 3}, 1},
    60  		{[]interface{}{1, 2, 4}, []interface{}{1, 2, 3}, 1},
    61  	}
    62  
    63  	for _, comparison := range comparisons {
    64  		t.Run(fmt.Sprintf("%v %v", comparison.val1, comparison.val2), func(t *testing.T) {
    65  			cmp, err := typ.Compare(comparison.val1, comparison.val2)
    66  			require.NoError(err)
    67  			assert.Equal(t, comparison.expectedCmp, cmp)
    68  		})
    69  	}
    70  }