-
Notifications
You must be signed in to change notification settings - Fork 8
/
cte_test.go
238 lines (224 loc) · 6.71 KB
/
cte_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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package sq
import (
"bytes"
"context"
"database/sql"
"errors"
"testing"
"github.com/bokwoon95/sq/internal/testutil"
)
func TestCTE(t *testing.T) {
t.Run("basic", func(t *testing.T) {
cte := NewCTE("cte", []string{"n"}, Queryf("SELECT 1")).Materialized().NotMaterialized().As("c")
TestTable{item: cte, wantQuery: "cte"}.assert(t)
field := NewAnyField("ff", TableStruct{name: "cte", alias: "c"})
if diff := testutil.Diff(cte.Field("ff"), field); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(cte.materialized, sql.NullBool{Valid: true, Bool: false}); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(cte.GetAlias(), "c"); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
}
func TestCTEs(t *testing.T) {
type TT struct {
description string
dialect string
ctes []CTE
wantQuery string
wantArgs []any
wantParams map[string][]int
}
tests := []TT{{
description: "basic",
ctes: []CTE{NewCTE("cte", nil, Queryf("SELECT 1"))},
wantQuery: "WITH cte AS (SELECT 1) ",
}, {
description: "recursive",
ctes: []CTE{
NewCTE("cte", nil, Queryf("SELECT 1")),
NewRecursiveCTE("nums", []string{"n"}, Union(
Queryf("SELECT 1"),
Queryf("SELECT n+1 FROM nums WHERE n < 10"),
)),
},
wantQuery: "WITH RECURSIVE cte AS (SELECT 1)" +
", nums (n) AS (SELECT 1 UNION SELECT n+1 FROM nums WHERE n < 10) ",
}, {
description: "mysql materialized",
dialect: DialectMySQL,
ctes: []CTE{NewCTE("cte", nil, Queryf("SELECT 1")).Materialized()},
wantQuery: "WITH cte AS (SELECT 1) ",
}, {
description: "postgres materialized",
dialect: DialectPostgres,
ctes: []CTE{NewCTE("cte", nil, Queryf("SELECT 1")).Materialized()},
wantQuery: "WITH cte AS MATERIALIZED (SELECT 1) ",
}, {
description: "postgres not materialized",
dialect: DialectPostgres,
ctes: []CTE{NewCTE("cte", nil, Queryf("SELECT 1")).NotMaterialized()},
wantQuery: "WITH cte AS NOT MATERIALIZED (SELECT 1) ",
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
buf, args, params := bufpool.Get().(*bytes.Buffer), &[]any{}, make(map[string][]int)
buf.Reset()
defer bufpool.Put(buf)
err := writeCTEs(context.Background(), tt.dialect, buf, args, params, tt.ctes)
if err != nil {
t.Fatal(testutil.Callers(), err)
}
if diff := testutil.Diff(buf.String(), tt.wantQuery); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(*args, tt.wantArgs); diff != "" {
t.Error(testutil.Callers(), diff)
}
if diff := testutil.Diff(params, tt.wantParams); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
}
t.Run("invalid cte", func(t *testing.T) {
t.Parallel()
buf, args, params := bufpool.Get().(*bytes.Buffer), &[]any{}, make(map[string][]int)
buf.Reset()
defer bufpool.Put(buf)
// no name
err := writeCTEs(context.Background(), "", buf, args, params, []CTE{
NewCTE("", nil, Queryf("SELECT 1")),
})
if err == nil {
t.Fatal(testutil.Callers(), "expected error but got nil")
}
// no query
err = writeCTEs(context.Background(), "", buf, args, params, []CTE{
NewCTE("cte", nil, nil),
})
if err == nil {
t.Fatal(testutil.Callers(), "expected error but got nil")
}
})
t.Run("err", func(t *testing.T) {
t.Parallel()
buf, args, params := bufpool.Get().(*bytes.Buffer), &[]any{}, make(map[string][]int)
buf.Reset()
defer bufpool.Put(buf)
// VariadicQuery
err := writeCTEs(context.Background(), "", buf, args, params, []CTE{
NewCTE("cte", nil, Union(
Queryf("SELECT 1"),
Queryf("SELECT {}", FaultySQL{}),
)),
})
if !errors.Is(err, ErrFaultySQL) {
t.Errorf(testutil.Callers()+"expected error %q but got %q", ErrFaultySQL, err)
}
// Query
err = writeCTEs(context.Background(), "", buf, args, params, []CTE{
NewCTE("cte", nil, Queryf("SELECT {}", FaultySQL{})),
})
if !errors.Is(err, ErrFaultySQL) {
t.Errorf(testutil.Callers()+"expected error %q but got %q", ErrFaultySQL, err)
}
})
}
func TestVariadicQuery(t *testing.T) {
q1, q2, q3 := Queryf("SELECT 1"), Queryf("SELECT 2"), Queryf("SELECT 3")
tests := []TestTable{{
description: "Union",
item: Union(q1, q2, q3),
wantQuery: "(SELECT 1 UNION SELECT 2 UNION SELECT 3)",
}, {
description: "UnionAll",
item: UnionAll(q1, q2, q3),
wantQuery: "(SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3)",
}, {
description: "Intersect",
item: Intersect(q1, q2, q3),
wantQuery: "(SELECT 1 INTERSECT SELECT 2 INTERSECT SELECT 3)",
}, {
description: "IntersectAll",
item: IntersectAll(q1, q2, q3),
wantQuery: "(SELECT 1 INTERSECT ALL SELECT 2 INTERSECT ALL SELECT 3)",
}, {
description: "Except",
item: Except(q1, q2, q3),
wantQuery: "(SELECT 1 EXCEPT SELECT 2 EXCEPT SELECT 3)",
}, {
description: "ExceptAll",
item: ExceptAll(q1, q2, q3),
wantQuery: "(SELECT 1 EXCEPT ALL SELECT 2 EXCEPT ALL SELECT 3)",
}, {
description: "No operator specified",
item: VariadicQuery{Queries: []Query{q1, q2, q3}},
wantQuery: "(SELECT 1 UNION SELECT 2 UNION SELECT 3)",
}, {
description: "nested VariadicQuery",
item: Union(Union(Union(q1, q2, q3))),
wantQuery: "(SELECT 1 UNION SELECT 2 UNION SELECT 3)",
}, {
description: "1 query",
item: Union(q1),
wantQuery: "SELECT 1",
}}
for _, tt := range tests {
tt := tt
t.Run(tt.description, func(t *testing.T) {
t.Parallel()
tt.assert(t)
})
}
t.Run("invalid VariadicQuery", func(t *testing.T) {
t.Parallel()
// empty
TestTable{item: Union()}.assertNotOK(t)
// nil query
TestTable{item: Union(nil)}.assertNotOK(t)
// nil query
TestTable{item: Union(q1, q2, nil)}.assertNotOK(t)
})
t.Run("err", func(t *testing.T) {
t.Parallel()
// VariadicQuery
TestTable{
item: Union(
Union(
Queryf("SELECT 1"),
Queryf("SELECT {}", FaultySQL{}),
),
),
}.assertErr(t, ErrFaultySQL)
// Query
TestTable{
item: Union(Queryf("SELECT {}", FaultySQL{})),
}.assertErr(t, ErrFaultySQL)
})
t.Run("SetFetchableFields", func(t *testing.T) {
t.Parallel()
_, ok := Union().SetFetchableFields([]Field{Expr("f1")})
if ok {
t.Error(testutil.Callers(), "expected not ok but got ok")
}
})
t.Run("GetDialect", func(t *testing.T) {
// empty VariadicQuery
if diff := testutil.Diff(Union().GetDialect(), ""); diff != "" {
t.Error(testutil.Callers(), diff)
}
// nil query
if diff := testutil.Diff(Union(nil).GetDialect(), ""); diff != "" {
t.Error(testutil.Callers(), diff)
}
// empty dialect propagated
if diff := testutil.Diff(Union(Queryf("SELECT 1")).GetDialect(), ""); diff != "" {
t.Error(testutil.Callers(), diff)
}
})
}