-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_test.go
More file actions
382 lines (349 loc) · 8.33 KB
/
formatter_test.go
File metadata and controls
382 lines (349 loc) · 8.33 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package sqlformatter
import (
"strings"
"testing"
)
func TestNewFormatter(t *testing.T) {
formatter := NewFormatter()
if formatter.IndentSize != 2 {
t.Errorf("Expected IndentSize to be 2, got %d", formatter.IndentSize)
}
if !formatter.KeywordUpper {
t.Errorf("Expected KeywordUpper to be true")
}
}
func TestSelectFormatting(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
expected string
}{
{
name: "Simple SELECT",
input: "SELECT * FROM users WHERE id = 1",
expected: `SELECT
*
FROM
users
WHERE
id = 1`,
},
{
name: "Multi-column SELECT",
input: "SELECT id, name, email FROM users",
expected: `SELECT
id,
name,
email
FROM
users`,
},
{
name: "Complex SELECT with JOIN",
input: "select u.id, p.product_name, u.name from users u join products p on u.id = p.user_id where u.age > 25 and p.category = 'electronics' group by u.id order by p.price desc limit 10",
expected: `SELECT
u.id,
p.product_name,
u.name
FROM
users u
JOIN products p on u.id = p.user_id
WHERE
u.age > 25 and p.category = 'electronics'
GROUP BY
u.id
ORDER BY
p.price desc
LIMIT
10`,
},
{
name: "SELECT with aggregate functions",
input: "SELECT COUNT(*) as total, category FROM products GROUP BY category HAVING COUNT(*) > 5",
expected: `SELECT
COUNT(*) as total,
category
FROM
products
GROUP BY
category
HAVING
COUNT(*) > 5`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := formatter.Format(tt.input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
if result != tt.expected {
t.Errorf("Formatting result mismatch\nExpected:\n%s\nActual:\n%s", tt.expected, result)
}
})
}
}
func TestInsertFormatting(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
expected string
}{
{
name: "Simple INSERT",
input: "INSERT INTO users (name, email) VALUES ('Alice', 'alice@example.com')",
expected: `INSERT INTO users
(name, email)
VALUES
('Alice', 'alice@example.com')`,
},
{
name: "Multi-column INSERT",
input: "INSERT INTO products (name, price, category, description) VALUES ('Laptop', 999.99, 'electronics', 'High-performance laptop')",
expected: `INSERT INTO products
(name, price, category, description)
VALUES
('Laptop', 999.99, 'electronics', 'High-performance laptop')`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := formatter.Format(tt.input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
if result != tt.expected {
t.Errorf("Formatting result mismatch\nExpected:\n%s\nActual:\n%s", tt.expected, result)
}
})
}
}
func TestUpdateFormatting(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
expected string
}{
{
name: "Simple UPDATE",
input: "UPDATE users SET name = 'John' WHERE id = 1",
expected: `UPDATE users
SET
name = 'John'
WHERE
id = 1`,
},
{
name: "Multi-field UPDATE",
input: "UPDATE users SET name = 'John', email = 'john@example.com' WHERE id = 1",
expected: `UPDATE users
SET
name = 'John',
email = 'john@example.com'
WHERE
id = 1`,
},
{
name: "UPDATE without WHERE",
input: "UPDATE users SET active = true",
expected: `UPDATE users
SET
active = true`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := formatter.Format(tt.input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
if result != tt.expected {
t.Errorf("Formatting result mismatch\nExpected:\n%s\nActual:\n%s", tt.expected, result)
}
})
}
}
func TestDeleteFormatting(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
expected string
}{
{
name: "DELETE with WHERE",
input: "DELETE FROM users WHERE age < 18",
expected: `DELETE FROM users
WHERE
age < 18`,
},
{
name: "DELETE with complex WHERE condition",
input: "DELETE FROM orders WHERE status = 'cancelled' AND created_at < '2023-01-01'",
expected: `DELETE FROM orders
WHERE
status = 'cancelled' AND created_at < '2023-01-01'`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := formatter.Format(tt.input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
if result != tt.expected {
t.Errorf("Formatting result mismatch\nExpected:\n%s\nActual:\n%s", tt.expected, result)
}
})
}
}
func TestFormatterOptions(t *testing.T) {
t.Run("Custom indent size", func(t *testing.T) {
formatter := NewFormatter()
formatter.IndentSize = 4
input := "SELECT id, name FROM users"
result, err := formatter.Format(input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
// Check if 4 spaces indentation is used
lines := strings.Split(result, "\n")
if len(lines) < 2 {
t.Fatal("Expected at least 2 lines of output")
}
if !strings.HasPrefix(lines[1], " ") { // 4 spaces
t.Errorf("Expected 4-space indentation, got indentation: '%s'", lines[1][:4])
}
})
t.Run("Lowercase keywords", func(t *testing.T) {
formatter := NewFormatter()
formatter.KeywordUpper = false
input := "SELECT * FROM users"
result, err := formatter.Format(input)
if err != nil {
t.Fatalf("Formatting failed: %v", err)
}
if !strings.Contains(result, "select") || !strings.Contains(result, "from") {
t.Errorf("Expected lowercase keywords, actual result: %s", result)
}
})
}
func TestErrorCases(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
}{
{
name: "Empty SQL",
input: "",
},
{
name: "Whitespace-only SQL",
input: " ",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := formatter.Format(tt.input)
if err == nil {
t.Errorf("Expected error to occur, but no error happened")
}
})
}
}
func TestSplitColumns(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
expected []string
}{
{
name: "Simple column split",
input: "id, name, email",
expected: []string{"id", " name", " email"},
},
{
name: "Column split with functions",
input: "id, COUNT(*), MAX(age)",
expected: []string{"id", " COUNT(*)", " MAX(age)"},
},
{
name: "Column split with nested parentheses",
input: "id, CASE WHEN (age > 18) THEN 'adult' ELSE 'minor' END, name",
expected: []string{"id", " CASE WHEN (age > 18) THEN 'adult' ELSE 'minor' END", " name"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := formatter.splitColumns(tt.input)
if len(result) != len(tt.expected) {
t.Errorf("Expected %d parts, got %d", len(tt.expected), len(result))
return
}
for i, expected := range tt.expected {
if result[i] != expected {
t.Errorf("Part %d mismatch, expected '%s', got '%s'", i, expected, result[i])
}
}
})
}
}
func TestKeywordFormatting(t *testing.T) {
formatter := NewFormatter()
tests := []struct {
name string
input string
upper bool
expected string
}{
{
name: "Uppercase keyword",
input: "select",
upper: true,
expected: "SELECT",
},
{
name: "Lowercase keyword",
input: "SELECT",
upper: false,
expected: "select",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
formatter.KeywordUpper = tt.upper
result := formatter.keyword(tt.input)
if result != tt.expected {
t.Errorf("Expected '%s', got '%s'", tt.expected, result)
}
})
}
}
// Benchmark tests
func BenchmarkSelectFormatting(b *testing.B) {
formatter := NewFormatter()
sql := "select u.id, p.product_name, u.name from users u join products p on u.id = p.user_id where u.age > 25 and p.category = 'electronics' group by u.id order by p.price desc limit 10"
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := formatter.Format(sql)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkInsertFormatting(b *testing.B) {
formatter := NewFormatter()
sql := "INSERT INTO products (name, price, category, description) VALUES ('Laptop', 999.99, 'electronics', 'High-performance laptop')"
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := formatter.Format(sql)
if err != nil {
b.Fatal(err)
}
}
}