MCPcopy
hub / github.com/sqlc-dev/sqlc / TestFormat

Function TestFormat

internal/endtoend/fmt_test.go:31–186  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

29}
30
31func TestFormat(t *testing.T) {
32 t.Parallel()
33 for _, tc := range FindTests(t, "testdata", "base") {
34 tc := tc
35 t.Run(tc.Name, func(t *testing.T) {
36 // Parse the config file to determine the engine
37 configPath := filepath.Join(tc.Path, tc.ConfigName)
38 configFile, err := os.Open(configPath)
39 if err != nil {
40 t.Fatal(err)
41 }
42 conf, err := config.ParseConfig(configFile)
43 configFile.Close()
44 if err != nil {
45 t.Fatal(err)
46 }
47
48 // Skip if there are no SQL packages configured
49 if len(conf.SQL) == 0 {
50 return
51 }
52
53 engine := conf.SQL[0].Engine
54
55 // Select the appropriate parser and fingerprint function based on engine
56 var parse sqlParser
57 var formatter sqlFormatter
58 var fingerprint func(string) (string, error)
59
60 switch engine {
61 case config.EnginePostgreSQL:
62 pgParser := postgresql.NewParser()
63 parse = pgParser
64 formatter = pgParser
65 fingerprint = postgresql.Fingerprint
66 case config.EngineMySQL:
67 mysqlParser := dolphin.NewParser()
68 parse = mysqlParser
69 formatter = mysqlParser
70 // For MySQL, we use a "round-trip" fingerprint: parse the SQL, format it,
71 // and return the formatted string. This tests that our formatting produces
72 // valid SQL that parses to the same AST structure.
73 fingerprint = func(sql string) (string, error) {
74 stmts, err := mysqlParser.Parse(strings.NewReader(sql))
75 if err != nil {
76 return "", err
77 }
78 if len(stmts) == 0 {
79 return "", nil
80 }
81 return ast.Format(stmts[0].Raw, mysqlParser), nil
82 }
83 case config.EngineSQLite:
84 sqliteParser := sqlite.NewParser()
85 parse = sqliteParser
86 formatter = sqliteParser
87 // For SQLite, we use the same "round-trip" fingerprint strategy as MySQL:
88 // parse the SQL, format it, and return the formatted string.

Callers

nothing calls this directly

Calls 13

ParseMethod · 0.95
ParseMethod · 0.95
ParseConfigFunction · 0.92
NewParserFunction · 0.92
NewParserFunction · 0.92
FormatFunction · 0.92
NewParserFunction · 0.92
DumpFunction · 0.92
FindTestsFunction · 0.85
OpenMethod · 0.80
CloseMethod · 0.65
JoinMethod · 0.45

Tested by

no test coverage detected