github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/concat_ws_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 TestConcatWithSeparator(t *testing.T) {
    28  	t.Run("multiple arguments", func(t *testing.T) {
    29  		require := require.New(t)
    30  		f, err := NewConcatWithSeparator(expression.NewLiteral(",", types.LongText),
    31  			expression.NewLiteral("foo", types.LongText),
    32  			expression.NewLiteral(5, types.LongText),
    33  			expression.NewLiteral(true, types.Boolean),
    34  		)
    35  		require.NoError(err)
    36  
    37  		v, err := f.Eval(sql.NewEmptyContext(), nil)
    38  		require.NoError(err)
    39  		require.Equal("foo,5,1", v)
    40  	})
    41  
    42  	t.Run("some argument is empty", func(t *testing.T) {
    43  		require := require.New(t)
    44  		f, err := NewConcatWithSeparator(expression.NewLiteral(",", types.LongText),
    45  			expression.NewLiteral("foo", types.LongText),
    46  			expression.NewLiteral("", types.LongText),
    47  			expression.NewLiteral(true, types.Boolean),
    48  		)
    49  		require.NoError(err)
    50  
    51  		v, err := f.Eval(sql.NewEmptyContext(), nil)
    52  		require.NoError(err)
    53  		require.Equal("foo,,1", v)
    54  	})
    55  
    56  	t.Run("some argument is nil", func(t *testing.T) {
    57  		require := require.New(t)
    58  		f, err := NewConcatWithSeparator(expression.NewLiteral(",", types.LongText),
    59  			expression.NewLiteral("foo", types.LongText),
    60  			expression.NewLiteral(nil, types.LongText),
    61  			expression.NewLiteral(true, types.Boolean),
    62  		)
    63  		require.NoError(err)
    64  
    65  		v, err := f.Eval(sql.NewEmptyContext(), nil)
    66  		require.NoError(err)
    67  		require.Equal("foo,1", v)
    68  	})
    69  
    70  	t.Run("separator is nil", func(t *testing.T) {
    71  		require := require.New(t)
    72  		f, err := NewConcatWithSeparator(expression.NewLiteral(nil, types.LongText),
    73  			expression.NewLiteral("foo", types.LongText),
    74  			expression.NewLiteral(5, types.LongText),
    75  			expression.NewLiteral(true, types.Boolean),
    76  		)
    77  		require.NoError(err)
    78  
    79  		v, err := f.Eval(sql.NewEmptyContext(), nil)
    80  		require.NoError(err)
    81  		require.Equal(nil, v)
    82  	})
    83  }
    84  
    85  func TestNewConcatWithSeparator(t *testing.T) {
    86  	require := require.New(t)
    87  
    88  	_, err := NewConcatWithSeparator(expression.NewLiteral(nil, types.LongText))
    89  	require.NoError(err)
    90  
    91  	_, err = NewConcatWithSeparator(expression.NewLiteral(nil, types.LongText), expression.NewLiteral(nil, types.Int64))
    92  	require.NoError(err)
    93  
    94  	_, err = NewConcatWithSeparator(expression.NewLiteral(nil, types.LongText),
    95  		expression.NewLiteral(nil, types.Boolean),
    96  		expression.NewLiteral(nil, types.Int64),
    97  		expression.NewLiteral(nil, types.LongText),
    98  	)
    99  	require.NoError(err)
   100  }