Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit e1d8da3

Browse files
authored
Add test to EvaluateCondition function (#771)
Add test to EvaluateCondition function
2 parents d9b20c8 + 8021ddf commit e1d8da3

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

sql/core.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"math"
7+
"strconv"
78
"time"
89

910
"gopkg.in/src-d/go-errors.v1"
@@ -232,14 +233,37 @@ func EvaluateCondition(ctx *Context, cond Expression, row Row) (bool, error) {
232233
switch b := v.(type) {
233234
case bool:
234235
return b, nil
235-
case int, int64, int32, int16, int8, uint, uint64, uint32, uint16, uint8:
236-
return b != 0, nil
236+
case int:
237+
return b != int(0), nil
238+
case int64:
239+
return b != int64(0), nil
240+
case int32:
241+
return b != int32(0), nil
242+
case int16:
243+
return b != int16(0), nil
244+
case int8:
245+
return b != int8(0), nil
246+
case uint:
247+
return b != uint(0), nil
248+
case uint64:
249+
return b != uint64(0), nil
250+
case uint32:
251+
return b != uint32(0), nil
252+
case uint16:
253+
return b != uint16(0), nil
254+
case uint8:
255+
return b != uint8(0), nil
237256
case time.Duration:
238257
return int64(b) != 0, nil
239258
case time.Time:
240259
return b.UnixNano() != 0, nil
241-
case float32, float64:
260+
case float64:
242261
return int(math.Round(v.(float64))) != 0, nil
262+
case float32:
263+
return int(math.Round(float64(v.(float32)))) != 0, nil
264+
case string:
265+
parsed, err := strconv.ParseFloat(v.(string), 64)
266+
return err == nil && int(parsed) != 0, nil
243267
default:
244268
return false, nil
245269
}

sql/core_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sql_test
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"time"
7+
8+
"github.com/src-d/go-mysql-server/sql"
9+
"github.com/src-d/go-mysql-server/sql/expression"
10+
"github.com/stretchr/testify/require"
11+
)
12+
13+
var conditions = []struct {
14+
evaluated bool
15+
value interface{}
16+
t sql.Type
17+
}{
18+
{true, int16(1), sql.Int16},
19+
{false, int16(0), sql.Int16},
20+
{true, int32(1), sql.Int32},
21+
{false, int32(0), sql.Int32},
22+
{true, int(1), sql.Int64},
23+
{false, int(0), sql.Int64},
24+
{true, float32(1), sql.Float32},
25+
{true, float64(1), sql.Float64},
26+
{false, float32(0), sql.Float32},
27+
{false, float64(0), sql.Float64},
28+
{true, float32(0.5), sql.Float32},
29+
{true, float64(0.5), sql.Float64},
30+
{true, "1", sql.Text},
31+
{false, "0", sql.Text},
32+
{false, "foo", sql.Text},
33+
{false, "0.5", sql.Text},
34+
{false, time.Duration(0), sql.Timestamp},
35+
{true, time.Duration(1), sql.Timestamp},
36+
{false, false, sql.Boolean},
37+
{true, true, sql.Boolean},
38+
}
39+
40+
func TestEvaluateCondition(t *testing.T) {
41+
for _, v := range conditions {
42+
t.Run(fmt.Sprint(v.value, " evaluated to ", v.evaluated, " type ", v.t), func(t *testing.T) {
43+
require := require.New(t)
44+
b, err := sql.EvaluateCondition(sql.NewEmptyContext(), expression.NewLiteral(v.value, v.t), sql.NewRow())
45+
require.NoError(err)
46+
require.Equal(v.evaluated, b)
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)