-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
where_test.go
90 lines (81 loc) · 2.13 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package pgq
import (
"reflect"
"testing"
"bytes"
)
func TestWherePartsAppendSQL(t *testing.T) {
t.Parallel()
parts := []SQLizer{
newWherePart("x = ?", 1),
newWherePart(nil),
newWherePart(Eq{"y": 2}),
}
sql := &bytes.Buffer{}
args, err := appendSQL(parts, sql, " AND ", []any{})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if want, got := "x = ? AND y = ?", sql.String(); want != got {
t.Errorf("expected %q, got %q instead", want, got)
}
expectedArgs := []any{1, 2}
if !reflect.DeepEqual(expectedArgs, args) {
t.Errorf("wanted %v, got %v instead", args, expectedArgs)
}
}
func TestWherePartsAppendSQLErr(t *testing.T) {
t.Parallel()
parts := []SQLizer{newWherePart(1)}
_, err := appendSQL(parts, &bytes.Buffer{}, "", []any{})
if want := "expected string-keyed map or string, not int"; err.Error() != want {
t.Errorf("expected error to be %q, got %q instead", want, err)
}
}
func TestWherePartNil(t *testing.T) {
t.Parallel()
sql, _, err := newWherePart(nil).SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if sql != "" {
t.Errorf("expected WHERE to be empty")
}
}
func TestWherePartErr(t *testing.T) {
t.Parallel()
_, _, err := newWherePart(1).SQL()
if want := "expected string-keyed map or string, not int"; err.Error() != want {
t.Errorf("expected error to be %q, got %q instead", want, err)
}
}
func TestWherePartString(t *testing.T) {
t.Parallel()
sql, args, err := newWherePart("x = ?", 1).SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if want := "x = ?"; sql != want {
t.Errorf("expected %q, got %q instead", want, sql)
}
expectedArgs := []any{1}
if !reflect.DeepEqual(expectedArgs, args) {
t.Errorf("wanted %v, got %v instead", args, expectedArgs)
}
}
func TestWherePartMap(t *testing.T) {
t.Parallel()
test := func(pred any) {
sql, _, err := newWherePart(pred).SQL()
if err != nil {
t.Errorf("unexpected error: %v", err)
}
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))
}