github.com/dolthub/go-mysql-server@v0.18.0/sql/types/time_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 TestTimeCompare(t *testing.T) { 27 // This is here so that it doesn't pollute the namespace 28 parseDuration := func(str string) time.Duration { 29 d, err := time.ParseDuration(str) 30 if err != nil { 31 panic(err) 32 } 33 return d 34 } 35 36 tests := []struct { 37 val1 interface{} 38 val2 interface{} 39 expectedCmp int 40 }{ 41 {nil, 0, 1}, 42 {0, nil, -1}, 43 {nil, nil, 0}, 44 {-1, 1, -1}, 45 {59, -59, 1}, 46 {parseDuration("100ms"), 1, -1}, 47 {510144, parseDuration("46h32m"), 1}, 48 {40.134612, 40, 1}, 49 {"1112", 1112, 0}, 50 {"1112", float64(1112), 0}, 51 {1112, "00:11:12", 0}, 52 {"11:12", 111200, 0}, 53 {"11:12", "11:12:00", 0}, 54 {"11:12:00", parseDuration("11h12m"), 0}, 55 {"11:12:00.1234567", "11:12:00.1234569", 0}, 56 {"-850:00:00", "-838:59:59", 0}, 57 {"850:00:00", "838:59:59", 0}, 58 {"-838:59:59.1", "-838:59:59", 0}, 59 {"838:59:59.1", "838:59:59", 0}, 60 {1112, "00:11:12.123", -1}, 61 {1112.123, "00:11:12", 1}, 62 } 63 64 for _, test := range tests { 65 t.Run(fmt.Sprintf("%v %v", test.val1, test.val2), func(t *testing.T) { 66 cmp, err := Time.Compare(test.val1, test.val2) 67 require.NoError(t, err) 68 assert.Equal(t, test.expectedCmp, cmp) 69 }) 70 } 71 } 72 73 func TestTimeConvert(t *testing.T) { 74 // This is here so that it doesn't pollute the namespace 75 parseDuration := func(str string) time.Duration { 76 d, err := time.ParseDuration(str) 77 if err != nil { 78 panic(err) 79 } 80 return d 81 } 82 83 tests := []struct { 84 val interface{} 85 expectedVal interface{} 86 expectedErr bool 87 }{ 88 {nil, nil, false}, 89 {int8(-1), "-00:00:01", false}, 90 {uint8(59), "00:00:59", false}, 91 {"-1", "-00:00:01", false}, 92 {"59", "00:00:59", false}, 93 {parseDuration("62h22m48s379247us"), "62:22:48.379247", false}, 94 {int16(1002), "00:10:02", false}, 95 {uint16(5958), "00:59:58", false}, 96 {int32(15958), "01:59:58", false}, 97 {uint32(3332211), "333:22:11", false}, 98 {int16(-1002), "-00:10:02", false}, 99 {int16(-5958), "-00:59:58", false}, 100 {int32(-15958), "-01:59:58", false}, 101 {int32(-3332211), "-333:22:11", false}, 102 {"1002", "00:10:02", false}, 103 {"902", "00:09:02", false}, 104 {902, "00:09:02", false}, 105 {float64(-0.25), "-00:00:00.250000", false}, 106 {float64(-46.25), "-00:00:46.250000", false}, 107 {float64(-56.75), "-00:00:56.750000", false}, 108 {float64(-256.75), "-00:02:56.750000", false}, 109 {float64(-122256.5), "-12:22:56.500000", false}, 110 {float64(902), "00:09:02", false}, 111 {"00:05:55.2", "00:05:55.200000", false}, 112 {"555.2", "00:05:55.200000", false}, 113 {555.2, "00:05:55.200000", false}, 114 {"5958", "00:59:58", false}, 115 {"15958", "01:59:58", false}, 116 {"3332211", "333:22:11", false}, 117 {float32(40.134613), "00:00:40.134613", false}, 118 {float64(401122.134612), "40:11:22.134612", false}, 119 {"40.134608", "00:00:40.134608", false}, 120 {"401122.134612", "40:11:22.134612", false}, 121 {"401122.134612585", "40:11:22.134613", false}, 122 {"595959.99999951", "59:59:59.999999", false}, 123 {"585859.999999514", "58:58:59.999999", false}, 124 {"40:11:22.134612585", "40:11:22.134613", false}, 125 {"59:59:59.9999995", "60:00:00", false}, 126 {"58:59:59.99999951", "58:59:59.999999", false}, 127 {"58:58:59.999999514", "58:58:59.999999", false}, 128 {"11:12", "11:12:00", false}, 129 {"-850:00:00", "-838:59:59", false}, 130 {"850:00:00", "838:59:59", false}, 131 {"-838:59:59.1", "-838:59:59", false}, 132 {"838:59:59.1", "838:59:59", false}, 133 {time.Date(2019, 12, 12, 12, 12, 12, 0, time.UTC), "12:12:12", false}, 134 135 {1060, nil, true}, 136 {60, nil, true}, 137 {6040, nil, true}, 138 {104060, nil, true}, 139 {106040, nil, true}, 140 {"1060", nil, true}, 141 {"60", nil, true}, 142 {"6040", nil, true}, 143 {"104060", nil, true}, 144 {"106040", nil, true}, 145 {"00:00:60", nil, true}, 146 {"00:60:00", nil, true}, 147 {-1060, nil, true}, 148 {-60, nil, true}, 149 {-6040, nil, true}, 150 {-104060, nil, true}, 151 {-106040, nil, true}, 152 {"-1060", nil, true}, 153 {"-60", nil, true}, 154 {"-6040", nil, true}, 155 {"-104060", nil, true}, 156 {"-106040", nil, true}, 157 {"-00:00:60", nil, true}, 158 {"-00:60:00", nil, true}, 159 {[]byte{0}, nil, true}, 160 } 161 162 for _, test := range tests { 163 t.Run(fmt.Sprintf("%v %v", test.val, test.expectedVal), func(t *testing.T) { 164 val, _, err := Time.Convert(test.val) 165 if test.expectedErr { 166 assert.Error(t, err) 167 } else { 168 require.NoError(t, err) 169 if test.val == nil { 170 assert.Equal(t, test.expectedVal, val) 171 } else { 172 assert.Equal(t, test.expectedVal, val.(Timespan).String()) 173 timespan, err := Time.ConvertToTimespan(test.val) 174 require.NoError(t, err) 175 require.True(t, timespan.Equals(val.(Timespan))) 176 ms := timespan.AsMicroseconds() 177 ums := Time.MicrosecondsToTimespan(ms) 178 cmp, err := Time.Compare(test.val, ums) 179 require.NoError(t, err) 180 assert.Equal(t, 0, cmp) 181 } 182 } 183 }) 184 } 185 } 186 187 func TestTimeConvertToTimeDuration(t *testing.T) { 188 // This is here so that it doesn't pollute the namespace 189 parseDuration := func(str string) time.Duration { 190 d, err := time.ParseDuration(str) 191 if err != nil { 192 panic(err) 193 } 194 return d 195 } 196 197 tests := []struct { 198 val string 199 expectedVal time.Duration 200 }{ 201 {"-00:00:01", parseDuration("-1s")}, 202 {"00:00:59", parseDuration("59s")}, 203 {"62:22:48.379247", parseDuration("62h22m48s379247µs")}, 204 {"00:10:02", parseDuration("10m2s")}, 205 {"00:59:58", parseDuration("59m58s")}, 206 } 207 208 for _, test := range tests { 209 t.Run(fmt.Sprintf("%v %v", test.val, test.expectedVal), func(t *testing.T) { 210 val, err := Time.ConvertToTimeDuration(test.val) 211 require.NoError(t, err) 212 assert.Equal(t, test.expectedVal, val) 213 }) 214 } 215 } 216 217 func TestTimeString(t *testing.T) { 218 require.Equal(t, "time(6)", Time.String()) 219 } 220 221 func TestTimeZero(t *testing.T) { 222 _, ok := Time.Zero().(Timespan) 223 require.True(t, ok) 224 }