github.com/dolthub/go-mysql-server@v0.18.0/sql/core_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 sql_test
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  	"time"
    21  
    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  var conditions = []struct {
    30  	evaluated bool
    31  	value     interface{}
    32  	t         sql.Type
    33  }{
    34  	{true, int16(1), types.Int16},
    35  	{false, int16(0), types.Int16},
    36  	{true, int32(1), types.Int32},
    37  	{false, int32(0), types.Int32},
    38  	{true, int(1), types.Int64},
    39  	{false, int(0), types.Int64},
    40  	{true, float32(1), types.Float32},
    41  	{true, float64(1), types.Float64},
    42  	{false, float32(0), types.Float32},
    43  	{false, float64(0), types.Float64},
    44  	{true, float32(0.5), types.Float32},
    45  	{true, float64(0.5), types.Float64},
    46  	{true, float32(0.01), types.Float32},
    47  	{true, float64(0.01), types.Float64},
    48  	{true, float32(-0.01), types.Float32},
    49  	{true, float64(-0.01), types.Float64},
    50  	{true, "1", types.LongText},
    51  	{false, "0", types.LongText},
    52  	{false, "foo", types.LongText},
    53  	{true, "0.5", types.LongText},
    54  	{true, "0.01", types.LongText},
    55  	{true, "-0.01", types.LongText},
    56  	{false, time.Duration(0), types.Timestamp},
    57  	{true, time.Duration(1), types.Timestamp},
    58  	{false, false, types.Boolean},
    59  	{true, true, types.Boolean},
    60  }
    61  
    62  func TestEvaluateCondition(t *testing.T) {
    63  	for _, v := range conditions {
    64  		t.Run(fmt.Sprint(v.value, " evaluated to ", v.evaluated, " type ", v.t), func(t *testing.T) {
    65  			require := require.New(t)
    66  			b, err := sql.EvaluateCondition(sql.NewEmptyContext(), expression.NewLiteral(v.value, v.t), sql.NewRow())
    67  			require.NoError(err)
    68  			require.Equal(v.evaluated, b)
    69  		})
    70  	}
    71  }