github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/logic_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 TestAnd(t *testing.T) { 27 var testCases = []struct { 28 name string 29 left, right interface{} 30 expected interface{} 31 }{ 32 {"left is true, right is false", true, false, false}, 33 {"left is true, right is null", true, nil, nil}, 34 {"left is false, right is true", false, true, false}, 35 {"left is null, right is true", nil, true, nil}, 36 {"left is false, right is null", false, nil, false}, 37 {"left is null, right is false", nil, false, false}, 38 {"both true", true, true, true}, 39 {"both false", false, false, false}, 40 {"both nil", nil, nil, nil}, 41 {"left is string, right is string", "dsdad", "dasa", false}, 42 {"left is string, right is nil", "ads", nil, false}, 43 {"left is nil, right is string", nil, "dada", false}, 44 } 45 46 for _, tt := range testCases { 47 t.Run(tt.name, func(t *testing.T) { 48 require := require.New(t) 49 result, err := NewAnd( 50 NewLiteral(tt.left, types.Boolean), 51 NewLiteral(tt.right, types.Boolean), 52 ).Eval(sql.NewEmptyContext(), sql.NewRow()) 53 require.NoError(err) 54 require.Equal(tt.expected, result) 55 56 // also test JoinAnd 57 result, err = JoinAnd( 58 NewLiteral(tt.left, types.Boolean), 59 NewLiteral(tt.right, types.Boolean), 60 ).Eval(sql.NewEmptyContext(), sql.NewRow()) 61 require.NoError(err) 62 require.Equal(tt.expected, result) 63 }) 64 } 65 } 66 67 func TestOr(t *testing.T) { 68 var testCases = []struct { 69 name string 70 left, right interface{} 71 expected interface{} 72 }{ 73 {"left is true, right is false", true, false, true}, 74 {"left is null, right is true", nil, true, true}, 75 {"left is null, right is false", nil, false, nil}, 76 {"left is false, right is true", false, true, true}, 77 {"left is true, right is null", true, nil, true}, 78 {"left is false, right is null", false, nil, nil}, 79 {"both true", true, true, true}, 80 {"both false", false, false, false}, 81 {"both null", nil, nil, nil}, 82 {"left is string, right is different string", "abc", "def", false}, 83 {"left is string, right is nil", "abc", nil, nil}, 84 {"left is nil, right is string", nil, "def", nil}, 85 {"left is float, right is string", 2.0, "hello", true}, 86 } 87 88 for _, tt := range testCases { 89 t.Run(tt.name, func(t *testing.T) { 90 require := require.New(t) 91 result, err := NewOr( 92 NewLiteral(tt.left, types.Boolean), 93 NewLiteral(tt.right, types.Boolean), 94 ).Eval(sql.NewEmptyContext(), sql.NewRow()) 95 require.NoError(err) 96 require.Equal(tt.expected, result) 97 98 // also test JoinOr 99 result, err = JoinOr( 100 NewLiteral(tt.left, types.Boolean), 101 NewLiteral(tt.right, types.Boolean), 102 ).Eval(sql.NewEmptyContext(), sql.NewRow()) 103 require.NoError(err) 104 require.Equal(tt.expected, result) 105 }) 106 } 107 } 108 109 func TestXor(t *testing.T) { 110 var testCases = []struct { 111 name string 112 left, right interface{} 113 expected interface{} 114 }{ 115 {"left is true, right is false", true, false, true}, 116 {"left is null, right is true", nil, true, nil}, 117 {"left is null, right is false", nil, false, nil}, 118 {"left is false, right is true", false, true, true}, 119 {"left is true, right is null", true, nil, nil}, 120 {"left is false, right is null", false, nil, nil}, 121 {"both true", true, true, false}, 122 {"both false", false, false, false}, 123 {"both null", nil, nil, nil}, 124 {"left is string, right is different string", "abc", "def", false}, 125 {"left is string, right is nil", "abc", nil, nil}, 126 {"left is nil, right is string", nil, "def", nil}, 127 {"left is float, right is string", 2.0, "hello", true}, 128 } 129 130 for _, tt := range testCases { 131 t.Run(tt.name, func(t *testing.T) { 132 require := require.New(t) 133 result, err := NewXor( 134 NewLiteral(tt.left, types.Boolean), 135 NewLiteral(tt.right, types.Boolean), 136 ).Eval(sql.NewEmptyContext(), sql.NewRow()) 137 require.NoError(err) 138 require.Equal(tt.expected, result) 139 }) 140 } 141 } 142 143 func TestJoinAnd(t *testing.T) { 144 require := require.New(t) 145 146 require.Nil(JoinAnd()) 147 148 require.Equal( 149 NewNot(nil), 150 JoinAnd(NewNot(nil)), 151 ) 152 153 require.Equal( 154 NewAnd( 155 NewAnd( 156 NewIsNull(nil), 157 NewEquals(nil, nil), 158 ), 159 NewNot(nil), 160 ), 161 JoinAnd( 162 NewIsNull(nil), 163 NewEquals(nil, nil), 164 NewNot(nil), 165 ), 166 ) 167 }