github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/convert_tz_test.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     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 function
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/expression"
    26  	"github.com/dolthub/go-mysql-server/sql/types"
    27  )
    28  
    29  func TestConvertTz(t *testing.T) {
    30  	tests := []struct {
    31  		name           string
    32  		datetime       interface{}
    33  		fromTimeZone   string
    34  		toTimeZone     string
    35  		expectedResult interface{}
    36  	}{
    37  		{
    38  			name:           "Simple timezone conversion",
    39  			datetime:       "2004-01-01 12:00:00",
    40  			fromTimeZone:   "GMT",
    41  			toTimeZone:     "MET",
    42  			expectedResult: time.Date(2004, 1, 1, 13, 0, 0, 0, time.UTC),
    43  		},
    44  		{
    45  			name:           "Simple timezone conversion as datetime object",
    46  			datetime:       time.Date(2004, 1, 1, 12, 0, 0, 0, time.UTC),
    47  			fromTimeZone:   "GMT",
    48  			toTimeZone:     "MET",
    49  			expectedResult: time.Date(2004, 1, 1, 13, 0, 0, 0, time.UTC),
    50  		},
    51  		{
    52  			name:           "Locations going backwards",
    53  			datetime:       "2004-01-01 12:00:00",
    54  			fromTimeZone:   "US/Eastern",
    55  			toTimeZone:     "US/Central",
    56  			expectedResult: time.Date(2004, 1, 1, 11, 0, 0, 0, time.UTC),
    57  		},
    58  		{
    59  			name:           "Locations going forward",
    60  			datetime:       "2004-01-01 12:00:00",
    61  			fromTimeZone:   "US/Central",
    62  			toTimeZone:     "US/Eastern",
    63  			expectedResult: time.Date(2004, 1, 1, 13, 0, 0, 0, time.UTC),
    64  		},
    65  		{
    66  			name:           "Simple time shift",
    67  			datetime:       "2004-01-01 12:00:00",
    68  			fromTimeZone:   "+01:00",
    69  			toTimeZone:     "+10:00",
    70  			expectedResult: time.Date(2004, 1, 1, 21, 0, 0, 0, time.UTC),
    71  		},
    72  		{
    73  			name:           "Simple time shift with minutes",
    74  			datetime:       "2004-01-01 12:00:00",
    75  			fromTimeZone:   "+01:00",
    76  			toTimeZone:     "+10:11",
    77  			expectedResult: time.Date(2004, 1, 1, 21, 11, 0, 0, time.UTC),
    78  		},
    79  		{
    80  			name:           "Different Time Format",
    81  			datetime:       "20100603121212",
    82  			fromTimeZone:   "+01:00",
    83  			toTimeZone:     "+10:00",
    84  			expectedResult: time.Date(2010, 6, 3, 21, 12, 12, 0, time.UTC),
    85  		},
    86  		{
    87  			name:           "From location string to offset string",
    88  			datetime:       "2004-01-01 12:00:00",
    89  			fromTimeZone:   "UTC",
    90  			toTimeZone:     "-10:00",
    91  			expectedResult: time.Date(2004, 1, 1, 2, 0, 0, 0, time.UTC),
    92  		},
    93  		{
    94  			name:           "From offset string to location string",
    95  			datetime:       "2004-01-01 17:00:00",
    96  			fromTimeZone:   "+10:00",
    97  			toTimeZone:     "US/Central",
    98  			expectedResult: time.Date(2004, 1, 1, 1, 0, 0, 0, time.UTC),
    99  		},
   100  		{
   101  			name:           "Bad timezone conversion",
   102  			datetime:       "2004-01-01 12:00:00",
   103  			fromTimeZone:   "GMT",
   104  			toTimeZone:     "HLP",
   105  			expectedResult: nil,
   106  		},
   107  		{
   108  			name:           "Bad Time Returns nils",
   109  			datetime:       "2004-01-01 12:00:00dsa",
   110  			fromTimeZone:   "+01:00",
   111  			toTimeZone:     "+10:00",
   112  			expectedResult: nil,
   113  		},
   114  		{
   115  			name:           "Bad Duration Returns nil",
   116  			datetime:       "2004-01-01 12:00:00",
   117  			fromTimeZone:   "+01:00",
   118  			toTimeZone:     "+10:00:11",
   119  			expectedResult: nil,
   120  		},
   121  		{
   122  			name:           "Negative time shift works accordingly",
   123  			datetime:       "2004-01-02 12:00:00",
   124  			fromTimeZone:   "-01:00",
   125  			toTimeZone:     "+10:11",
   126  			expectedResult: time.Date(2004, 1, 2, 23, 11, 0, 0, time.UTC),
   127  		},
   128  		{
   129  			name:           "Test With negatives and datetime type",
   130  			datetime:       time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC),
   131  			fromTimeZone:   "-01:00",
   132  			toTimeZone:     "+10:00",
   133  			expectedResult: time.Date(2010, 6, 3, 23, 12, 12, 0, time.UTC),
   134  		},
   135  		{
   136  			name:           "No symbol on toTimeZone errors",
   137  			datetime:       time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC),
   138  			fromTimeZone:   "-01:00",
   139  			toTimeZone:     "10:00",
   140  			expectedResult: nil,
   141  		},
   142  		{
   143  			name:           "Test fromTimeZone value: SYSTEM",
   144  			datetime:       time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC),
   145  			fromTimeZone:   "SYSTEM",
   146  			toTimeZone:     "+01:00",
   147  			expectedResult: time.Date(2010, 6, 3, 13, 12, 12, 0, time.UTC),
   148  		},
   149  		{
   150  			name:           "Test toTimeZone value: SYSTEM",
   151  			datetime:       time.Date(2010, 6, 3, 12, 12, 12, 0, time.UTC),
   152  			fromTimeZone:   "+01:00",
   153  			toTimeZone:     "SYSTEM",
   154  			expectedResult: time.Date(2010, 6, 3, 11, 12, 12, 0, time.UTC),
   155  		},
   156  	}
   157  
   158  	for _, test := range tests {
   159  		t.Run(test.name, func(t *testing.T) {
   160  			// Set the system timezone to a known value so we can test convert_tz with the SYSTEM param
   161  			loc, err := time.LoadLocation("UTC")
   162  			time.Local = loc
   163  			fn := NewConvertTz(expression.NewLiteral(test.datetime, types.Text), expression.NewLiteral(test.fromTimeZone, types.Text), expression.NewLiteral(test.toTimeZone, types.Text))
   164  
   165  			res, err := fn.Eval(sql.NewEmptyContext(), sql.Row{})
   166  			require.NoError(t, err)
   167  
   168  			assert.Equal(t, test.expectedResult, res)
   169  		})
   170  	}
   171  }