forked from Masterminds/squirrel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where_test.go
62 lines (52 loc) · 1.49 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
58
59
60
61
62
package squirrel
import (
"fmt"
"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) {
_, _, err := newWherePart(1).ToSql()
assert.Error(t, err)
}
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))
}
func TestWherePartNoArgs(t *testing.T) {
_, _, err := newWherePart(Eq{"test": []string{}}).ToSql()
assert.Equal(t, err, fmt.Errorf("equality condition must contain at least one paramater"))
}