AssertColumns asserts that the SQL selects the expected columns. This extracts column names from SELECT statements and compares them (order-independent). Example: testing.AssertColumns(t, "SELECT id, name, email FROM users", []string{"id", "name", "email"})
(t TestingT, sql string, expectedColumns []string)
| 179 | // "SELECT id, name, email FROM users", |
| 180 | // []string{"id", "name", "email"}) |
| 181 | func AssertColumns(t TestingT, sql string, expectedColumns []string) bool { |
| 182 | t.Helper() |
| 183 | |
| 184 | astNode, err := gosqlx.Parse(sql) |
| 185 | if err != nil { |
| 186 | t.Errorf("Failed to parse SQL for column extraction:\n SQL: %s\n Error: %v", |
| 187 | truncateSQL(sql), err) |
| 188 | return false |
| 189 | } |
| 190 | |
| 191 | columns := extractColumns(astNode) |
| 192 | |
| 193 | // Sort both slices for comparison |
| 194 | sort.Strings(columns) |
| 195 | expectedSorted := make([]string, len(expectedColumns)) |
| 196 | copy(expectedSorted, expectedColumns) |
| 197 | sort.Strings(expectedSorted) |
| 198 | |
| 199 | if !stringSlicesEqual(columns, expectedSorted) { |
| 200 | t.Errorf("SQL column references do not match expected:\n SQL: %s\n Expected: %v\n Got: %v", |
| 201 | truncateSQL(sql), expectedColumns, columns) |
| 202 | return false |
| 203 | } |
| 204 | return true |
| 205 | } |
| 206 | |
| 207 | // AssertParsesTo asserts that SQL parses to a specific AST statement type. |
| 208 | // This is useful for verifying that SQL is interpreted as the expected statement type. |