vitess.io/vitess@v0.16.2/go/vt/sqlparser/parse_date_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestParseDate(t *testing.T) {
    28  	type date struct {
    29  		year  int
    30  		month time.Month
    31  		day   int
    32  	}
    33  	tests := []struct {
    34  		input  string
    35  		output date
    36  		err    bool
    37  	}{{
    38  		input:  "2022-10-12",
    39  		output: date{2022, time.October, 12},
    40  	}, {
    41  		input:  "22-10-12",
    42  		output: date{2022, time.October, 12},
    43  	}, {
    44  		input:  "20221012",
    45  		output: date{2022, time.October, 12},
    46  	}, {
    47  		input:  "221012",
    48  		output: date{2022, time.October, 12},
    49  	}, {
    50  		input: "2022",
    51  		err:   true,
    52  	}}
    53  
    54  	for _, test := range tests {
    55  		t.Run(test.input, func(t *testing.T) {
    56  			got, err := ParseDate(test.input)
    57  			if test.err {
    58  				assert.Error(t, err)
    59  				return
    60  			}
    61  
    62  			require.NoError(t, err)
    63  			assert.Equal(t, test.output.year, got.Year())
    64  			assert.Equal(t, test.output.month, got.Month())
    65  			assert.Equal(t, test.output.day, got.Day())
    66  		})
    67  	}
    68  }
    69  
    70  func TestParseTime(t *testing.T) {
    71  	type testTime struct {
    72  		hour       int
    73  		minute     int
    74  		second     int
    75  		nanosecond int
    76  	}
    77  	tests := []struct {
    78  		input  string
    79  		output testTime
    80  		err    bool
    81  	}{{
    82  		input:  "11:12:13",
    83  		output: testTime{11, 12, 13, 0},
    84  	}, {
    85  		input:  "11:12:13.123456",
    86  		output: testTime{11, 12, 13, 123456000},
    87  	}, {
    88  		input:  "3 11:12:13",
    89  		output: testTime{3*24 + 11, 12, 13, 0},
    90  	}, {
    91  		input: "35 11:12:13",
    92  		err:   true,
    93  	}, {
    94  		input:  "11:12",
    95  		output: testTime{11, 12, 0, 0},
    96  	}, {
    97  		input:  "5 11:12",
    98  		output: testTime{5*24 + 11, 12, 0, 0},
    99  	}, {
   100  		input:  "-2 11:12",
   101  		output: testTime{-2*24 - 11, -12, 0, 0},
   102  	}, {
   103  		input: "--2 11:12",
   104  		err:   true,
   105  	}, {
   106  		input:  "2 11",
   107  		output: testTime{2*24 + 11, 0, 0, 0},
   108  	}, {
   109  		input: "2 -11",
   110  		err:   true,
   111  	}, {
   112  		input:  "13",
   113  		output: testTime{0, 0, 13, 0},
   114  	}, {
   115  		input:  "111213",
   116  		output: testTime{11, 12, 13, 0},
   117  	}, {
   118  		input:  "111213.123456",
   119  		output: testTime{11, 12, 13, 123456000},
   120  	}, {
   121  		input:  "-111213",
   122  		output: testTime{-11, -12, -13, 0},
   123  	}, {
   124  		input:  "1213",
   125  		output: testTime{0, 12, 13, 0},
   126  	}, {
   127  		input: "25:12:13",
   128  		err:   true,
   129  	}}
   130  
   131  	for _, test := range tests {
   132  		t.Run(test.input, func(t *testing.T) {
   133  			got, err := ParseTime(test.input)
   134  			if test.err {
   135  				assert.Errorf(t, err, "got: %s", got)
   136  				return
   137  			}
   138  
   139  			require.NoError(t, err)
   140  			now := time.Now()
   141  			startOfToday := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.UTC)
   142  			expected := startOfToday.Add(time.Duration(test.output.hour)*time.Hour +
   143  				time.Duration(test.output.minute)*time.Minute +
   144  				time.Duration(test.output.second)*time.Second +
   145  				time.Duration(test.output.nanosecond)*time.Nanosecond)
   146  
   147  			assert.Equal(t, expected, got)
   148  		})
   149  	}
   150  }
   151  
   152  func TestParseDateTime(t *testing.T) {
   153  	type datetime struct {
   154  		year       int
   155  		month      time.Month
   156  		day        int
   157  		hour       int
   158  		minute     int
   159  		second     int
   160  		nanosecond int
   161  	}
   162  	tests := []struct {
   163  		input  string
   164  		output datetime
   165  		err    bool
   166  	}{{
   167  		input:  "2022-10-12 11:12:13",
   168  		output: datetime{2022, time.October, 12, 11, 12, 13, 0},
   169  	}, {
   170  		input:  "2022-10-12 11:12:13.123456",
   171  		output: datetime{2022, time.October, 12, 11, 12, 13, 123456000},
   172  	}, {
   173  		input:  "20221012111213.123456",
   174  		output: datetime{2022, time.October, 12, 11, 12, 13, 123456000},
   175  	}}
   176  
   177  	for _, test := range tests {
   178  		t.Run(test.input, func(t *testing.T) {
   179  			got, err := ParseDateTime(test.input)
   180  			if test.err {
   181  				assert.Error(t, err)
   182  				return
   183  			}
   184  
   185  			require.NoError(t, err)
   186  			assert.Equal(t, test.output.year, got.Year())
   187  			assert.Equal(t, test.output.month, got.Month())
   188  			assert.Equal(t, test.output.day, got.Day())
   189  			assert.Equal(t, test.output.hour, got.Hour())
   190  			assert.Equal(t, test.output.minute, got.Minute())
   191  			assert.Equal(t, test.output.second, got.Second())
   192  			assert.Equal(t, test.output.nanosecond, got.Nanosecond())
   193  		})
   194  	}
   195  }