AssertFormattedSQL asserts that the SQL formats to match the expected output. This validates both that the SQL is valid and that it formats correctly. Example: testing.AssertFormattedSQL(t, "select * from users", "SELECT * FROM users;")
(t TestingT, sql, expected string)
| 114 | // "select * from users", |
| 115 | // "SELECT * FROM users;") |
| 116 | func AssertFormattedSQL(t TestingT, sql, expected string) bool { |
| 117 | t.Helper() |
| 118 | |
| 119 | opts := gosqlx.DefaultFormatOptions() |
| 120 | formatted, err := gosqlx.Format(sql, opts) |
| 121 | if err != nil { |
| 122 | t.Errorf("Failed to format SQL:\n SQL: %s\n Error: %v", |
| 123 | truncateSQL(sql), err) |
| 124 | return false |
| 125 | } |
| 126 | |
| 127 | // Normalize whitespace for comparison |
| 128 | formattedNorm := strings.TrimSpace(formatted) |
| 129 | expectedNorm := strings.TrimSpace(expected) |
| 130 | |
| 131 | if formattedNorm != expectedNorm { |
| 132 | t.Errorf("Formatted SQL does not match expected:\n Input: %s\n Expected: %s\n Got: %s", |
| 133 | truncateSQL(sql), expectedNorm, formattedNorm) |
| 134 | return false |
| 135 | } |
| 136 | return true |
| 137 | } |
| 138 | |
| 139 | // AssertTables asserts that the SQL contains references to the expected tables. |
| 140 | // This extracts table names from the AST and compares them (order-independent). |