github.com/matrixorigin/matrixone@v0.7.0/pkg/vectorize/timediff/timediff_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 timediff
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/container/types"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestTimeDiffInTime(t *testing.T) {
    25  	testCases := []struct {
    26  		name        string
    27  		firstValue  string
    28  		secondValue string
    29  		want        string
    30  		success     bool
    31  	}{
    32  		{
    33  			name:        "TestCase01",
    34  			firstValue:  "22:22:22",
    35  			secondValue: "11:11:11",
    36  			want:        "11:11:11",
    37  		},
    38  		{
    39  			name:        "TestCase02",
    40  			firstValue:  "22:22:22",
    41  			secondValue: "-11:11:11",
    42  			want:        "33:33:33",
    43  		},
    44  		{
    45  			name:        "TestCase03",
    46  			firstValue:  "-22:22:22",
    47  			secondValue: "11:11:11",
    48  			want:        "-33:33:33",
    49  		},
    50  		{
    51  			name:        "TestCase04",
    52  			firstValue:  "-22:22:22",
    53  			secondValue: "-11:11:11",
    54  			want:        "-11:11:11",
    55  		},
    56  		{
    57  			name:        "TestCase05",
    58  			firstValue:  "11:11:11",
    59  			secondValue: "22:22:22",
    60  			want:        "-11:11:11",
    61  		},
    62  		{
    63  			name:        "TestCase06",
    64  			firstValue:  "11:11:11",
    65  			secondValue: "-22:22:22",
    66  			want:        "33:33:33",
    67  		},
    68  		{
    69  			name:        "TestCase07",
    70  			firstValue:  "-11:11:11",
    71  			secondValue: "-22:22:22",
    72  			want:        "11:11:11",
    73  		},
    74  		{
    75  			name:        "TestCase08",
    76  			firstValue:  "-11:11:11",
    77  			secondValue: "22:22:22",
    78  			want:        "-33:33:33",
    79  		},
    80  		{
    81  			name:        "TestCase09",
    82  			firstValue:  "-838:59:59",
    83  			secondValue: "-838:59:59",
    84  			want:        "00:00:00",
    85  		},
    86  		{
    87  			name:        "TestCase10",
    88  			firstValue:  "838:59:59",
    89  			secondValue: "838:59:59",
    90  			want:        "00:00:00",
    91  		},
    92  		{
    93  			name:        "TestCase11",
    94  			firstValue:  "2562047787:00:00",
    95  			secondValue: "-2562047787:00:00",
    96  			want:        "2562047787:59:59",
    97  		},
    98  		{
    99  			name:        "TestCase11",
   100  			firstValue:  "-2562047787:00:00",
   101  			secondValue: "2562047787:00:00",
   102  			want:        "-2562047787:59:59",
   103  		},
   104  	}
   105  
   106  	for _, v := range testCases {
   107  		//input
   108  		firstV, err := types.ParseTime(v.firstValue, 0)
   109  		require.NoError(t, err)
   110  		secondV, err := types.ParseTime(v.secondValue, 0)
   111  		require.NoError(t, err)
   112  
   113  		//result
   114  		res, _ := timeDiff(firstV, secondV)
   115  		want, _ := types.ParseTime(v.want, 0)
   116  
   117  		//test
   118  		require.NoError(t, err)
   119  		require.Equal(t, res, want)
   120  	}
   121  }
   122  
   123  func TestTimeDiffInDateTime(t *testing.T) {
   124  	testCases := []struct {
   125  		name        string
   126  		firstValue  string
   127  		secondValue string
   128  		want        string
   129  		success     bool
   130  	}{
   131  		{
   132  			name:        "TestCase01",
   133  			firstValue:  "2012-12-12 22:22:22",
   134  			secondValue: "2012-12-12 11:11:11",
   135  			want:        "11:11:11",
   136  		},
   137  		{
   138  			name:        "TestCase02",
   139  			firstValue:  "2012-12-12 11:11:11",
   140  			secondValue: "2012-12-12 22:22:22",
   141  			want:        "-11:11:11",
   142  		},
   143  		{
   144  			name:        "TestCase03",
   145  			firstValue:  "2012-12-12 22:22:22",
   146  			secondValue: "2012-12-10 11:11:11",
   147  			want:        "59:11:11",
   148  		},
   149  		{
   150  			name:        "TestCase04",
   151  			firstValue:  "2012-12-10 11:11:11",
   152  			secondValue: "2012-12-12 22:22:22",
   153  			want:        "-59:11:11",
   154  		},
   155  		{
   156  			name:        "TestCase05",
   157  			firstValue:  "2012-12-10 11:11:11",
   158  			secondValue: "2012-12-10 11:11:11",
   159  			want:        "00:00:00",
   160  		},
   161  	}
   162  
   163  	for _, v := range testCases {
   164  		//input
   165  		firstV, err := types.ParseDatetime(v.firstValue, 0)
   166  		require.NoError(t, err)
   167  		secondV, err := types.ParseDatetime(v.secondValue, 0)
   168  		require.NoError(t, err)
   169  
   170  		//result
   171  		res, _ := timeDiff(firstV, secondV)
   172  		want, _ := types.ParseTime(v.want, 0)
   173  
   174  		//test
   175  		require.NoError(t, err)
   176  		require.Equal(t, res, want)
   177  	}
   178  }