github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/month/month_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 month
    16  
    17  import (
    18  	"reflect"
    19  	"testing"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  )
    23  
    24  func ParseDateCast(s string) types.Date {
    25  	d, _ := types.ParseDateCast(s)
    26  	return d
    27  }
    28  
    29  func parseDatetime(s string) types.Datetime {
    30  	dt, _ := types.ParseDatetime(s, 6)
    31  	return dt
    32  }
    33  
    34  func TestDateToMonth(t *testing.T) {
    35  	type args struct {
    36  		xs []types.Date
    37  		rs []uint8
    38  	}
    39  	tests := []struct {
    40  		name string
    41  		args args
    42  		want []uint8
    43  	}{
    44  		{
    45  			name: "normal date test",
    46  			args: args{
    47  				xs: []types.Date{
    48  					ParseDateCast("2022-01-01"),
    49  					ParseDateCast("2022-02-02"),
    50  					ParseDateCast("2022-03-03"),
    51  					ParseDateCast("2022-04-01"),
    52  					ParseDateCast("2022-05-01"),
    53  					ParseDateCast("2022-06-01"),
    54  					ParseDateCast("2022-07-01"),
    55  					ParseDateCast("2022-08-01"),
    56  					ParseDateCast("2022-09-01"),
    57  					ParseDateCast("2022-10-01"),
    58  					ParseDateCast("2022-11-01"),
    59  					ParseDateCast("2022-12-01"),
    60  				},
    61  				rs: make([]uint8, 12),
    62  			},
    63  			want: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
    64  		},
    65  	}
    66  
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			if got := DateToMonth(tt.args.xs, tt.args.rs); !reflect.DeepEqual(got, tt.want) {
    70  				t.Errorf("DateToMonth() = %v, want %v", got, tt.want)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestDatetimeToMonth(t *testing.T) {
    77  	type args struct {
    78  		xs []types.Datetime
    79  		rs []uint8
    80  	}
    81  	tests := []struct {
    82  		name string
    83  		args args
    84  		want []uint8
    85  	}{
    86  		{
    87  			name: "normal date test",
    88  			args: args{
    89  				xs: []types.Datetime{
    90  					parseDatetime("2022-01-01 00:00:00"),
    91  					parseDatetime("2022-02-01 00:00:00"),
    92  					parseDatetime("2022-03-01 00:00:00"),
    93  					parseDatetime("2022-04-01 00:00:00"),
    94  					parseDatetime("2022-05-01 00:00:00"),
    95  					parseDatetime("2022-06-01 00:00:00"),
    96  					parseDatetime("2022-07-01 00:00:00"),
    97  					parseDatetime("2022-08-01 00:00:00"),
    98  					parseDatetime("2022-09-01 00:00:00"),
    99  					parseDatetime("2022-10-01 00:00:00"),
   100  					parseDatetime("2022-11-01 00:00:00"),
   101  					parseDatetime("2022-12-01 00:00:00"),
   102  				},
   103  				rs: make([]uint8, 12),
   104  			},
   105  			want: []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
   106  		},
   107  	}
   108  	for _, tt := range tests {
   109  		t.Run(tt.name, func(t *testing.T) {
   110  			if got := DatetimeToMonth(tt.args.xs, tt.args.rs); !reflect.DeepEqual(got, tt.want) {
   111  				t.Errorf("DatetimeToMonth() = %v, want %v", got, tt.want)
   112  			}
   113  		})
   114  	}
   115  }