(t *testing.T)
| 29 | } |
| 30 | |
| 31 | func 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. |
nothing calls this directly
no test coverage detected