github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/timestamp/timestamp_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 timestamp 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/matrixorigin/matrixone/pkg/container/nulls" 22 "github.com/stretchr/testify/require" 23 24 "github.com/matrixorigin/matrixone/pkg/container/types" 25 ) 26 27 func TestDateToTimestamp(t *testing.T) { 28 testCases := []struct { 29 name string 30 args []types.Date 31 want []types.Timestamp 32 success bool 33 }{ 34 { 35 name: "normal test cases", 36 args: []types.Date{types.DateFromCalendar(2022, 3, 30)}, 37 want: []types.Timestamp{types.FromClockUTC(2022, 3, 30, 0, 0, 0, 0)}, 38 success: true, 39 }, 40 } 41 42 for _, v := range testCases { 43 reply := make([]types.Timestamp, len(v.args)) 44 ns := &nulls.Nulls{} 45 reply = DateToTimestamp(time.UTC, v.args, ns, reply) 46 require.Equal(t, reply, v.want) 47 require.Equal(t, !nulls.Contains(ns, 0), v.success) 48 } 49 } 50 51 func TestDatetimeToTimestamp(t *testing.T) { 52 testCases := []struct { 53 name string 54 args []types.Datetime 55 want []types.Timestamp 56 success bool 57 }{ 58 { 59 name: "normal test cases", 60 args: []types.Datetime{types.DatetimeFromClock(2022, 3, 30, 0, 0, 0, 0)}, 61 want: []types.Timestamp{types.FromClockUTC(2022, 3, 30, 0, 0, 0, 0)}, 62 success: true, 63 }, 64 } 65 66 for _, v := range testCases { 67 reply := make([]types.Timestamp, len(v.args)) 68 ns := &nulls.Nulls{} 69 reply = DatetimeToTimestamp(time.UTC, v.args, ns, reply) 70 require.Equal(t, reply, v.want) 71 require.Equal(t, !nulls.Contains(ns, 0), v.success) 72 } 73 } 74 75 func TestDateStringToTimestamp(t *testing.T) { 76 testCases := []struct { 77 name string 78 args []string 79 want []types.Timestamp 80 success bool 81 }{ 82 { 83 name: "normal test cases", 84 args: []string{"2022-03-30 00:00:00"}, 85 want: []types.Timestamp{types.FromClockUTC(2022, 3, 30, 0, 0, 0, 0)}, 86 success: true, 87 }, 88 { 89 name: "normal test cases", 90 args: []string{"1022-03-30 00:00:00"}, 91 want: []types.Timestamp{types.FromClockUTC(1022, 3, 30, 0, 0, 0, 0)}, 92 success: true, 93 }, 94 } 95 96 for _, v := range testCases { 97 reply := make([]types.Timestamp, len(v.args)) 98 ns := &nulls.Nulls{} 99 reply = DateStringToTimestamp(time.UTC, v.args, ns, reply) 100 require.Equal(t, reply, v.want) 101 require.Equal(t, !nulls.Contains(ns, 0), v.success) 102 } 103 }