-
Notifications
You must be signed in to change notification settings - Fork 41
/
expression_test.go
189 lines (161 loc) · 4.07 KB
/
expression_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package bob
import (
"bytes"
"context"
"database/sql"
"errors"
"io"
"strconv"
"testing"
)
var d dialect
type dialect struct{}
func (d dialect) WriteArg(w io.Writer, position int) {
w.Write([]byte("$"))
w.Write([]byte(strconv.Itoa(position)))
}
func (d dialect) WriteQuoted(w io.Writer, s string) {
w.Write([]byte(`"`))
w.Write([]byte(s))
w.Write([]byte(`"`))
}
var expression = ExpressionFunc(func(ctx context.Context, w io.Writer, d Dialect, start int) ([]any, error) {
w.Write([]byte("Hello "))
d.WriteArg(w, start)
w.Write([]byte(" "))
d.WriteArg(w, start+1)
return nil, nil
})
func compare(t *testing.T, sqlExpected, sqlGotten string, argsExpected, argsGotten []any) {
t.Helper()
if sqlExpected != sqlGotten {
t.Fatalf("Wrong sql string\nExpected: %s\nGot: %s", sqlExpected, sqlGotten)
}
if len(argsGotten) != len(argsExpected) {
t.Fatalf("wrong length of debug args.\nExpected: %d\nGot: %d\n\n%s", len(argsExpected), len(argsGotten), argsGotten)
}
for i := range argsExpected {
arg := argsExpected[i]
debugArg := argsGotten[i]
if arg != debugArg {
t.Fatalf("wrong debug arg %d.\nExpected: %#v\nGot: %#v", i, arg, debugArg)
}
}
}
func TestExpress(t *testing.T) {
w := bytes.NewBuffer(nil)
args, err := Express(context.Background(), w, d, 2, expression)
if err != nil {
t.Fatalf("err while expressing")
}
compare(t, "Hello $2 $3", w.String(), nil, args)
}
func TestExpress2(t *testing.T) {
tests := map[string]struct {
value any
expected string
}{
"string": {
value: "a string",
expected: "a string",
},
"[]byte": {
value: []byte("a byte slice"),
expected: "a byte slice",
},
"Expression": {
value: expression,
expected: "Hello $1 $2",
},
"number": {
value: 100,
expected: "100",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
w := bytes.NewBuffer(nil)
args, err := Express(context.Background(), w, d, 1, test.value)
if err != nil {
t.Fatalf("err while expressing")
}
compare(t, test.expected, w.String(), nil, args)
})
}
}
func TestExpressIf(t *testing.T) {
tests := map[string]struct {
cond bool
prefix string
suffix string
}{
"false": {
cond: false,
},
"false with prefix and suffix": {
cond: false,
prefix: "pr",
suffix: "sf",
},
"true": {
cond: true,
},
"true with prefix and suffix": {
cond: true,
prefix: "pr",
suffix: "sf",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
w := bytes.NewBuffer(nil)
args, err := ExpressIf(context.Background(), w, d, 1, expression, test.cond, test.prefix, test.suffix)
if err != nil {
t.Fatalf("err while expressing")
}
expected := test.prefix + "Hello $1 $2" + test.suffix
if !test.cond {
expected = ""
}
compare(t, expected, w.String(), nil, args)
})
}
}
func TestExpressEmptySlice(t *testing.T) {
w := bytes.NewBuffer(nil)
args, err := ExpressSlice(context.Background(), w, d, 2, []string{}, "prefix ", ", ", " suffix")
if err != nil {
t.Fatalf("err while expressing")
}
compare(t, "", w.String(), nil, args)
}
func TestExpressSlice(t *testing.T) {
w := bytes.NewBuffer(nil)
args, err := ExpressSlice(context.Background(), w, d, 2, []string{"one", "two", "three"}, "prefix ", ", ", " suffix")
if err != nil {
t.Fatalf("err while expressing")
}
compare(t, "prefix one, two, three suffix", w.String(), nil, args)
}
type dialectWithNamed struct{ dialect }
func (d dialectWithNamed) WriteNamedArg(w io.Writer, name string) {
w.Write([]byte(":"))
w.Write([]byte(name))
}
func TestNamedArgs(t *testing.T) {
arg := sql.Named("name", "value")
w := bytes.NewBuffer(nil)
args, err := Express(context.Background(), w, dialectWithNamed{}, 1, arg)
if err != nil {
t.Fatalf("err while expressing")
}
compare(t, ":name", w.String(), []any{arg}, args)
}
func TestErrNoNamedArgs(t *testing.T) {
arg := sql.Named("name", "value")
w := bytes.NewBuffer(nil)
_, err := Express(context.Background(), w, d, 1, arg)
if !errors.Is(err, ErrNoNamedArgs) {
t.Fatalf("Expected to get ErrNoNamedArgs but got %v", err)
}
}