-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathintegration_test.go
More file actions
515 lines (428 loc) · 13.9 KB
/
integration_test.go
File metadata and controls
515 lines (428 loc) · 13.9 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// Copyright 2026 GoSQLX Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/ajitpratap0/GoSQLX/pkg/sql/parser"
"github.com/ajitpratap0/GoSQLX/pkg/sql/tokenizer"
)
// SQLTestFile represents a test file with metadata
type SQLTestFile struct {
Path string
Dialect string
Name string
Complexity string // Simple, Medium, Complex
Content string
}
// TestResult stores the result of parsing a SQL file
type TestResult struct {
File string
Dialect string
Success bool
Error error
ParseTime time.Duration
}
// loadSQLTestFiles loads all SQL files from testdata directory
func loadSQLTestFiles(t *testing.T, rootPath string) []SQLTestFile {
var files []SQLTestFile
dialects := []string{"postgresql", "mysql", "mssql", "oracle", "real_world"}
for _, dialect := range dialects {
dialectPath := filepath.Join(rootPath, "testdata", dialect)
err := filepath.WalkDir(dialectPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(path, ".sql") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
t.Logf("Warning: Could not read file %s: %v", path, err)
return nil
}
// Extract complexity from comment
complexity := "Medium"
contentStr := string(content)
if strings.Contains(contentStr, "Complexity: Simple") {
complexity = "Simple"
} else if strings.Contains(contentStr, "Complexity: Complex") {
complexity = "Complex"
}
files = append(files, SQLTestFile{
Path: path,
Dialect: dialect,
Name: filepath.Base(path),
Complexity: complexity,
Content: contentStr,
})
return nil
})
if err != nil {
t.Logf("Warning: Could not walk directory %s: %v", dialectPath, err)
}
}
return files
}
// extractSQLStatement extracts the main SQL statement from a file (ignoring comments)
func extractSQLStatement(content string) string {
lines := strings.Split(content, "\n")
var sqlLines []string
for _, line := range lines {
trimmed := strings.TrimSpace(line)
// Skip empty lines and comment-only lines
if trimmed == "" || strings.HasPrefix(trimmed, "--") {
continue
}
sqlLines = append(sqlLines, line)
}
return strings.Join(sqlLines, "\n")
}
// TestIntegration_AllDialects tests all SQL files across all dialects
func TestIntegration_AllDialects(t *testing.T) {
// Get the project root (go up from pkg/sql to project root)
projectRoot, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
t.Fatalf("Failed to determine project root: %v", err)
}
files := loadSQLTestFiles(t, projectRoot)
if len(files) == 0 {
t.Skip("No SQL test files found - testdata directory may not exist")
}
t.Logf("Found %d SQL test files to process", len(files))
var results []TestResult
successCount := 0
failCount := 0
// Group tests by dialect for better organization
dialectGroups := make(map[string][]SQLTestFile)
for _, file := range files {
dialectGroups[file.Dialect] = append(dialectGroups[file.Dialect], file)
}
// Run tests for each dialect
for dialect, dialectFiles := range dialectGroups {
t.Run(dialect, func(t *testing.T) {
dialectSuccess := 0
dialectFail := 0
for _, file := range dialectFiles {
t.Run(file.Name, func(t *testing.T) {
result := testSQLFile(t, file)
results = append(results, result)
if result.Success {
dialectSuccess++
successCount++
} else {
dialectFail++
failCount++
t.Logf("Failed to parse %s: %v", file.Name, result.Error)
}
})
}
t.Logf("Dialect %s: %d passed, %d failed (%.1f%% success rate)",
dialect, dialectSuccess, dialectFail,
100.0*float64(dialectSuccess)/float64(dialectSuccess+dialectFail))
})
}
// Summary statistics
t.Logf("\n=== Integration Test Summary ===")
t.Logf("Total files tested: %d", len(files))
t.Logf("Successful parses: %d", successCount)
t.Logf("Failed parses: %d", failCount)
t.Logf("Success rate: %.2f%%", 100.0*float64(successCount)/float64(len(files)))
// Breakdown by dialect
t.Logf("\n=== Results by Dialect ===")
for dialect, dialectFiles := range dialectGroups {
dialectSuccess := 0
for _, file := range dialectFiles {
for _, result := range results {
if result.File == file.Name && result.Success {
dialectSuccess++
break
}
}
}
t.Logf("%s: %d/%d files (%.1f%%)",
dialect, dialectSuccess, len(dialectFiles),
100.0*float64(dialectSuccess)/float64(len(dialectFiles)))
}
// Breakdown by complexity
t.Logf("\n=== Results by Complexity ===")
complexityGroups := make(map[string]int)
complexitySuccess := make(map[string]int)
for _, file := range files {
complexityGroups[file.Complexity]++
for _, result := range results {
if result.File == file.Name && result.Success {
complexitySuccess[file.Complexity]++
break
}
}
}
for complexity, total := range complexityGroups {
success := complexitySuccess[complexity]
t.Logf("%s: %d/%d files (%.1f%%)",
complexity, success, total,
100.0*float64(success)/float64(total))
}
}
// testSQLFile tests parsing a single SQL file
func testSQLFile(t *testing.T, file SQLTestFile) TestResult {
result := TestResult{
File: file.Name,
Dialect: file.Dialect,
Success: false,
}
// Extract SQL statement (remove comments)
sqlStatement := extractSQLStatement(file.Content)
if strings.TrimSpace(sqlStatement) == "" {
result.Error = fmt.Errorf("empty SQL statement after removing comments")
return result
}
// Get tokenizer from pool
tkz := tokenizer.GetTokenizer()
defer tokenizer.PutTokenizer(tkz)
// Tokenize
startTime := time.Now()
tokens, err := tkz.Tokenize([]byte(sqlStatement))
if err != nil {
result.Error = fmt.Errorf("tokenization failed: %w", err)
return result
}
// Parse directly from model tokens
p := parser.NewParser()
defer p.Release()
ast, err := p.ParseFromModelTokens(tokens)
result.ParseTime = time.Since(startTime)
if err != nil {
result.Error = fmt.Errorf("parsing failed: %w", err)
return result
}
if ast == nil {
result.Error = fmt.Errorf("parser returned nil AST without error")
return result
}
if len(ast.Statements) == 0 {
result.Error = fmt.Errorf("parser returned empty statement list")
return result
}
result.Success = true
return result
}
// TestIntegration_PostgreSQL_Simple tests simple PostgreSQL queries
func TestIntegration_PostgreSQL_Simple(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
simplePostgres := filterFiles(files, "postgresql", "Simple")
if len(simplePostgres) == 0 {
t.Skip("No simple PostgreSQL test files found")
}
runTestBatch(t, simplePostgres, "PostgreSQL Simple Queries")
}
// TestIntegration_MySQL_Medium tests medium complexity MySQL queries
func TestIntegration_MySQL_Medium(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
mediumMySQL := filterFiles(files, "mysql", "Medium")
if len(mediumMySQL) == 0 {
t.Skip("No medium MySQL test files found")
}
runTestBatch(t, mediumMySQL, "MySQL Medium Queries")
}
// TestIntegration_RealWorld_Complex tests complex real-world queries
func TestIntegration_RealWorld_Complex(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
complexRealWorld := filterFiles(files, "real_world", "Complex")
if len(complexRealWorld) == 0 {
t.Skip("No complex real-world test files found")
}
runTestBatch(t, complexRealWorld, "Real-World Complex Queries")
}
// TestIntegration_WindowFunctions tests window function support across dialects
func TestIntegration_WindowFunctions(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
windowFiles := []SQLTestFile{}
for _, file := range files {
if strings.Contains(strings.ToLower(file.Content), "over") &&
(strings.Contains(strings.ToLower(file.Content), "partition") ||
strings.Contains(strings.ToLower(file.Content), "row_number") ||
strings.Contains(strings.ToLower(file.Content), "rank")) {
windowFiles = append(windowFiles, file)
}
}
if len(windowFiles) == 0 {
t.Skip("No window function test files found")
}
t.Logf("Found %d files with window functions", len(windowFiles))
runTestBatch(t, windowFiles, "Window Functions")
}
// TestIntegration_CTEs tests Common Table Expression support
func TestIntegration_CTEs(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
cteFiles := []SQLTestFile{}
for _, file := range files {
if strings.Contains(strings.ToUpper(file.Content), "WITH") &&
(strings.Contains(strings.ToUpper(file.Content), "AS (") ||
strings.Contains(strings.ToUpper(file.Content), "RECURSIVE")) {
cteFiles = append(cteFiles, file)
}
}
if len(cteFiles) == 0 {
t.Skip("No CTE test files found")
}
t.Logf("Found %d files with CTEs", len(cteFiles))
runTestBatch(t, cteFiles, "Common Table Expressions")
}
// TestIntegration_JOINs tests JOIN support across dialects
func TestIntegration_JOINs(t *testing.T) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(t, projectRoot)
joinFiles := []SQLTestFile{}
for _, file := range files {
content := strings.ToUpper(file.Content)
if strings.Contains(content, "JOIN") &&
(strings.Contains(content, "INNER JOIN") ||
strings.Contains(content, "LEFT JOIN") ||
strings.Contains(content, "RIGHT JOIN") ||
strings.Contains(content, "FULL JOIN") ||
strings.Contains(content, "CROSS JOIN")) {
joinFiles = append(joinFiles, file)
}
}
if len(joinFiles) == 0 {
t.Skip("No JOIN test files found")
}
t.Logf("Found %d files with JOINs", len(joinFiles))
runTestBatch(t, joinFiles, "JOIN Operations")
}
// filterFiles filters files by dialect and complexity
func filterFiles(files []SQLTestFile, dialect string, complexity string) []SQLTestFile {
var filtered []SQLTestFile
for _, file := range files {
if file.Dialect == dialect && file.Complexity == complexity {
filtered = append(filtered, file)
}
}
return filtered
}
// runTestBatch runs a batch of tests and reports statistics
func runTestBatch(t *testing.T, files []SQLTestFile, batchName string) {
if len(files) == 0 {
t.Skip("No files to test")
}
successCount := 0
var totalTime time.Duration
for _, file := range files {
result := testSQLFile(t, file)
if result.Success {
successCount++
} else {
t.Logf("Failed: %s - %v", file.Name, result.Error)
}
totalTime += result.ParseTime
}
avgTime := totalTime / time.Duration(len(files))
successRate := 100.0 * float64(successCount) / float64(len(files))
t.Logf("\n=== %s Summary ===", batchName)
t.Logf("Total files: %d", len(files))
t.Logf("Successful: %d", successCount)
t.Logf("Failed: %d", len(files)-successCount)
t.Logf("Success rate: %.2f%%", successRate)
t.Logf("Average parse time: %v", avgTime)
t.Logf("Total parse time: %v", totalTime)
}
// BenchmarkIntegration_SimpleQueries benchmarks simple queries across all dialects
func BenchmarkIntegration_SimpleQueries(b *testing.B) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(&testing.T{}, projectRoot)
simpleFiles := []SQLTestFile{}
for _, file := range files {
if file.Complexity == "Simple" {
simpleFiles = append(simpleFiles, file)
}
}
if len(simpleFiles) == 0 {
b.Skip("No simple test files found")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, file := range simpleFiles {
sqlStatement := extractSQLStatement(file.Content)
tkz := tokenizer.GetTokenizer()
tokens, _ := tkz.Tokenize([]byte(sqlStatement))
tokenizer.PutTokenizer(tkz)
p := parser.NewParser()
_, _ = p.ParseFromModelTokens(tokens)
p.Release()
}
}
}
// BenchmarkIntegration_ComplexQueries benchmarks complex queries
func BenchmarkIntegration_ComplexQueries(b *testing.B) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(&testing.T{}, projectRoot)
complexFiles := []SQLTestFile{}
for _, file := range files {
if file.Complexity == "Complex" {
complexFiles = append(complexFiles, file)
}
}
if len(complexFiles) == 0 {
b.Skip("No complex test files found")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, file := range complexFiles {
sqlStatement := extractSQLStatement(file.Content)
tkz := tokenizer.GetTokenizer()
tokens, _ := tkz.Tokenize([]byte(sqlStatement))
tokenizer.PutTokenizer(tkz)
p := parser.NewParser()
_, _ = p.ParseFromModelTokens(tokens)
p.Release()
}
}
}
// BenchmarkIntegration_RealWorldScenarios benchmarks real-world query scenarios
func BenchmarkIntegration_RealWorldScenarios(b *testing.B) {
projectRoot, _ := filepath.Abs(filepath.Join("..", ".."))
files := loadSQLTestFiles(&testing.T{}, projectRoot)
realWorldFiles := []SQLTestFile{}
for _, file := range files {
if file.Dialect == "real_world" {
realWorldFiles = append(realWorldFiles, file)
}
}
if len(realWorldFiles) == 0 {
b.Skip("No real-world test files found")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, file := range realWorldFiles {
sqlStatement := extractSQLStatement(file.Content)
tkz := tokenizer.GetTokenizer()
tokens, _ := tkz.Tokenize([]byte(sqlStatement))
tokenizer.PutTokenizer(tkz)
p := parser.NewParser()
_, _ = p.ParseFromModelTokens(tokens)
p.Release()
}
}
}