(t *testing.T)
| 166 | } |
| 167 | |
| 168 | func TestSQLAnalyzer_IssueDetails(t *testing.T) { |
| 169 | testCases := []struct { |
| 170 | name string |
| 171 | sql string |
| 172 | expectedIssueID string |
| 173 | checkFields func(t *testing.T, issue AnalysisIssue) |
| 174 | }{ |
| 175 | { |
| 176 | name: "SELECT_STAR issue details", |
| 177 | sql: "SELECT * FROM users", |
| 178 | expectedIssueID: "SELECT_STAR", |
| 179 | checkFields: func(t *testing.T, issue AnalysisIssue) { |
| 180 | if issue.Category != IssueCategoryPerformance { |
| 181 | t.Errorf("Expected performance category, got %s", issue.Category) |
| 182 | } |
| 183 | if issue.Severity != IssueSeverityMedium { |
| 184 | t.Errorf("Expected medium severity, got %s", issue.Severity) |
| 185 | } |
| 186 | if !strings.Contains(issue.Description, "SELECT *") { |
| 187 | t.Error("Description should mention SELECT *") |
| 188 | } |
| 189 | if issue.Suggestion == "" { |
| 190 | t.Error("Suggestion should not be empty") |
| 191 | } |
| 192 | }, |
| 193 | }, |
| 194 | { |
| 195 | name: "FUNCTION_IN_WHERE issue details", |
| 196 | sql: "SELECT name FROM users WHERE UPPER(name) = 'TEST'", |
| 197 | expectedIssueID: "FUNCTION_IN_WHERE", |
| 198 | checkFields: func(t *testing.T, issue AnalysisIssue) { |
| 199 | if issue.Category != IssueCategoryPerformance { |
| 200 | t.Errorf("Expected performance category, got %s", issue.Category) |
| 201 | } |
| 202 | if issue.Severity != IssueSeverityMedium { |
| 203 | t.Errorf("Expected medium severity, got %s", issue.Severity) |
| 204 | } |
| 205 | if !strings.Contains(strings.ToLower(issue.Description), "function") { |
| 206 | t.Errorf("Description should mention function, got: %s", issue.Description) |
| 207 | } |
| 208 | }, |
| 209 | }, |
| 210 | } |
| 211 | |
| 212 | for _, tc := range testCases { |
| 213 | t.Run(tc.name, func(t *testing.T) { |
| 214 | // Parse and analyze |
| 215 | tkz := tokenizer.GetTokenizer() |
| 216 | defer tokenizer.PutTokenizer(tkz) |
| 217 | |
| 218 | tokens, err := tkz.Tokenize([]byte(tc.sql)) |
| 219 | if err != nil { |
| 220 | t.Fatalf("Tokenization failed: %v", err) |
| 221 | } |
| 222 | |
| 223 | p := parser.NewParser() |
| 224 | astObj := ast.NewAST() |
| 225 | defer ast.ReleaseAST(astObj) |
nothing calls this directly
no test coverage detected