github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/week/week_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  
    15  package week
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestDateToWeek(t *testing.T) {
    25  	cases := []struct {
    26  		name      string
    27  		inputDate []types.Date
    28  		expected  []uint8
    29  	}{
    30  		{
    31  			name: "Date to week for ",
    32  			inputDate: []types.Date{
    33  				types.DateFromCalendar(2003, 12, 30),
    34  				types.DateFromCalendar(2004, 1, 2),
    35  
    36  				types.DateFromCalendar(2004, 12, 31),
    37  				types.DateFromCalendar(2005, 1, 1),
    38  
    39  				types.DateFromCalendar(2001, 2, 16),
    40  				types.DateFromCalendar(2012, 6, 18),
    41  				types.DateFromCalendar(2015, 9, 25),
    42  				types.DateFromCalendar(2022, 12, 5),
    43  			},
    44  			expected: []uint8{1, 1, 53, 53, 7, 25, 39, 49},
    45  		},
    46  	}
    47  
    48  	for _, c := range cases {
    49  		t.Run(c.name, func(t *testing.T) {
    50  			result := make([]uint8, len(c.inputDate))
    51  			require.Equal(t, c.expected, DateToWeek(c.inputDate, result))
    52  		})
    53  	}
    54  }
    55  
    56  func TestDatetimeToWeek(t *testing.T) {
    57  	cases := []struct {
    58  		name          string
    59  		inputDatetime []types.Datetime
    60  		expected      []uint8
    61  	}{
    62  		{
    63  			name: "Datetime to day test",
    64  			inputDatetime: []types.Datetime{
    65  				types.DatetimeFromClock(2003, 12, 30, 0, 0, 0, 0),
    66  				types.DatetimeFromClock(2004, 1, 2, 0, 0, 0, 0),
    67  
    68  				types.DatetimeFromClock(2004, 12, 31, 0, 0, 0, 0),
    69  				types.DatetimeFromClock(2005, 1, 1, 0, 0, 0, 0),
    70  
    71  				types.DatetimeFromClock(2001, 2, 16, 0, 0, 0, 0),
    72  				types.DatetimeFromClock(2012, 6, 18, 0, 0, 0, 0),
    73  				types.DatetimeFromClock(2015, 9, 25, 0, 0, 0, 0),
    74  				types.DatetimeFromClock(2022, 12, 5, 0, 0, 0, 0),
    75  			},
    76  			expected: []uint8{1, 1, 53, 53, 7, 25, 39, 49},
    77  		},
    78  	}
    79  
    80  	for _, c := range cases {
    81  		t.Run(c.name, func(t *testing.T) {
    82  			result := make([]uint8, len(c.inputDatetime))
    83  			require.Equal(t, c.expected, DatetimeToWeek(c.inputDatetime, result))
    84  		})
    85  	}
    86  }