-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathfmt_test.go
More file actions
184 lines (166 loc) · 4.76 KB
/
fmt_test.go
File metadata and controls
184 lines (166 loc) · 4.76 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
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"testing"
"github.com/sqlc-dev/sqlc/internal/config"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/engine/dolphin"
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
"github.com/sqlc-dev/sqlc/internal/engine/sqlite"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
"github.com/sqlc-dev/sqlc/internal/sql/format"
)
// sqlParser is an interface for SQL parsers
type sqlParser interface {
Parse(r io.Reader) ([]ast.Statement, error)
}
// sqlFormatter is an interface for formatters
type sqlFormatter interface {
format.Dialect
}
func TestFormat(t *testing.T) {
t.Parallel()
for _, tc := range FindTests(t, "testdata", "base") {
t.Run(tc.Name, func(t *testing.T) {
// Parse the config file to determine the engine
configPath := filepath.Join(tc.Path, tc.ConfigName)
configFile, err := os.Open(configPath)
if err != nil {
t.Fatal(err)
}
conf, err := config.ParseConfig(configFile)
configFile.Close()
if err != nil {
t.Fatal(err)
}
// Skip if there are no SQL packages configured
if len(conf.SQL) == 0 {
return
}
engine := conf.SQL[0].Engine
// Select the appropriate parser and fingerprint function based on engine
var parse sqlParser
var formatter sqlFormatter
var fingerprint func(string) (string, error)
switch engine {
case config.EnginePostgreSQL:
pgParser := postgresql.NewParser()
parse = pgParser
formatter = pgParser
fingerprint = postgresql.Fingerprint
case config.EngineMySQL:
mysqlParser := dolphin.NewParser()
parse = mysqlParser
formatter = mysqlParser
// For MySQL, we use a "round-trip" fingerprint: parse the SQL, format it,
// and return the formatted string. This tests that our formatting produces
// valid SQL that parses to the same AST structure.
fingerprint = func(sql string) (string, error) {
stmts, err := mysqlParser.Parse(strings.NewReader(sql))
if err != nil {
return "", err
}
if len(stmts) == 0 {
return "", nil
}
return ast.Format(stmts[0].Raw, mysqlParser), nil
}
case config.EngineSQLite:
sqliteParser := sqlite.NewParser()
parse = sqliteParser
formatter = sqliteParser
// For SQLite, we use the same "round-trip" fingerprint strategy as MySQL:
// parse the SQL, format it, and return the formatted string.
fingerprint = func(sql string) (string, error) {
stmts, err := sqliteParser.Parse(strings.NewReader(sql))
if err != nil {
return "", err
}
if len(stmts) == 0 {
return "", nil
}
return strings.ToLower(ast.Format(stmts[0].Raw, sqliteParser)), nil
}
default:
// Skip unsupported engines
return
}
// Find query files from config
var queryFiles []string
for _, sql := range conf.SQL {
for _, q := range sql.Queries {
queryPath := filepath.Join(tc.Path, q)
info, err := os.Stat(queryPath)
if err != nil {
continue
}
if info.IsDir() {
// If it's a directory, glob for .sql files
matches, err := filepath.Glob(filepath.Join(queryPath, "*.sql"))
if err != nil {
continue
}
queryFiles = append(queryFiles, matches...)
} else {
queryFiles = append(queryFiles, queryPath)
}
}
}
if len(queryFiles) == 0 {
return
}
for _, queryFile := range queryFiles {
if _, err := os.Stat(queryFile); os.IsNotExist(err) {
continue
}
contents, err := os.ReadFile(queryFile)
if err != nil {
t.Fatal(err)
}
// Parse the entire file to get proper statement boundaries
stmts, err := parse.Parse(bytes.NewReader(contents))
if err != nil {
// Skip files with parse errors (e.g., syntax_errors test cases)
return
}
for i, stmt := range stmts {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
// Extract the original query text using statement location and length
start := stmt.Raw.StmtLocation
length := stmt.Raw.StmtLen
if length == 0 {
// If StmtLen is 0, it means the statement goes to the end of the input
length = len(contents) - start
}
query := strings.TrimSpace(string(contents[start : start+length]))
expected, err := fingerprint(query)
if err != nil {
t.Fatal(err)
}
if false {
r, err := postgresql.Parse(query)
debug.Dump(r, err)
}
out := ast.Format(stmt.Raw, formatter)
actual, err := fingerprint(out)
if err != nil {
t.Error(err)
}
if expected != actual {
debug.Dump(stmt.Raw)
t.Errorf("- %s", expected)
t.Errorf("- %s", query)
t.Errorf("+ %s", actual)
t.Errorf("+ %s", out)
}
})
}
}
})
}
}