-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathexpression_test.go
More file actions
218 lines (183 loc) · 4.81 KB
/
Copy pathexpression_test.go
File metadata and controls
218 lines (183 loc) · 4.81 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package bob
import (
"bytes"
"context"
"database/sql"
"errors"
"io"
"strconv"
"testing"
)
var d dialect
type dialect struct{}
func (d dialect) WriteArg(w io.StringWriter, position int) {
w.WriteString("$")
w.WriteString(strconv.Itoa(position))
}
func (d dialect) WriteQuoted(w io.StringWriter, s string) {
w.WriteString(`"`)
w.WriteString(s)
w.WriteString(`"`)
}
var expression = ExpressionFunc(func(ctx context.Context, w io.StringWriter, d Dialect, start int) ([]any, error) {
w.WriteString("Hello ")
d.WriteArg(w, start)
w.WriteString(" ")
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.StringWriter, name string) {
w.WriteString(":")
w.WriteString(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)
}
}
type wrappedExpr struct {
Expression
}
func (wrappedExpr) ShouldOmitParens() bool { return true }
func TestParensOmitter(t *testing.T) {
var _ ParensOmitter = wrappedExpr{}
wrapped := wrappedExpr{Expression: ExpressionFunc(func(ctx context.Context, w io.StringWriter, d Dialect, start int) ([]any, error) {
d.WriteQuoted(w, "s")
w.WriteString(".")
d.WriteQuoted(w, "id")
return nil, nil
})}
if !wrapped.ShouldOmitParens() {
t.Fatalf("expected wrapped expression to opt out of auto parenthesis wrapping")
}
w := bytes.NewBuffer(nil)
args, err := Express(context.Background(), w, d, 1, wrapped)
if err != nil {
t.Fatalf("err while expressing wrapped expression: %v", err)
}
compare(t, `"s"."id"`, w.String(), nil, args)
}