github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/date_add/date_add_test.go (about) 1 // Copyright 2021 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 package date_add 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/matrixorigin/matrixone/pkg/container/nulls" 21 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func TestDateAdd(t *testing.T) { 27 testCases := []struct { 28 name string 29 args1 []types.Date 30 args2 []int64 31 args3 []int64 32 want []types.Date 33 success bool 34 }{ 35 { 36 args1: []types.Date{types.DateFromCalendar(2021, 8, 13)}, 37 args2: []int64{1}, 38 args3: []int64{int64(types.Day)}, 39 want: []types.Date{types.DateFromCalendar(2021, 8, 14)}, 40 success: true, 41 }, 42 { 43 args1: []types.Date{types.DateFromCalendar(2021, 1, 31)}, 44 args2: []int64{1}, 45 args3: []int64{int64(types.Month)}, 46 want: []types.Date{types.DateFromCalendar(2021, 2, 28)}, 47 success: true, 48 }, 49 { 50 args1: []types.Date{types.DateFromCalendar(9999, 12, 31)}, 51 args2: []int64{1}, 52 args3: []int64{int64(types.Day)}, 53 want: []types.Date{types.DateFromCalendar(1, 1, 1)}, 54 success: false, 55 }, 56 } 57 58 for _, c := range testCases { 59 t.Run(c.name, func(t *testing.T) { 60 got := make([]types.Date, len(c.args1)) 61 xnu := &nulls.Nulls{} 62 ynu := &nulls.Nulls{} 63 rnu := &nulls.Nulls{} 64 d, e := DateAdd(c.args1, c.args2, c.args3, xnu, ynu, rnu, got) 65 if c.success { 66 require.Equal(t, e, nil) 67 } else { 68 require.NotEqual(t, e, nil) 69 } 70 require.Equal(t, c.want, d) 71 }) 72 } 73 74 } 75 76 func TestDatetimeAdd(t *testing.T) { 77 testCases := []struct { 78 name string 79 args1 []types.Datetime 80 args2 []int64 81 args3 []int64 82 want []types.Datetime 83 success bool 84 }{ 85 { 86 args1: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 1)}, 87 args2: []int64{1}, 88 args3: []int64{int64(types.MicroSecond)}, 89 want: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 2)}, 90 success: true, 91 }, 92 { 93 args1: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 1)}, 94 args2: []int64{1}, 95 args3: []int64{int64(types.Second)}, 96 want: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 2, 1)}, 97 success: true, 98 }, 99 { 100 args1: []types.Datetime{types.DatetimeFromClock(9999, 1, 1, 1, 1, 1, 1)}, 101 args2: []int64{1}, 102 args3: []int64{int64(types.Year)}, 103 want: []types.Datetime{types.DatetimeFromClock(1, 1, 1, 0, 0, 0, 0)}, 104 success: false, 105 }, 106 } 107 108 for _, c := range testCases { 109 t.Run(c.name, func(t *testing.T) { 110 got := make([]types.Datetime, len(c.args1)) 111 xnu := &nulls.Nulls{} 112 ynu := &nulls.Nulls{} 113 rnu := &nulls.Nulls{} 114 d, e := DatetimeAdd(c.args1, c.args2, c.args3, xnu, ynu, rnu, got) 115 if c.success { 116 require.Equal(t, e, nil) 117 } else { 118 require.NotEqual(t, e, nil) 119 } 120 require.Equal(t, c.want, d) 121 }) 122 } 123 124 } 125 126 func TestDateStringAdd(t *testing.T) { 127 testCases := []struct { 128 name string 129 args1 []string 130 args2 []int64 131 args3 []int64 132 want []types.Datetime 133 success bool 134 }{ 135 { 136 args1: []string{"2018-01-01"}, 137 args2: []int64{1}, 138 args3: []int64{int64(types.Day)}, 139 want: []types.Datetime{types.DatetimeFromClock(2018, 1, 2, 0, 0, 0, 0)}, 140 success: true, 141 }, 142 { 143 args1: []string{"2018-01-01"}, 144 args2: []int64{1}, 145 args3: []int64{int64(types.Second)}, 146 want: []types.Datetime{types.DatetimeFromClock(2018, 1, 1, 0, 0, 1, 0)}, 147 success: true, 148 }, 149 { 150 args1: []string{"2018-01-01 00:00:01"}, 151 args2: []int64{1}, 152 args3: []int64{int64(types.Second)}, 153 want: []types.Datetime{types.DatetimeFromClock(2018, 1, 1, 0, 0, 2, 0)}, 154 success: true, 155 }, 156 { 157 args1: []string{"xxxx"}, 158 args2: []int64{1}, 159 args3: []int64{int64(types.Second)}, 160 want: []types.Datetime{types.DatetimeFromClock(1, 1, 1, 0, 0, 0, 0)}, 161 success: false, 162 }, 163 } 164 165 for _, c := range testCases { 166 t.Run(c.name, func(t *testing.T) { 167 got := make([]types.Datetime, len(c.args1)) 168 xnu := &nulls.Nulls{} 169 ynu := &nulls.Nulls{} 170 rnu := &nulls.Nulls{} 171 d, e := DateStringAdd(c.args1, c.args2, c.args3, xnu, ynu, rnu, got) 172 if c.success { 173 require.Equal(t, e, nil) 174 } else { 175 require.NotEqual(t, e, nil) 176 } 177 require.Equal(t, c.want, d) 178 }) 179 } 180 181 } 182 183 func TestTimeStampAdd(t *testing.T) { 184 testCases := []struct { 185 name string 186 args1 []types.Timestamp 187 args2 []int64 188 args3 []int64 189 want []types.Timestamp 190 success bool 191 }{ 192 { 193 args1: []types.Timestamp{types.FromClockUTC(2020, 1, 1, 1, 1, 1, 1)}, 194 args2: []int64{1}, 195 args3: []int64{int64(types.MicroSecond)}, 196 want: []types.Timestamp{types.FromClockUTC(2020, 1, 1, 1, 1, 1, 2)}, 197 success: true, 198 }, 199 { 200 args1: []types.Timestamp{types.FromClockUTC(2038, 1, 19, 3, 14, 7, 999999)}, 201 args2: []int64{1}, 202 args3: []int64{int64(types.MicroSecond)}, 203 want: []types.Timestamp{types.FromClockUTC(2038, 1, 19, 3, 14, 8, 0)}, 204 success: true, 205 }, 206 } 207 208 for _, c := range testCases { 209 t.Run(c.name, func(t *testing.T) { 210 got := make([]types.Timestamp, len(c.args1)) 211 xnu := &nulls.Nulls{} 212 ynu := &nulls.Nulls{} 213 rnu := &nulls.Nulls{} 214 rs, err := TimestampAdd(time.Local, c.args1, c.args2, c.args3, xnu, ynu, rnu, got) 215 require.Equal(t, c.want, rs) 216 if c.success { 217 require.Equal(t, err, nil) 218 } else { 219 require.NotEqual(t, err, nil) 220 } 221 }) 222 } 223 224 }