forked from fluge/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where_test.go
57 lines (48 loc) · 1.32 KB
/
where_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package squirrel
import (
"testing"
"bytes"
"github.com/stretchr/testify/assert"
)
func TestWherePartsAppendToSql(t *testing.T) {
parts := []Sqlizer{
newWherePart("x = ?", 1),
newWherePart(nil),
newWherePart(Eq{"y": 2}),
}
sql := &bytes.Buffer{}
args, _ := appendToSql(parts, sql, " AND ", []interface{}{})
assert.Equal(t, "x = ? AND y = ?", sql.String())
assert.Equal(t, []interface{}{1, 2}, args)
}
//func TestWherePartsAppendToSqlErr(t *testing.T) {
// parts := []Sqlizer{newWherePart(1)}
// _, err := appendToSql(parts, &bytes.Buffer{}, "", []interface{}{})
// assert.Error(t, err)
//}
func TestWherePartNil(t *testing.T) {
sql, _:= newWherePart(nil).ToSql()
assert.Equal(t, "", sql)
}
//func TestWherePartErr(t *testing.T) {
// sql, args := newWherePart(1).ToSql()
// //assert.Error(t, err)
// t.Log(sql,args)
//}
func TestWherePartString(t *testing.T) {
sql, args := newWherePart("x = ?", 1).ToSql()
assert.Equal(t, "x = ?", sql)
assert.Equal(t, []interface{}{1}, args)
}
func TestWherePartMap(t *testing.T) {
test := func(pred interface{}) {
sql, _ := newWherePart(pred).ToSql()
expect := []string{"x = ? AND y = ?", "y = ? AND x = ?"}
if sql != expect[0] && sql != expect[1] {
t.Errorf("expected one of %#v, got %#v", expect, sql)
}
}
m := map[string]interface{}{"x": 1, "y": 2}
test(m)
test(Eq(m))
}