github.com/dolthub/go-mysql-server@v0.18.0/sql/types/year_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  	"reflect"
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestYearCompare(t *testing.T) {
    28  	tests := []struct {
    29  		val1        interface{}
    30  		val2        interface{}
    31  		expectedCmp int
    32  	}{
    33  		{nil, 0, 1},
    34  		{0, nil, -1},
    35  		{nil, nil, 0},
    36  		{1, 70, 1},
    37  		{80, 30, -1},
    38  		{0, "0", -1},
    39  		{2050, 50, 0},
    40  		{"2050", "2050", 0},
    41  		{10, time.Date(2010, 1, 2, 3, 4, 5, 0, time.UTC), 0},
    42  	}
    43  
    44  	for _, test := range tests {
    45  		t.Run(fmt.Sprintf("%v %v", test.val1, test.val2), func(t *testing.T) {
    46  			cmp, err := Year.Compare(test.val1, test.val2)
    47  			require.NoError(t, err)
    48  			assert.Equal(t, test.expectedCmp, cmp)
    49  		})
    50  	}
    51  }
    52  
    53  func TestYearConvert(t *testing.T) {
    54  	tests := []struct {
    55  		val         interface{}
    56  		expectedVal interface{}
    57  		expectedErr bool
    58  	}{
    59  		{int(0), int16(0), false},
    60  		{uint(1), int16(2001), false},
    61  		{int8(31), int16(2031), false},
    62  		{uint8(32), int16(2032), false},
    63  		{int16(69), int16(2069), false},
    64  		{uint16(70), int16(1970), false},
    65  		{uint16(99), int16(1999), false},
    66  		{int32(1901), int16(1901), false},
    67  		{uint32(2000), int16(2000), false},
    68  		{int64(2100), int16(2100), false},
    69  		{uint64(2155), int16(2155), false},
    70  		{"0", int16(2000), false},
    71  		{"1", int16(2001), false},
    72  		{"31", int16(2031), false},
    73  		{"32", int16(2032), false},
    74  		{"69", int16(2069), false},
    75  		{"70", int16(1970), false},
    76  		{"99", int16(1999), false},
    77  		{"1901", int16(1901), false},
    78  		{"2000", int16(2000), false},
    79  		{"2100", int16(2100), false},
    80  		{"2155", int16(2155), false},
    81  		{time.Date(2010, 1, 2, 3, 4, 5, 0, time.UTC), int16(2010), false},
    82  
    83  		{100, nil, true},
    84  		{"100", nil, true},
    85  		{1850, nil, true},
    86  		{"1850", nil, true},
    87  		{[]byte{0}, nil, true},
    88  		{false, nil, true},
    89  	}
    90  
    91  	for _, test := range tests {
    92  		t.Run(fmt.Sprintf("%v %v", test.val, test.expectedVal), func(t *testing.T) {
    93  			val, _, err := Year.Convert(test.val)
    94  			if test.expectedErr {
    95  				assert.Error(t, err)
    96  			} else {
    97  				require.NoError(t, err)
    98  				assert.Equal(t, test.expectedVal, val)
    99  				if val != nil {
   100  					assert.Equal(t, Year.ValueType(), reflect.TypeOf(val))
   101  				}
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestYearString(t *testing.T) {
   108  	require.Equal(t, "year", Year.String())
   109  }
   110  
   111  func TestYearZero(t *testing.T) {
   112  	_, ok := Year.Zero().(int16)
   113  	require.True(t, ok)
   114  }