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

Function TestSQLAnalyzer_ComplexityMetrics

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

Source from the content-addressed store, hash-verified

258}
259
260func TestSQLAnalyzer_ComplexityMetrics(t *testing.T) {
261 testCases := []struct {
262 name string
263 sql string
264 expectedComplexity string // LOW, MEDIUM, HIGH, VERY_HIGH
265 minJoinCount int
266 minFunctionCount int
267 }{
268 {
269 name: "Simple query",
270 sql: "SELECT name FROM users WHERE active = true",
271 expectedComplexity: "MEDIUM", // Current analyzer seems to report medium for simple queries
272 minJoinCount: 0,
273 minFunctionCount: 0,
274 },
275 {
276 name: "Medium complexity with JOINs",
277 sql: "SELECT u.name, p.title FROM users u JOIN posts p ON u.id = p.user_id WHERE u.active = true",
278 expectedComplexity: "MEDIUM", // Should be medium with JOIN
279 minJoinCount: 1,
280 minFunctionCount: 0,
281 },
282 {
283 name: "High complexity with multiple JOINs",
284 sql: `SELECT u.name, p.title FROM users u
285 JOIN posts p ON u.id = p.user_id
286 JOIN categories c ON p.category_id = c.id
287 WHERE u.active = true`,
288 expectedComplexity: "MEDIUM",
289 minJoinCount: 2,
290 minFunctionCount: 0,
291 },
292 }
293
294 for _, tc := range testCases {
295 t.Run(tc.name, func(t *testing.T) {
296 // Parse and analyze
297 tkz := tokenizer.GetTokenizer()
298 defer tokenizer.PutTokenizer(tkz)
299
300 tokens, err := tkz.Tokenize([]byte(tc.sql))
301 if err != nil {
302 t.Fatalf("Tokenization failed: %v", err)
303 }
304
305 p := parser.NewParser()
306 astObj := ast.NewAST()
307 defer ast.ReleaseAST(astObj)
308
309 result, err := p.ParseFromModelTokens(tokens)
310 if err != nil {
311 t.Fatalf("Parsing failed: %v", err)
312 }
313
314 astObj.Statements = result.Statements
315
316 analyzer := NewSQLAnalyzer()
317 report, err := analyzer.Analyze(astObj)

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