github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/date_sub/date_sub_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  package date_sub
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/nulls"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestDateSub(t *testing.T) {
    27  	testCases := []struct {
    28  		name  string
    29  		args1 []types.Date
    30  		args2 []int64
    31  		args3 []int64
    32  		want  []types.Date
    33  	}{
    34  		{
    35  			args1: []types.Date{types.DateFromCalendar(2021, 8, 13)},
    36  			args2: []int64{1},
    37  			args3: []int64{int64(types.Day)},
    38  			want:  []types.Date{types.DateFromCalendar(2021, 8, 12)},
    39  		},
    40  		{
    41  			args1: []types.Date{types.DateFromCalendar(2021, 1, 1)},
    42  			args2: []int64{1},
    43  			args3: []int64{int64(types.Day)},
    44  			want:  []types.Date{types.DateFromCalendar(2020, 12, 31)},
    45  		},
    46  	}
    47  
    48  	for _, c := range testCases {
    49  		t.Run(c.name, func(t *testing.T) {
    50  			got := make([]types.Date, len(c.args1))
    51  			xnu := &nulls.Nulls{}
    52  			ynu := &nulls.Nulls{}
    53  			rnu := &nulls.Nulls{}
    54  			d, e := DateSub(c.args1, c.args2, c.args3, xnu, ynu, rnu, got)
    55  			require.Equal(t, c.want, d)
    56  			require.Equal(t, e, nil)
    57  		})
    58  	}
    59  
    60  }
    61  
    62  func TestDatetimeSub(t *testing.T) {
    63  	testCases := []struct {
    64  		name  string
    65  		args1 []types.Datetime
    66  		args2 []int64
    67  		args3 []int64
    68  		want  []types.Datetime
    69  	}{
    70  		{
    71  			args1: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 1)},
    72  			args2: []int64{1},
    73  			args3: []int64{int64(types.MicroSecond)},
    74  			want:  []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 0)},
    75  		},
    76  		{
    77  			args1: []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 1, 1, 1)},
    78  			args2: []int64{2},
    79  			args3: []int64{int64(types.Second)},
    80  			want:  []types.Datetime{types.DatetimeFromClock(2020, 1, 1, 1, 0, 59, 1)},
    81  		},
    82  	}
    83  
    84  	for _, c := range testCases {
    85  		t.Run(c.name, func(t *testing.T) {
    86  			got := make([]types.Datetime, len(c.args1))
    87  			xnu := &nulls.Nulls{}
    88  			ynu := &nulls.Nulls{}
    89  			rnu := &nulls.Nulls{}
    90  			d, e := DatetimeSub(c.args1, c.args2, c.args3, xnu, ynu, rnu, got)
    91  			require.Equal(t, c.want, d)
    92  			require.Equal(t, e, nil)
    93  		})
    94  	}
    95  
    96  }
    97  
    98  func TestDateStringSub(t *testing.T) {
    99  	testCases := []struct {
   100  		name    string
   101  		args1   []string
   102  		args2   []int64
   103  		args3   []int64
   104  		want    []types.Datetime
   105  		contain bool
   106  	}{
   107  		{
   108  			args1:   []string{"2018-01-02"},
   109  			args2:   []int64{1},
   110  			args3:   []int64{int64(types.Day)},
   111  			want:    []types.Datetime{types.DatetimeFromClock(2018, 1, 1, 0, 0, 0, 0)},
   112  			contain: false,
   113  		},
   114  		{
   115  			args1:   []string{"2018-01-02"},
   116  			args2:   []int64{1},
   117  			args3:   []int64{int64(types.Second)},
   118  			want:    []types.Datetime{types.DatetimeFromClock(2018, 1, 1, 23, 59, 59, 0)},
   119  			contain: false,
   120  		},
   121  		{
   122  			args1:   []string{"2018-01-01 00:00:02"},
   123  			args2:   []int64{1},
   124  			args3:   []int64{int64(types.Second)},
   125  			want:    []types.Datetime{types.DatetimeFromClock(2018, 1, 1, 0, 0, 1, 0)},
   126  			contain: false,
   127  		},
   128  	}
   129  
   130  	for _, c := range testCases {
   131  		t.Run(c.name, func(t *testing.T) {
   132  			got := make([]types.Datetime, len(c.args1))
   133  			xnu := &nulls.Nulls{}
   134  			ynu := &nulls.Nulls{}
   135  			rnu := &nulls.Nulls{}
   136  			d, e := DateStringSub(c.args1, c.args2, c.args3, xnu, ynu, rnu, got)
   137  			require.Equal(t, c.want, d)
   138  			require.Equal(t, e, nil)
   139  		})
   140  	}
   141  
   142  }
   143  
   144  func TestTimeStampSub(t *testing.T) {
   145  	testCases := []struct {
   146  		name    string
   147  		args1   []types.Timestamp
   148  		args2   []int64
   149  		args3   []int64
   150  		want    []types.Timestamp
   151  		success bool
   152  	}{
   153  		{
   154  			args1:   []types.Timestamp{types.FromClockUTC(2020, 1, 1, 1, 1, 1, 1)},
   155  			args2:   []int64{1},
   156  			args3:   []int64{int64(types.MicroSecond)},
   157  			want:    []types.Timestamp{types.FromClockUTC(2020, 1, 1, 1, 1, 1, 0)},
   158  			success: true,
   159  		},
   160  	}
   161  
   162  	for _, c := range testCases {
   163  		t.Run(c.name, func(t *testing.T) {
   164  			got := make([]types.Timestamp, len(c.args1))
   165  			xnu := &nulls.Nulls{}
   166  			ynu := &nulls.Nulls{}
   167  			rnu := &nulls.Nulls{}
   168  			rs, err := TimestampSub(time.Local, c.args1, c.args2, c.args3, xnu, ynu, rnu, got)
   169  			require.Equal(t, c.want, rs)
   170  			require.Equal(t, err, nil)
   171  		})
   172  	}
   173  
   174  }