github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/weekday_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 unary 16 17 import ( 18 "testing" 19 20 "github.com/matrixorigin/matrixone/pkg/container/types" 21 "github.com/matrixorigin/matrixone/pkg/container/vector" 22 "github.com/matrixorigin/matrixone/pkg/testutil" 23 "github.com/smartystreets/goconvey/convey" 24 ) 25 26 func TestWeekday(t *testing.T) { 27 convey.Convey("WeekDayDateCase", t, func() { 28 type kase struct { 29 s string 30 want int64 31 } 32 33 kases := []kase{ 34 { 35 s: "2004-04-03", 36 want: 5, 37 }, 38 { 39 s: "2021-10-03", 40 want: 6, 41 }, 42 { 43 s: "2020-08-23", 44 want: 6, 45 }, 46 } 47 48 var inStrs []string 49 var wantInt64 []int64 50 for _, k := range kases { 51 inStrs = append(inStrs, k.s) 52 wantInt64 = append(wantInt64, k.want) 53 } 54 55 inVector := testutil.MakeDateVector(inStrs, nil) 56 wantVec := testutil.MakeInt64Vector(wantInt64, nil) 57 proc := testutil.NewProc() 58 res, err := DateToWeekday([]*vector.Vector{inVector}, proc) 59 convey.So(err, convey.ShouldBeNil) 60 compare := testutil.CompareVectors(wantVec, res) 61 convey.So(compare, convey.ShouldBeTrue) 62 }) 63 64 convey.Convey("ScalarWeekDayDateCase", t, func() { 65 type kase struct { 66 s string 67 want int64 68 } 69 70 k := kase{ 71 s: "2020-08-23", 72 want: 6, 73 } 74 75 inVector := testutil.MakeScalarDate(k.s, 10) 76 wantVec := testutil.MakeScalarInt64(k.want, 10) 77 proc := testutil.NewProc() 78 res, err := DateToWeekday([]*vector.Vector{inVector}, proc) 79 convey.So(err, convey.ShouldBeNil) 80 compare := testutil.CompareVectors(wantVec, res) 81 convey.So(compare, convey.ShouldBeTrue) 82 }) 83 84 convey.Convey("WeekDayDateTimeCase", t, func() { 85 type kase struct { 86 s string 87 want int64 88 } 89 90 kases := []kase{ 91 { 92 s: "2004-04-03 13:11:10", 93 want: 5, 94 }, 95 { 96 s: "2021-10-03 15:24:18", 97 want: 6, 98 }, 99 { 100 s: "2020-08-23 21:53:09", 101 want: 6, 102 }, 103 } 104 105 var inStrs []string 106 var wantInt64 []int64 107 for _, k := range kases { 108 inStrs = append(inStrs, k.s) 109 wantInt64 = append(wantInt64, k.want) 110 } 111 112 inVector := testutil.MakeDateTimeVector(inStrs, nil) 113 wantVec := testutil.MakeInt64Vector(wantInt64, nil) 114 proc := testutil.NewProc() 115 res, err := DatetimeToWeekday([]*vector.Vector{inVector}, proc) 116 convey.So(err, convey.ShouldBeNil) 117 compare := testutil.CompareVectors(wantVec, res) 118 convey.So(compare, convey.ShouldBeTrue) 119 }) 120 121 convey.Convey("ScalarWeekDayDateTimeCase", t, func() { 122 type kase struct { 123 s string 124 want int64 125 } 126 127 k := kase{ 128 s: "2021-10-03 15:24:18", 129 want: 6, 130 } 131 132 inVector := testutil.MakeScalarDateTime(k.s, 10) 133 wantVec := testutil.MakeScalarInt64(k.want, 10) 134 proc := testutil.NewProc() 135 res, err := DatetimeToWeekday([]*vector.Vector{inVector}, proc) 136 convey.So(err, convey.ShouldBeNil) 137 compare := testutil.CompareVectors(wantVec, res) 138 convey.So(compare, convey.ShouldBeTrue) 139 }) 140 141 convey.Convey("ScalarDateNUllCase", t, func() { 142 inVector := testutil.MakeScalarNull(types.T_date, 10) 143 wantVec := testutil.MakeScalarNull(types.T_int64, 10) 144 proc := testutil.NewProc() 145 res, err := DateToWeekday([]*vector.Vector{inVector}, proc) 146 convey.So(err, convey.ShouldBeNil) 147 compare := testutil.CompareVectors(wantVec, res) 148 convey.So(compare, convey.ShouldBeTrue) 149 150 }) 151 152 convey.Convey("ScalarDateTimeNUllCase", t, func() { 153 inVector := testutil.MakeScalarNull(types.T_datetime, 10) 154 wantVec := testutil.MakeScalarNull(types.T_int64, 10) 155 proc := testutil.NewProc() 156 res, err := DatetimeToWeekday([]*vector.Vector{inVector}, proc) 157 convey.So(err, convey.ShouldBeNil) 158 compare := testutil.CompareVectors(wantVec, res) 159 convey.So(compare, convey.ShouldBeTrue) 160 161 }) 162 163 }