github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/time/time_test.go (about) 1 // Copyright 2022 Matrix Origin 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 time 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/nulls" 21 "github.com/matrixorigin/matrixone/pkg/container/types" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestDateToTime(t *testing.T) { 26 testCases := []struct { 27 name string 28 args []types.Date 29 want []types.Time 30 success bool 31 }{ 32 { 33 name: "TestDateToTime01", 34 args: []types.Date{types.DateFromCalendar(2022, 3, 30)}, 35 want: []types.Time{types.TimeFromClock(false, 0, 0, 0, 0)}, 36 success: true, 37 }, 38 } 39 40 for _, v := range testCases { 41 reply := make([]types.Time, len(v.args)) 42 ns := &nulls.Nulls{} 43 reply = DateToTime(v.args, reply) 44 require.Equal(t, reply, v.want) 45 require.Equal(t, !nulls.Contains(ns, 0), v.success) 46 } 47 } 48 49 func TestDatetimeToTime(t *testing.T) { 50 testCases := []struct { 51 name string 52 inputStr string 53 want []types.Time 54 precision int32 55 success bool 56 }{ 57 { 58 name: "TestDatetimeToTime01", 59 inputStr: "2022-12-12 11:22:33", 60 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 61 precision: 0, 62 success: true, 63 }, 64 { 65 name: "TestDatetimeToTime02", 66 inputStr: "2022-12-12 11:22:33", 67 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 68 precision: 3, 69 success: true, 70 }, 71 { 72 name: "TestDatetimeToTime03", 73 inputStr: "2022-12-12 11:22:33.1234", 74 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 123000)}, 75 precision: 3, 76 success: true, 77 }, 78 { 79 name: "TestDatetimeToTime03", 80 inputStr: "2022-12-12 11:22:33.1235", 81 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 124000)}, 82 precision: 3, 83 success: true, 84 }, 85 { 86 name: "TestDatetimeToTime04", 87 inputStr: "20221212112233", 88 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 89 precision: 0, 90 success: true, 91 }, 92 } 93 94 for _, v := range testCases { 95 // only 1 input 96 reply := make([]types.Time, 1) 97 dtArr := make([]types.Datetime, 1) 98 ns := &nulls.Nulls{} 99 100 var err error 101 dtArr[0], err = types.ParseDatetime(v.inputStr, v.precision) 102 require.NoError(t, err) 103 104 reply = DatetimeToTime(dtArr, reply, v.precision) 105 require.Equal(t, reply, v.want) 106 require.Equal(t, !nulls.Contains(ns, 0), v.success) 107 } 108 } 109 110 func TestStringToTime(t *testing.T) { 111 testCases := []struct { 112 name string 113 inputStr string 114 want []types.Time 115 success bool 116 }{ 117 { 118 name: "TestStringToTime01", 119 inputStr: "2022-12-12 11:22:33", 120 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 121 success: true, 122 }, 123 { 124 name: "TestStringToTime02", 125 inputStr: "2022-12-12 11:22:33", 126 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 0)}, 127 success: true, 128 }, 129 { 130 name: "TestStringToTime02", 131 inputStr: "2022-12-12 11:22:33.1234", 132 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 123400)}, 133 success: true, 134 }, 135 { 136 name: "TestStringToTime03", 137 inputStr: "2022-12-12 11:22:33.1235", 138 want: []types.Time{types.TimeFromClock(false, 11, 22, 33, 123500)}, 139 success: true, 140 }, 141 { 142 name: "TestStringToTime04", 143 inputStr: "20221212112233", 144 want: []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 0)}, 145 success: true, 146 }, 147 { 148 name: "TestStringToTime05", 149 inputStr: "20221212112233.1235", 150 want: []types.Time{types.TimeFromClock(false, 2022121211, 22, 33, 123500)}, 151 success: true, 152 }, 153 { 154 name: "TestStringToTime06", 155 inputStr: "1122.1235", 156 want: []types.Time{types.TimeFromClock(false, 0, 11, 22, 123500)}, 157 success: true, 158 }, 159 { 160 name: "TestStringToTime07", 161 inputStr: "-1122.1235", 162 want: []types.Time{types.TimeFromClock(true, 0, 11, 22, 123500)}, 163 success: true, 164 }, 165 { 166 name: "TestStringToTime08", 167 inputStr: "-3.1235", 168 want: []types.Time{types.TimeFromClock(true, 0, 0, 3, 123500)}, 169 success: true, 170 }, 171 } 172 173 for _, v := range testCases { 174 // only 1 input 175 reply := make([]types.Time, 1) 176 strArr := make([]string, 1) 177 strArr[0] = v.inputStr 178 ns := &nulls.Nulls{} 179 180 var err error 181 reply, err = DateStringToTime(strArr, reply) 182 require.NoError(t, err) 183 require.Equal(t, reply, v.want) 184 require.Equal(t, !nulls.Contains(ns, 0), v.success) 185 } 186 }