github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/ifnull_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/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestIfNull(t *testing.T) {
    28  	testCases := []struct {
    29  		expression interface{}
    30  		value      interface{}
    31  		expected   interface{}
    32  	}{
    33  		{"foo", "bar", "foo"},
    34  		{"foo", "foo", "foo"},
    35  		{nil, "foo", "foo"},
    36  		{"foo", nil, "foo"},
    37  		{nil, nil, nil},
    38  		{"", nil, ""},
    39  	}
    40  
    41  	f := NewIfNull(
    42  		expression.NewGetField(0, types.LongText, "expression", true),
    43  		expression.NewGetField(1, types.LongText, "value", true),
    44  	)
    45  	require.Equal(t, types.LongText, f.Type())
    46  
    47  	for _, tc := range testCases {
    48  		v, err := f.Eval(sql.NewEmptyContext(), sql.NewRow(tc.expression, tc.value))
    49  		require.NoError(t, err)
    50  		require.Equal(t, tc.expected, v)
    51  	}
    52  }