github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/sleep_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/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  	"github.com/dolthub/go-mysql-server/sql/expression"
    25  	"github.com/dolthub/go-mysql-server/sql/types"
    26  )
    27  
    28  func TestSleep(t *testing.T) {
    29  	f := NewSleep(
    30  		expression.NewGetField(0, types.LongText, "n", false),
    31  	)
    32  	testCases := []struct {
    33  		name     string
    34  		row      sql.Row
    35  		expected interface{}
    36  		waitTime float64
    37  		err      bool
    38  	}{
    39  		{"null input", sql.NewRow(nil), nil, 0, false},
    40  		{"string input", sql.NewRow("foo"), nil, 0, true},
    41  		{"int input", sql.NewRow(3), int(0), 3.0, false},
    42  		{"number is zero", sql.NewRow(0), int(0), 0, false},
    43  		{"negative number", sql.NewRow(-4), int(0), 0, false},
    44  		{"positive number", sql.NewRow(4.48), int(0), 4.48, false},
    45  	}
    46  	for _, tt := range testCases {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			t.Helper()
    49  			require := require.New(t)
    50  			ctx := sql.NewEmptyContext()
    51  
    52  			t1 := time.Now()
    53  			v, err := f.Eval(ctx, tt.row)
    54  			t2 := time.Now()
    55  			if tt.err {
    56  				require.Error(err)
    57  			} else {
    58  				require.NoError(err)
    59  				require.Equal(tt.expected, v)
    60  
    61  				waited := t2.Sub(t1).Seconds()
    62  				require.InDelta(waited, tt.waitTime, 0.2)
    63  			}
    64  		})
    65  	}
    66  }