github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/istrue_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 expression
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/stretchr/testify/require"
    21  
    22  	"github.com/dolthub/go-mysql-server/sql"
    23  	"github.com/dolthub/go-mysql-server/sql/types"
    24  )
    25  
    26  func TestIsTrue(t *testing.T) {
    27  	require := require.New(t)
    28  
    29  	boolF := NewGetField(0, types.Boolean, "col1", true)
    30  	e := NewIsTrue(boolF)
    31  	require.Equal(types.Boolean, e.Type())
    32  	require.False(e.IsNullable())
    33  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    34  	require.Equal(true, eval(t, e, sql.NewRow(true)))
    35  	require.Equal(false, eval(t, e, sql.NewRow(false)))
    36  
    37  	intF := NewGetField(0, types.Int64, "col1", true)
    38  	e = NewIsTrue(intF)
    39  	require.Equal(types.Boolean, e.Type())
    40  	require.False(e.IsNullable())
    41  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    42  	require.Equal(true, eval(t, e, sql.NewRow(100)))
    43  	require.Equal(true, eval(t, e, sql.NewRow(-1)))
    44  	require.Equal(false, eval(t, e, sql.NewRow(0)))
    45  
    46  	floatF := NewGetField(0, types.Float64, "col1", true)
    47  	e = NewIsTrue(floatF)
    48  	require.Equal(types.Boolean, e.Type())
    49  	require.False(e.IsNullable())
    50  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    51  	require.Equal(true, eval(t, e, sql.NewRow(1.5)))
    52  	require.Equal(true, eval(t, e, sql.NewRow(-1.5)))
    53  	require.Equal(false, eval(t, e, sql.NewRow(0)))
    54  
    55  	stringF := NewGetField(0, types.Text, "col1", true)
    56  	e = NewIsTrue(stringF)
    57  	require.Equal(types.Boolean, e.Type())
    58  	require.False(e.IsNullable())
    59  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    60  	require.Equal(false, eval(t, e, sql.NewRow("")))
    61  	require.Equal(false, eval(t, e, sql.NewRow("false")))
    62  	require.Equal(false, eval(t, e, sql.NewRow("true")))
    63  }
    64  
    65  func TestIsFalse(t *testing.T) {
    66  	require := require.New(t)
    67  
    68  	boolF := NewGetField(0, types.Boolean, "col1", true)
    69  	e := NewIsFalse(boolF)
    70  	require.Equal(types.Boolean, e.Type())
    71  	require.False(e.IsNullable())
    72  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    73  	require.Equal(false, eval(t, e, sql.NewRow(true)))
    74  	require.Equal(true, eval(t, e, sql.NewRow(false)))
    75  
    76  	intF := NewGetField(0, types.Int64, "col1", true)
    77  	e = NewIsFalse(intF)
    78  	require.Equal(types.Boolean, e.Type())
    79  	require.False(e.IsNullable())
    80  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    81  	require.Equal(false, eval(t, e, sql.NewRow(100)))
    82  	require.Equal(false, eval(t, e, sql.NewRow(-1)))
    83  	require.Equal(true, eval(t, e, sql.NewRow(0)))
    84  
    85  	floatF := NewGetField(0, types.Float64, "col1", true)
    86  	e = NewIsFalse(floatF)
    87  	require.Equal(types.Boolean, e.Type())
    88  	require.False(e.IsNullable())
    89  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    90  	require.Equal(false, eval(t, e, sql.NewRow(1.5)))
    91  	require.Equal(false, eval(t, e, sql.NewRow(-1.5)))
    92  	require.Equal(true, eval(t, e, sql.NewRow(0)))
    93  
    94  	stringF := NewGetField(0, types.Text, "col1", true)
    95  	e = NewIsFalse(stringF)
    96  	require.Equal(types.Boolean, e.Type())
    97  	require.False(e.IsNullable())
    98  	require.Equal(false, eval(t, e, sql.NewRow(nil)))
    99  	require.Equal(true, eval(t, e, sql.NewRow("")))
   100  	require.Equal(true, eval(t, e, sql.NewRow("false")))
   101  	require.Equal(true, eval(t, e, sql.NewRow("true")))
   102  }