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

     1  // Copyright 2020-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 function
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/dolthub/vitess/go/sqltypes"
    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 TestNullIf(t *testing.T) {
    29  	testCases := []struct {
    30  		ex1      interface{}
    31  		ex2      interface{}
    32  		expected interface{}
    33  	}{
    34  		{"foo", "bar", "foo"},
    35  		{"foo", "foo", nil},
    36  		{nil, "foo", nil},
    37  		{"foo", nil, "foo"},
    38  		{nil, nil, nil},
    39  		{"", nil, ""},
    40  	}
    41  
    42  	f := NewNullIf(
    43  		expression.NewGetField(0, types.LongText, "ex1", true),
    44  		expression.NewGetField(1, types.LongText, "ex2", true),
    45  	)
    46  	require.Equal(t, types.LongText, f.Type())
    47  
    48  	var3 := types.MustCreateStringWithDefaults(sqltypes.VarChar, 3)
    49  	f = NewNullIf(
    50  		expression.NewGetField(0, var3, "ex1", true),
    51  		expression.NewGetField(1, var3, "ex2", true),
    52  	)
    53  	require.Equal(t, var3, f.Type())
    54  
    55  	for _, tc := range testCases {
    56  		v, err := f.Eval(sql.NewEmptyContext(), sql.NewRow(tc.ex1, tc.ex2))
    57  		require.NoError(t, err)
    58  		require.Equal(t, tc.expected, v)
    59  	}
    60  }