-
Notifications
You must be signed in to change notification settings - Fork 0
/
fragment_test.go
183 lines (180 loc) · 4.09 KB
/
fragment_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
package sqlf_test
import (
"reflect"
"testing"
"github.com/qjebbs/go-sqlf/v2"
"github.com/qjebbs/go-sqlf/v2/sqlb"
"github.com/qjebbs/go-sqlf/v2/syntax"
)
func TestBuildFragment(t *testing.T) {
t.Parallel()
var table, alias sqlb.Table = "table", "t"
testCases := []struct {
name string
style syntax.BindVarStyle
fragment *sqlf.Fragment
want string
wantArgs []any
wantErr bool
}{
{
name: "build nil fragment",
fragment: nil,
want: "",
wantArgs: nil,
},
{
name: "#join",
style: syntax.Question,
fragment: sqlf.Fa(
"?,#join('#arg',',')",
1, 2,
),
want: "?,?,?",
wantArgs: []any{1, 1, 2},
},
{
name: "#join range",
style: syntax.Dollar,
fragment: sqlf.Fa(
"$1,#join('#arg',',', 2)",
1, 2, 3, 4,
),
want: "$1,$2,$3,$4",
wantArgs: []any{1, 2, 3, 4},
},
{
name: "args merging",
style: syntax.Dollar,
fragment: sqlf.Fa(
"#join('#arg',',')",
1, 1, 2, 3,
),
want: "$1,$1,$2,$3",
wantArgs: []any{1, 2, 3},
},
{
name: "#join mixed function and call",
style: syntax.Dollar,
fragment: sqlf.F("#join('#f1#arg',',')").
WithFragments(sqlf.Fa("p")).
WithArgs(1, 2),
want: "p$1,p$2",
wantArgs: []any{1, 2},
},
{
name: "#f",
fragment: sqlf.Ff("WHERE 1=1 #f1").
WithFragments(sqlf.F("")),
want: "WHERE 1=1",
wantArgs: nil,
},
{
name: "#f and args",
style: syntax.Question,
fragment: sqlf.F("WHERE #f1=?").
WithFragments(alias.Column("id")).
WithArgs(nil),
want: "WHERE t.id=?",
wantArgs: []any{nil},
},
{
name: "build nil column",
style: syntax.Dollar,
fragment: sqlf.F("WHERE #f1=$1").
WithFragments((*sqlf.Fragment)(nil)).
WithArgs(nil),
want: "WHERE =$1",
wantArgs: []any{nil},
},
{
name: "build complex fragment",
style: syntax.Dollar,
fragment: sqlf.F("WITH t AS (#f1) SELECT #f2,#f3,$1 FROM #f4 AS #f5").
WithArgs("foo").
WithFragments(
sqlf.F("SELECT * FROM #f1 AS #f2 WHERE #f3 > $1").
WithArgs(1).
WithFragments(table, alias, alias.Column("id")),
alias.Column("id"),
sqlf.F("#f1.id=$1").WithFragments(alias).WithArgs(2),
table, alias,
),
want: "WITH t AS (SELECT * FROM table AS t WHERE t.id > $1) SELECT t.id,t.id=$2,$3 FROM table AS t",
wantArgs: []any{1, 2, "foo"},
},
{
name: "build complex fragment 2",
style: syntax.Dollar,
fragment: sqlf.F("SELECT #join('#f', ', ', 3) FROM #f1 AS #f2").
WithFragments(
table, alias,
alias.Column("id"),
sqlf.F("#f1.id=$1").WithFragments(alias).WithArgs(1),
alias.Column("name"),
),
want: "SELECT t.id, t.id=$1, t.name FROM table AS t",
wantArgs: []any{1},
},
{
name: "prefix and suffix",
fragment: sqlf.F("#f1").WithPrefix("SELECT").WithSuffix("FOR UPDATE").
WithFragments(sqlf.F("")),
want: "",
wantArgs: nil,
},
{
name: "prefix and suffix",
fragment: sqlf.F("#f1").WithPrefix("SELECT").WithSuffix("FOR UPDATE").
WithFragments(sqlf.F("foo")),
want: "SELECT foo FOR UPDATE",
wantArgs: nil,
},
{
name: "ref fragment twice",
style: syntax.Dollar,
fragment: sqlf.F("#f1, #f1").
WithFragments(
sqlf.F("#join('#arg', ', '), ?").WithArgs(1, 2),
),
want: "$1, $2, $1, $1, $2, $1",
wantArgs: []any{1, 2},
},
{
name: "arg and fragment",
style: syntax.Question,
fragment: sqlf.Fa("? #f1", 1).
WithFragments(
sqlf.Fa("$1", 2),
),
want: "? ?",
wantArgs: []any{1, 2},
},
{
name: "mixed bindvar style",
fragment: sqlf.Fa("?, $1", nil),
wantErr: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
// t.Parallel()
ctx := sqlf.NewContext(tc.style)
got, err := tc.fragment.BuildFragment(ctx)
if err != nil {
if tc.wantErr {
return
}
t.Fatal(err)
}
if got != tc.want {
t.Errorf("got %q, want %q", got, tc.want)
}
args := ctx.Args()
if !reflect.DeepEqual(args, tc.wantArgs) {
t.Errorf("got %v, want %v", args, tc.wantArgs)
}
})
}
}