github.com/dolthub/go-mysql-server@v0.18.0/sql/types/datetime_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 sqltypes "github.com/dolthub/vitess/go/sqltypes" 24 "github.com/dolthub/vitess/go/vt/proto/query" 25 "github.com/stretchr/testify/assert" 26 "github.com/stretchr/testify/require" 27 28 "github.com/dolthub/go-mysql-server/sql" 29 ) 30 31 func TestDatetimeCompare(t *testing.T) { 32 tests := []struct { 33 typ sql.Type 34 val1 interface{} 35 val2 interface{} 36 expectedCmp int 37 }{ 38 {Date, nil, 0, 1}, 39 {Datetime, 0, nil, -1}, 40 {Timestamp, nil, nil, 0}, 41 42 {Date, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 43 time.Date(2012, 12, 12, 12, 24, 24, 24, time.UTC), 0}, 44 {Date, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 45 "2010-06-03", 1}, 46 {Date, "2010-06-03 06:03:11", 47 time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), -1}, 48 {Datetime, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 49 time.Date(2012, 12, 12, 12, 24, 24, 24, time.UTC), -1}, 50 {Datetime, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 51 "2010-06-03", 1}, 52 {Datetime, "2010-06-03 06:03:11", 53 time.Date(2010, 6, 3, 6, 3, 11, 0, time.UTC), 0}, 54 {Datetime, "2010-06-03 06:03:11", "2010-06-03 06:03:11", 0}, 55 {DatetimeMaxPrecision, "2010-06-03 06:03:11.123456", "2010-06-03 06:03:11", 1}, 56 {DatetimeMaxPrecision, "2010-06-03 06:03:11", "2010-06-03 06:03:11.123456", -1}, 57 {DatetimeMaxPrecision, "2010-06-03 06:03:11.123456111", "2010-06-03 06:03:11.123456333", 0}, 58 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 06:03:11.123", "2010-06-03 06:03:11", 1}, 59 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 06:03:11", "2010-06-03 06:03:11.123", -1}, 60 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 06:03:11.123456", "2010-06-03 06:03:11.123789", 0}, 61 {Timestamp, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 62 time.Date(2012, 12, 12, 12, 24, 24, 24, time.UTC), -1}, 63 {Timestamp, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 64 "2010-06-03", 1}, 65 {Timestamp, "2010-06-03 06:03:11", 66 time.Date(2010, 6, 3, 6, 3, 11, 0, time.UTC), 0}, 67 } 68 69 for _, test := range tests { 70 t.Run(fmt.Sprintf("%v %v", test.val1, test.val2), func(t *testing.T) { 71 cmp, err := test.typ.Compare(test.val1, test.val2) 72 require.NoError(t, err) 73 assert.Equal(t, test.expectedCmp, cmp) 74 }) 75 } 76 } 77 78 func TestDatetimeCreate(t *testing.T) { 79 tests := []struct { 80 baseType query.Type 81 expectedType datetimeType 82 expectedErr bool 83 }{ 84 {baseType: sqltypes.Date, expectedType: datetimeType{baseType: sqltypes.Date}}, 85 {baseType: sqltypes.Datetime, expectedType: datetimeType{baseType: sqltypes.Datetime}}, 86 {baseType: sqltypes.Timestamp, expectedType: datetimeType{baseType: sqltypes.Timestamp}}, 87 } 88 89 for _, test := range tests { 90 t.Run(fmt.Sprintf("%v", test.baseType), func(t *testing.T) { 91 typ, err := CreateDatetimeType(test.baseType, 0) 92 if test.expectedErr { 93 assert.Error(t, err) 94 } else { 95 require.NoError(t, err) 96 assert.Equal(t, test.expectedType, typ) 97 } 98 }) 99 } 100 } 101 102 func TestDatetimeCreateInvalidBaseTypes(t *testing.T) { 103 tests := []struct { 104 baseType query.Type 105 expectedType datetimeType 106 expectedErr bool 107 }{ 108 {sqltypes.Binary, datetimeType{}, true}, 109 {sqltypes.Bit, datetimeType{}, true}, 110 {sqltypes.Blob, datetimeType{}, true}, 111 {sqltypes.Char, datetimeType{}, true}, 112 {sqltypes.Decimal, datetimeType{}, true}, 113 {sqltypes.Enum, datetimeType{}, true}, 114 {sqltypes.Expression, datetimeType{}, true}, 115 {sqltypes.Float32, datetimeType{}, true}, 116 {sqltypes.Float64, datetimeType{}, true}, 117 {sqltypes.Geometry, datetimeType{}, true}, 118 {sqltypes.Int16, datetimeType{}, true}, 119 {sqltypes.Int24, datetimeType{}, true}, 120 {sqltypes.Int32, datetimeType{}, true}, 121 {sqltypes.Int64, datetimeType{}, true}, 122 {sqltypes.Int8, datetimeType{}, true}, 123 {sqltypes.Null, datetimeType{}, true}, 124 {sqltypes.Set, datetimeType{}, true}, 125 {sqltypes.Text, datetimeType{}, true}, 126 {sqltypes.Time, datetimeType{}, true}, 127 {sqltypes.TypeJSON, datetimeType{}, true}, 128 {sqltypes.Uint16, datetimeType{}, true}, 129 {sqltypes.Uint24, datetimeType{}, true}, 130 {sqltypes.Uint32, datetimeType{}, true}, 131 {sqltypes.Uint64, datetimeType{}, true}, 132 {sqltypes.Uint8, datetimeType{}, true}, 133 {sqltypes.VarBinary, datetimeType{}, true}, 134 {sqltypes.VarChar, datetimeType{}, true}, 135 {sqltypes.Year, datetimeType{}, true}, 136 } 137 138 for _, test := range tests { 139 t.Run(fmt.Sprintf("%v", test.baseType), func(t *testing.T) { 140 typ, err := CreateDatetimeType(test.baseType, 0) 141 if test.expectedErr { 142 assert.Error(t, err) 143 } else { 144 require.NoError(t, err) 145 assert.Equal(t, test.expectedType, typ) 146 } 147 }) 148 } 149 } 150 151 func TestDatetimeConvert(t *testing.T) { 152 type testcase struct { 153 typ sql.Type 154 val interface{} 155 expectedVal interface{} 156 expectedErr bool 157 } 158 tests := []testcase{ 159 {Date, nil, nil, false}, 160 {Date, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 161 time.Date(2012, 12, 12, 0, 0, 0, 0, time.UTC), false}, 162 {Date, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 163 {Date, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 164 {Date, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 165 {Date, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 166 {Date, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 167 {Date, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 168 {Date, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 169 {Date, "2010-06-03T12:12:12.000012Z", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 170 {Date, "20100603", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 171 {Date, "20100603121212", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 172 173 {DatetimeMaxPrecision, nil, nil, false}, 174 {DatetimeMaxPrecision, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 175 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 176 {DatetimeMaxPrecision, time.Date(2012, 12, 12, 12, 12, 12, 12345, time.UTC), 177 time.Date(2012, 12, 12, 12, 12, 12, 12000, time.UTC), false}, 178 {DatetimeMaxPrecision, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 179 {DatetimeMaxPrecision, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 180 {DatetimeMaxPrecision, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 181 {DatetimeMaxPrecision, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 182 {DatetimeMaxPrecision, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 183 {DatetimeMaxPrecision, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false}, 184 {DatetimeMaxPrecision, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 185 {DatetimeMaxPrecision, "2010-06-03T12:12:12.000012Z", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false}, 186 {DatetimeMaxPrecision, "20100603", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 187 {DatetimeMaxPrecision, "20100603121212", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 188 {DatetimeMaxPrecision, "2010-6-3 12:12:12", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 189 {DatetimeMaxPrecision, "2010-6-13 12:12:12", time.Date(2010, 6, 13, 12, 12, 12, 0, time.UTC), false}, 190 {DatetimeMaxPrecision, "2010-10-3 12:12:12", time.Date(2010, 10, 3, 12, 12, 12, 0, time.UTC), false}, 191 {DatetimeMaxPrecision, "2010-10-3 12:12:2", time.Date(2010, 10, 3, 12, 12, 2, 0, time.UTC), false}, 192 {DatetimeMaxPrecision, "2010-10-3 12:2:2", time.Date(2010, 10, 3, 12, 2, 2, 0, time.UTC), false}, 193 {DatetimeMaxPrecision, "2010-06-03 12:3", time.Date(2010, 6, 3, 12, 3, 0, 0, time.UTC), false}, 194 {DatetimeMaxPrecision, "2010-06-03 12:34", time.Date(2010, 6, 3, 12, 34, 0, 0, time.UTC), false}, 195 {DatetimeMaxPrecision, "2010-06-03 12:34:", time.Date(2010, 6, 3, 12, 34, 0, 0, time.UTC), false}, 196 {DatetimeMaxPrecision, "2010-06-03 12:34:.", time.Date(2010, 6, 3, 12, 34, 0, 0, time.UTC), false}, 197 {DatetimeMaxPrecision, "2010-06-03 12:34:5", time.Date(2010, 6, 3, 12, 34, 5, 0, time.UTC), false}, 198 {DatetimeMaxPrecision, "2010-06-03 12:34:56", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 199 {DatetimeMaxPrecision, "2010-06-03 12:34:56.", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 200 {DatetimeMaxPrecision, "2010-06-03 12:34:56.7", time.Date(2010, 6, 3, 12, 34, 56, 700000000, time.UTC), false}, 201 {DatetimeMaxPrecision, "2010-06-03 12:34:56.78", time.Date(2010, 6, 3, 12, 34, 56, 780000000, time.UTC), false}, 202 {DatetimeMaxPrecision, "2010-06-03 12:34:56.789", time.Date(2010, 6, 3, 12, 34, 56, 789000000, time.UTC), false}, 203 204 {MustCreateDatetimeType(sqltypes.Datetime, 3), nil, nil, false}, 205 {MustCreateDatetimeType(sqltypes.Datetime, 3), time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 206 time.Date(2012, 12, 12, 12, 12, 12, 12000000, time.UTC), false}, 207 {MustCreateDatetimeType(sqltypes.Datetime, 3), time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 208 time.Date(2012, 12, 12, 12, 12, 12, 12000000, time.UTC), false}, 209 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 12:12:12.123456", time.Date(2010, 6, 3, 12, 12, 12, 123000000, time.UTC), false}, 210 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03T12:12:12.123456Z", time.Date(2010, 6, 3, 12, 12, 12, 123000000, time.UTC), false}, 211 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 12:34:56.7", time.Date(2010, 6, 3, 12, 34, 56, 700000000, time.UTC), false}, 212 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 12:34:56.78", time.Date(2010, 6, 3, 12, 34, 56, 780000000, time.UTC), false}, 213 {MustCreateDatetimeType(sqltypes.Datetime, 3), "2010-06-03 12:34:56.789", time.Date(2010, 6, 3, 12, 34, 56, 789000000, time.UTC), false}, 214 215 {Datetime, nil, nil, false}, 216 {Datetime, time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 217 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 218 {Datetime, time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 219 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 220 {Datetime, "2010-06-03 12:12:12.123456", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 221 {Datetime, "2010-06-03T12:12:12.123456Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 222 {Datetime, "2010-06-03 12:34:56.7", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 223 {Datetime, "2010-06-03 12:34:56.78", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 224 {Datetime, "2010-06-03 12:34:56.789", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 225 226 {TimestampMaxPrecision, nil, nil, false}, 227 {TimestampMaxPrecision, time.Date(2012, 12, 12, 12, 12, 12, 12, time.UTC), 228 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 229 {TimestampMaxPrecision, time.Date(2012, 12, 12, 12, 12, 12, 12345, time.UTC), 230 time.Date(2012, 12, 12, 12, 12, 12, 12000, time.UTC), false}, 231 {TimestampMaxPrecision, "2010-06-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 232 {TimestampMaxPrecision, "2010-6-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 233 {TimestampMaxPrecision, "2010-6-03", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 234 {TimestampMaxPrecision, "2010-06-3", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 235 {TimestampMaxPrecision, "2010-06-03 12:12:12", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 236 {TimestampMaxPrecision, "2010-06-03 12:12:12.000012", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false}, 237 {TimestampMaxPrecision, "2010-06-03T12:12:12Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 238 {TimestampMaxPrecision, "2010-06-03T12:12:12.000012Z", time.Date(2010, 6, 3, 12, 12, 12, 12000, time.UTC), false}, 239 {TimestampMaxPrecision, "20100603", time.Date(2010, 6, 3, 0, 0, 0, 0, time.UTC), false}, 240 {TimestampMaxPrecision, "20100603121212", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 241 {TimestampMaxPrecision, time.Date(2012, 12, 12, 12, 12, 12, 12345, time.UTC).UTC().String(), time.Date(2012, 12, 12, 12, 12, 12, 12000, time.UTC), false}, 242 243 {MustCreateDatetimeType(sqltypes.Timestamp, 3), nil, nil, false}, 244 {MustCreateDatetimeType(sqltypes.Timestamp, 3), time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 245 time.Date(2012, 12, 12, 12, 12, 12, 12000000, time.UTC), false}, 246 {MustCreateDatetimeType(sqltypes.Timestamp, 3), time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 247 time.Date(2012, 12, 12, 12, 12, 12, 12000000, time.UTC), false}, 248 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "2010-06-03 12:12:12.123456", time.Date(2010, 6, 3, 12, 12, 12, 123000000, time.UTC), false}, 249 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "2010-06-03T12:12:12.123456Z", time.Date(2010, 6, 3, 12, 12, 12, 123000000, time.UTC), false}, 250 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "2010-06-03 12:34:56.7", time.Date(2010, 6, 3, 12, 34, 56, 700000000, time.UTC), false}, 251 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "2010-06-03 12:34:56.78", time.Date(2010, 6, 3, 12, 34, 56, 780000000, time.UTC), false}, 252 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "2010-06-03 12:34:56.789", time.Date(2010, 6, 3, 12, 34, 56, 789000000, time.UTC), false}, 253 254 {Timestamp, nil, nil, false}, 255 {Timestamp, time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 256 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 257 {Timestamp, time.Date(2012, 12, 12, 12, 12, 12, 12345678, time.UTC), 258 time.Date(2012, 12, 12, 12, 12, 12, 0, time.UTC), false}, 259 {Timestamp, "2010-06-03 12:12:12.123456", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 260 {Timestamp, "2010-06-03T12:12:12.123456Z", time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC), false}, 261 {Timestamp, "2010-06-03 12:34:56.7", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 262 {Timestamp, "2010-06-03 12:34:56.78", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 263 {Timestamp, "2010-06-03 12:34:56.789", time.Date(2010, 6, 3, 12, 34, 56, 0, time.UTC), false}, 264 265 {Date, "0000-01-01 00:00:00", time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 266 {Date, "0500-01-01 00:00:00", time.Date(500, 1, 1, 0, 0, 0, 0, time.UTC), false}, 267 {Date, time.Date(10000, 1, 1, 1, 1, 1, 1, time.UTC), nil, true}, 268 {Date, "", nil, true}, 269 {Date, "0500-01-01", time.Date(500, 1, 1, 0, 0, 0, 0, time.UTC), false}, 270 {Date, "10000-01-01", nil, true}, 271 {Date, int(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 272 {Date, int8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 273 {Date, int16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 274 {Date, int32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 275 {Date, int64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 276 {Date, uint(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 277 {Date, uint8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 278 {Date, uint16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 279 {Date, uint32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 280 {Date, uint64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 281 {Date, float32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 282 {Date, float64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 283 {Date, []byte{0}, nil, true}, 284 285 {DatetimeMaxPrecision, "0500-01-01 01:01:01", time.Date(500, 1, 1, 1, 1, 1, 0, time.UTC), false}, 286 {DatetimeMaxPrecision, "0000-01-01 00:00:00", time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 287 {DatetimeMaxPrecision, time.Date(10000, 1, 1, 1, 1, 1, 1, time.UTC), nil, true}, 288 {DatetimeMaxPrecision, int(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 289 {DatetimeMaxPrecision, int8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 290 {DatetimeMaxPrecision, int16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 291 {DatetimeMaxPrecision, int32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 292 {DatetimeMaxPrecision, int64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 293 {DatetimeMaxPrecision, uint(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 294 {DatetimeMaxPrecision, uint8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 295 {DatetimeMaxPrecision, uint16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 296 {DatetimeMaxPrecision, uint32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 297 {DatetimeMaxPrecision, uint64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 298 {DatetimeMaxPrecision, float32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 299 {DatetimeMaxPrecision, float64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 300 {DatetimeMaxPrecision, []byte{0}, nil, true}, 301 302 {TimestampMaxPrecision, time.Date(1960, 1, 1, 1, 1, 1, 1, time.UTC), nil, true}, 303 {TimestampMaxPrecision, "1970-01-01 00:00:00", nil, true}, 304 {TimestampMaxPrecision, "1970-01-01 00:00:01", time.Date(1970, 1, 1, 0, 0, 1, 0, time.UTC), false}, 305 {TimestampMaxPrecision, time.Date(2040, 1, 1, 1, 1, 1, 1, time.UTC), nil, true}, 306 {TimestampMaxPrecision, int(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 307 {TimestampMaxPrecision, int8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 308 {TimestampMaxPrecision, int16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 309 {TimestampMaxPrecision, int32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 310 {TimestampMaxPrecision, int64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 311 {TimestampMaxPrecision, uint(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 312 {TimestampMaxPrecision, uint8(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 313 {TimestampMaxPrecision, uint16(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 314 {TimestampMaxPrecision, uint32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 315 {TimestampMaxPrecision, uint64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 316 {TimestampMaxPrecision, float32(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 317 {TimestampMaxPrecision, float64(0), time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC), false}, 318 {TimestampMaxPrecision, []byte{0}, nil, true}, 319 320 {Date, int(1), nil, true}, 321 {Date, int8(1), nil, true}, 322 {Date, int16(1), nil, true}, 323 {Date, int32(1), nil, true}, 324 {Date, int64(1), nil, true}, 325 {Date, uint(1), nil, true}, 326 {Date, uint8(1), nil, true}, 327 {Date, uint16(1), nil, true}, 328 {Date, uint32(1), nil, true}, 329 {Date, uint64(1), nil, true}, 330 {Date, float32(1), nil, true}, 331 {Date, float64(1), nil, true}, 332 333 {DatetimeMaxPrecision, int(1), nil, true}, 334 {DatetimeMaxPrecision, int8(1), nil, true}, 335 {DatetimeMaxPrecision, int16(1), nil, true}, 336 {DatetimeMaxPrecision, int32(1), nil, true}, 337 {DatetimeMaxPrecision, int64(1), nil, true}, 338 {DatetimeMaxPrecision, uint(1), nil, true}, 339 {DatetimeMaxPrecision, uint8(1), nil, true}, 340 {DatetimeMaxPrecision, uint16(1), nil, true}, 341 {DatetimeMaxPrecision, uint32(1), nil, true}, 342 {DatetimeMaxPrecision, uint64(1), nil, true}, 343 {DatetimeMaxPrecision, float32(1), nil, true}, 344 {DatetimeMaxPrecision, float64(1), nil, true}, 345 346 {TimestampMaxPrecision, int(1), nil, true}, 347 {TimestampMaxPrecision, int8(1), nil, true}, 348 {TimestampMaxPrecision, int16(1), nil, true}, 349 {TimestampMaxPrecision, int32(1), nil, true}, 350 {TimestampMaxPrecision, int64(1), nil, true}, 351 {TimestampMaxPrecision, uint(1), nil, true}, 352 {TimestampMaxPrecision, uint8(1), nil, true}, 353 {TimestampMaxPrecision, uint16(1), nil, true}, 354 {TimestampMaxPrecision, uint32(1), nil, true}, 355 {TimestampMaxPrecision, uint64(1), nil, true}, 356 {TimestampMaxPrecision, float32(1), nil, true}, 357 {TimestampMaxPrecision, float64(1), nil, true}, 358 } 359 360 for _, test := range tests { 361 t.Run(fmt.Sprintf("%v %v %v", test.typ, test.val, test.expectedVal), func(t *testing.T) { 362 val, _, err := test.typ.Convert(test.val) 363 if test.expectedErr { 364 assert.Error(t, err) 365 } else { 366 require.NoError(t, err) 367 assert.Equal(t, test.expectedVal, val) 368 if val != nil { 369 assert.Equal(t, test.typ.ValueType(), reflect.TypeOf(val)) 370 } 371 } 372 }) 373 } 374 } 375 376 func TestDatetimeString(t *testing.T) { 377 tests := []struct { 378 typ sql.Type 379 expectedStr string 380 }{ 381 {MustCreateDatetimeType(sqltypes.Date, 0), "date"}, 382 {MustCreateDatetimeType(sqltypes.Datetime, 0), "datetime"}, 383 {datetimeType{baseType: sqltypes.Datetime, precision: 3}, "datetime(3)"}, 384 {datetimeType{baseType: sqltypes.Datetime, precision: 6}, "datetime(6)"}, 385 {MustCreateDatetimeType(sqltypes.Timestamp, 0), "timestamp"}, 386 {MustCreateDatetimeType(sqltypes.Timestamp, 3), "timestamp(3)"}, 387 {MustCreateDatetimeType(sqltypes.Timestamp, 6), "timestamp(6)"}, 388 } 389 390 for _, test := range tests { 391 t.Run(fmt.Sprintf("%v %v", test.typ, test.expectedStr), func(t *testing.T) { 392 str := test.typ.String() 393 assert.Equal(t, test.expectedStr, str) 394 }) 395 } 396 } 397 398 func TestDatetimeZero(t *testing.T) { 399 _, ok := MustCreateDatetimeType(sqltypes.Date, 0).Zero().(time.Time) 400 require.True(t, ok) 401 _, ok = MustCreateDatetimeType(sqltypes.Datetime, 0).Zero().(time.Time) 402 require.True(t, ok) 403 _, ok = MustCreateDatetimeType(sqltypes.Timestamp, 0).Zero().(time.Time) 404 require.True(t, ok) 405 }