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

Function TestSQLAnalyzer_ScoreCalculation

cmd/gosqlx/cmd/sql_analyzer_test.go:341–411  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

339}
340
341func TestSQLAnalyzer_ScoreCalculation(t *testing.T) {
342 testCases := []struct {
343 name string
344 sql string
345 expectRange func(score int) bool
346 }{
347 {
348 name: "Perfect query",
349 sql: "SELECT name, email FROM users WHERE active = true",
350 expectRange: func(score int) bool {
351 return score >= 90 // Should be A grade
352 },
353 },
354 {
355 name: "Query with medium issues",
356 sql: "SELECT * FROM users",
357 expectRange: func(score int) bool {
358 return score >= 60 && score < 90 // B-C grade range
359 },
360 },
361 {
362 name: "Query with SELECT *",
363 sql: "SELECT * FROM users",
364 expectRange: func(score int) bool {
365 return score >= 50 && score < 90 // Should be B-C grade due to SELECT * issues
366 },
367 },
368 }
369
370 for _, tc := range testCases {
371 t.Run(tc.name, func(t *testing.T) {
372 // Parse and analyze
373 tkz := tokenizer.GetTokenizer()
374 defer tokenizer.PutTokenizer(tkz)
375
376 tokens, err := tkz.Tokenize([]byte(tc.sql))
377 if err != nil {
378 t.Fatalf("Tokenization failed: %v", err)
379 }
380
381 p := parser.NewParser()
382 astObj := ast.NewAST()
383 defer ast.ReleaseAST(astObj)
384
385 result, err := p.ParseFromModelTokens(tokens)
386 if err != nil {
387 t.Fatalf("Parsing failed: %v", err)
388 }
389
390 astObj.Statements = result.Statements
391
392 analyzer := NewSQLAnalyzer()
393 report, err := analyzer.Analyze(astObj)
394 if err != nil {
395 t.Fatalf("Analysis failed: %v", err)
396 }
397
398 // Verify score is in expected range

Callers

nothing calls this directly

Calls 13

ParseFromModelTokensMethod · 0.95
AnalyzeMethod · 0.95
GetTokenizerFunction · 0.92
PutTokenizerFunction · 0.92
NewParserFunction · 0.92
NewASTFunction · 0.92
ReleaseASTFunction · 0.92
NewSQLAnalyzerFunction · 0.85
CalculateGradeFunction · 0.85
RunMethod · 0.80
TokenizeMethod · 0.80
FatalfMethod · 0.65

Tested by

no test coverage detected