github.com/dolthub/go-mysql-server@v0.18.0/sql/types/null_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  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestNullCompare(t *testing.T) {
    27  	tests := []struct {
    28  		val1 interface{}
    29  		val2 interface{}
    30  	}{
    31  		{true, 1},
    32  		{"blah", nil},
    33  		{time.Now(), []byte{0}},
    34  	}
    35  
    36  	for _, test := range tests {
    37  		t.Run(fmt.Sprintf("%v %v", test.val1, test.val2), func(t *testing.T) {
    38  			cmp, err := Null.Compare(test.val1, test.val2)
    39  			require.NoError(t, err)
    40  			assert.Equal(t, 0, cmp)
    41  		})
    42  	}
    43  }
    44  
    45  func TestNullConvert(t *testing.T) {
    46  	tests := []struct {
    47  		val         interface{}
    48  		expectedVal interface{}
    49  		expectedErr bool
    50  	}{
    51  		{nil, nil, false},
    52  		{int(0), nil, true},
    53  		{int8(0), nil, true},
    54  		{int16(0), nil, true},
    55  		{int32(0), nil, true},
    56  		{int64(0), nil, true},
    57  		{uint(0), nil, true},
    58  		{uint8(0), nil, true},
    59  		{uint16(0), nil, true},
    60  		{uint32(0), nil, true},
    61  		{uint64(0), nil, true},
    62  		{float32(0), nil, true},
    63  		{float64(0), nil, true},
    64  		{"stuff", nil, true},
    65  		{[]byte{0}, nil, true},
    66  		{time.Date(2019, 12, 12, 12, 12, 12, 0, time.UTC), nil, true},
    67  	}
    68  
    69  	for _, test := range tests {
    70  		t.Run(fmt.Sprintf("%v %v", test.val, test.expectedVal), func(t *testing.T) {
    71  			val, _, err := Null.Convert(test.val)
    72  			if test.expectedErr {
    73  				assert.Error(t, err)
    74  			} else {
    75  				require.NoError(t, err)
    76  				assert.Equal(t, test.expectedVal, val)
    77  			}
    78  		})
    79  	}
    80  }
    81  
    82  func TestNullString(t *testing.T) {
    83  	require.Equal(t, "null", Null.String())
    84  }