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

     1  package function
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/dolthub/go-mysql-server/sql"
    10  	"github.com/dolthub/go-mysql-server/sql/expression"
    11  	"github.com/dolthub/go-mysql-server/sql/types"
    12  )
    13  
    14  func TestStrToDate(t *testing.T) {
    15  	setupTimezone(t)
    16  
    17  	testCases := [...]struct {
    18  		name     string
    19  		dateStr  string
    20  		fmtStr   string
    21  		expected string
    22  	}{
    23  		{"standard", "Dec 26, 2000 2:13:15", "%b %e, %Y %T", "2000-12-26 02:13:15"},
    24  	}
    25  
    26  	for _, tt := range testCases {
    27  		f, err := NewStrToDate(
    28  			expression.NewGetField(0, types.Text, "", true),
    29  			expression.NewGetField(1, types.Text, "", true),
    30  		)
    31  		if err != nil {
    32  			t.Fatal(err)
    33  		}
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			dtime := eval(t, f, sql.NewRow(tt.dateStr, tt.fmtStr))
    36  			require.Equal(t, tt.expected, dtime)
    37  		})
    38  		req := require.New(t)
    39  		req.True(f.IsNullable())
    40  	}
    41  }
    42  
    43  func TestStrToDateFailure(t *testing.T) {
    44  	setupTimezone(t)
    45  
    46  	testCases := [...]struct {
    47  		name    string
    48  		dateStr string
    49  		fmtStr  string
    50  	}{
    51  		{"standard", "BadMonth 26, 2000 2:13:15", "%b %e, %Y %T"},
    52  	}
    53  
    54  	for _, tt := range testCases {
    55  		f, err := NewStrToDate(
    56  			expression.NewGetField(0, types.Text, "", true),
    57  			expression.NewGetField(1, types.Text, "", true),
    58  		)
    59  		if err != nil {
    60  			t.Fatal(err)
    61  		}
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			dtime := eval(t, f, sql.NewRow(tt.dateStr, tt.fmtStr))
    64  			require.Equal(t, nil, dtime)
    65  		})
    66  		req := require.New(t)
    67  		req.True(f.IsNullable())
    68  	}
    69  }
    70  
    71  func setupTimezone(t *testing.T) {
    72  	loc, err := time.LoadLocation("America/Chicago")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	old := time.Local
    77  	time.Local = loc
    78  	t.Cleanup(func() { time.Local = old })
    79  }