MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / AssertColumns

Function AssertColumns

pkg/gosqlx/testing/testing.go:181–205  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

179// "SELECT id, name, email FROM users",
180// []string{"id", "name", "email"})
181func 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.

Callers 4

Calls 7

ParseFunction · 0.92
truncateSQLFunction · 0.85
copyFunction · 0.85
extractColumnsFunction · 0.70
stringSlicesEqualFunction · 0.70
HelperMethod · 0.65
ErrorfMethod · 0.65

Tested by 4