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

     1  // Copyright 2020-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  
    23  	"github.com/dolthub/go-mysql-server/sql/expression"
    24  	"github.com/dolthub/go-mysql-server/sql/types"
    25  )
    26  
    27  func TestTimeFormatting(t *testing.T) {
    28  	dt := time.Date(2020, 2, 3, 4, 5, 6, 7000, time.UTC)
    29  	tests := []struct {
    30  		formatStr string
    31  		expected  string
    32  		expectErr bool
    33  	}{
    34  		{"%f", "000007", false},               // Microseconds (000000 to 999999)
    35  		{"%h %p--%f", "04 AM--000007", false}, // Microseconds (000000 to 999999)
    36  		{"%H", "04", false},                   // Hour (00 to 23)
    37  		{"%h", "04", false},                   // Hour (00 to 12)
    38  		{"%I", "04", false},                   // Hour (00 to 12)
    39  		{"%i", "05", false},                   // Minutes (00 to 59)
    40  		{"%p", "AM", false},                   // AM or PM
    41  		{"%r", "04:05:06 AM", false},          // Time in 12 hour AM or PM format (hh:mm:ss AM/PM)
    42  		{"%S", "06", false},                   // Seconds (00 to 59)
    43  		{"%s", "06", false},                   // Seconds (00 to 59)
    44  		{"%T", "04:05:06", false},             // Time in 24 hour format (hh:mm:ss)
    45  		{"%U", "U", false},                    // Assert that unsupported (date) verbs are ignored
    46  		{"%z", "z", false},                    // Assert that unsupported (unknown) verbs are ignored
    47  	}
    48  
    49  	for _, test := range tests {
    50  		t.Run(dt.String()+test.formatStr, func(t *testing.T) {
    51  			result, err := formatTime(test.formatStr, dt)
    52  
    53  			if test.expectErr {
    54  				assert.Error(t, err)
    55  			} else {
    56  				assert.NoError(t, err)
    57  				t.Log(result)
    58  				assert.Equal(t, test.expected, result)
    59  			}
    60  		})
    61  	}
    62  }
    63  
    64  func TestTimeFormatEval(t *testing.T) {
    65  	timeLit := expression.NewLiteral("04:05:06.000007", types.Time)
    66  	format := expression.NewLiteral("%H-%i-%s|%f", types.Text)
    67  	nullLiteral := expression.NewLiteral(nil, types.Null)
    68  
    69  	timeFormat := NewTimeFormat(timeLit, format)
    70  	res, err := timeFormat.Eval(nil, nil)
    71  	assert.NoError(t, err)
    72  	assert.Equal(t, "04-05-06|000007", res)
    73  
    74  	timeFormat = NewTimeFormat(timeLit, nil)
    75  	res, err = timeFormat.Eval(nil, nil)
    76  	assert.NoError(t, err)
    77  	assert.Nil(t, res)
    78  
    79  	timeFormat = NewTimeFormat(nil, format)
    80  	res, err = timeFormat.Eval(nil, nil)
    81  	assert.NoError(t, err)
    82  	assert.Nil(t, res)
    83  
    84  	timeFormat = NewTimeFormat(timeLit, nullLiteral)
    85  	res, err = timeFormat.Eval(nil, nil)
    86  	assert.NoError(t, err)
    87  	assert.Nil(t, res)
    88  
    89  	timeFormat = NewTimeFormat(nullLiteral, format)
    90  	res, err = timeFormat.Eval(nil, nil)
    91  	assert.NoError(t, err)
    92  	assert.Nil(t, res)
    93  }