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

     1  // Copyright 2021 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 expression
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/dolthub/vitess/go/vt/proto/query"
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestBinary(t *testing.T) {
    28  	require := require.New(t)
    29  
    30  	// Validate Binary is reflexive
    31  	e := NewBinary(NewGetField(0, types.Text, "foo", true))
    32  	require.Equal(eval(t, e, sql.NewRow("hi")), eval(t, e, sql.NewRow("hi")))
    33  
    34  	// Go through assorted test cases
    35  	testCases := []struct {
    36  		val      interface{}
    37  		valType  sql.Type
    38  		expected []byte
    39  	}{
    40  		{"hi", types.MustCreateBinary(query.Type_VARBINARY, int64(16)), []byte("hi")},
    41  		{int8(1), types.Int8, []byte("1")},
    42  		{true, types.Boolean, []byte("1")},
    43  		{"hello", types.LongText, []byte("hello")},
    44  	}
    45  
    46  	for _, tt := range testCases {
    47  		f := NewBinary(NewLiteral(tt.val, tt.valType))
    48  		require.Equal(tt.expected, eval(t, f, sql.Row{nil}))
    49  	}
    50  
    51  	// Try with nil case
    52  	e = NewBinary(NewLiteral(nil, types.Null))
    53  	require.Equal(nil, eval(t, e, sql.Row{nil}))
    54  }