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

Function TestSQLAnalyzer_MixedStatements

cmd/gosqlx/cmd/sql_analyzer_test.go:669–722  ·  view source on GitHub ↗

TestSQLAnalyzer_MixedStatements tests analysis with multiple statement types

(t *testing.T)

Source from the content-addressed store, hash-verified

667
668// TestSQLAnalyzer_MixedStatements tests analysis with multiple statement types
669func TestSQLAnalyzer_MixedStatements(t *testing.T) {
670 tests := []struct {
671 name string
672 sql string
673 expectedStmtCount int
674 expectedMinScore int
675 }{
676 {
677 name: "SELECT and INSERT",
678 sql: "SELECT * FROM users; INSERT INTO logs (message) VALUES ('test')",
679 expectedStmtCount: 2,
680 expectedMinScore: 60,
681 },
682 }
683
684 for _, tt := range tests {
685 t.Run(tt.name, func(t *testing.T) {
686 tkz := tokenizer.GetTokenizer()
687 defer tokenizer.PutTokenizer(tkz)
688
689 tokens, err := tkz.Tokenize([]byte(tt.sql))
690 if err != nil {
691 t.Fatalf("Tokenization failed: %v", err)
692 }
693
694 p := parser.NewParser()
695 astObj := ast.NewAST()
696 defer ast.ReleaseAST(astObj)
697
698 result, err := p.ParseFromModelTokens(tokens)
699 if err != nil {
700 t.Skipf("Parsing failed (parser doesn't support multiple statements yet): %v", err)
701 return
702 }
703 astObj.Statements = result.Statements
704
705 analyzer := NewSQLAnalyzer()
706 report, err := analyzer.Analyze(astObj)
707 if err != nil {
708 t.Fatalf("Analysis failed: %v", err)
709 }
710
711 if report.Query.StatementCount != tt.expectedStmtCount {
712 t.Errorf("Expected StatementCount=%d, got %d",
713 tt.expectedStmtCount, report.Query.StatementCount)
714 }
715
716 if report.SecurityScore < tt.expectedMinScore {
717 t.Errorf("Security score too low: expected >= %d, got %d",
718 tt.expectedMinScore, report.SecurityScore)
719 }
720 })
721 }
722}
723
724// TestSQLAnalyzer_SecurityScannerIntegration tests that the security scanner
725// integration is working correctly to detect SQL injection patterns

Callers

nothing calls this directly

Calls 12

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

Tested by

no test coverage detected