-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhere_test.go
55 lines (47 loc) · 1.25 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
package sq
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestWherePartsAppendToSql(t *testing.T) {
parts := []Sqlizer{
newWherePart("x = ?", 1),
newWherePart(nil),
newWherePart(Eq{"y": 2}),
}
var sql strings.Builder
args, _ := appendToSql(parts, &sql, " AND ", []any{})
require.Equal(t, "x = ? AND y = ?", sql.String())
require.Equal(t, []any{1, 2}, args)
}
func TestWherePartsAppendToSqlErr(t *testing.T) {
parts := []Sqlizer{newWherePart(1)}
_, err := appendToSql(parts, &strings.Builder{}, "", []any{})
require.Error(t, err)
}
func TestWherePartNil(t *testing.T) {
sql, _, _ := newWherePart(nil).ToSql()
require.Equal(t, "", sql)
}
func TestWherePartErr(t *testing.T) {
_, _, err := newWherePart(1).ToSql()
require.Error(t, err)
}
func TestWherePartString(t *testing.T) {
sql, args, _ := newWherePart("x = ?", 1).ToSql()
require.Equal(t, "x = ?", sql)
require.Equal(t, []any{1}, args)
}
func TestWherePartMap(t *testing.T) {
test := func(pred any) {
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]any{"x": 1, "y": 2}
test(m)
test(Eq(m))
}