github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/function/builtin/unary/dayofyear_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 TestDayOfYear(t *testing.T) {
    27  	convey.Convey("RightCase", t, func() {
    28  		type kase struct {
    29  			s    string
    30  			want uint16
    31  		}
    32  
    33  		kases := []kase{
    34  			{
    35  				s:    "1999-04-05",
    36  				want: 95,
    37  			},
    38  			{
    39  				s:    "2004-04-03",
    40  				want: 94,
    41  			},
    42  		}
    43  
    44  		var intStrs []string
    45  		var outUint16 []uint16
    46  		for _, k := range kases {
    47  			intStrs = append(intStrs, k.s)
    48  			outUint16 = append(outUint16, k.want)
    49  		}
    50  
    51  		inVector := testutil.MakeDateVector(intStrs, nil)
    52  		wantVec := testutil.MakeUint16Vector(outUint16, nil)
    53  		proc := testutil.NewProc()
    54  		res, err := DayOfYear([]*vector.Vector{inVector}, proc)
    55  		convey.So(err, convey.ShouldBeNil)
    56  		compare := testutil.CompareVectors(wantVec, res)
    57  		convey.So(compare, convey.ShouldBeTrue)
    58  	})
    59  
    60  	convey.Convey("ScalarCase", t, func() {
    61  		type kase struct {
    62  			s    string
    63  			want uint16
    64  		}
    65  
    66  		k := kase{
    67  			s:    "2004-09-03",
    68  			want: 247,
    69  		}
    70  
    71  		inVector := testutil.MakeScalarDate(k.s, 10)
    72  		wantVec := testutil.MakeScalarUint16(k.want, 10)
    73  		proc := testutil.NewProc()
    74  		res, err := DayOfYear([]*vector.Vector{inVector}, proc)
    75  		convey.So(err, convey.ShouldBeNil)
    76  		compare := testutil.CompareVectors(wantVec, res)
    77  		convey.So(compare, convey.ShouldBeTrue)
    78  	})
    79  
    80  	convey.Convey("NullCase", t, func() {
    81  		inVector := testutil.MakeScalarNull(types.T_date, 10)
    82  		wantVec := testutil.MakeScalarNull(types.T_uint16, 10)
    83  		proc := testutil.NewProc()
    84  		res, err := DayOfYear([]*vector.Vector{inVector}, proc)
    85  		convey.So(err, convey.ShouldBeNil)
    86  		compare := testutil.CompareVectors(wantVec, res)
    87  		convey.So(compare, convey.ShouldBeTrue)
    88  	})
    89  
    90  }