Skip to content

Commit fa8bf4f

Browse files
Ajit Pratap SinghAjit Pratap Singh
authored andcommitted
fix: replace coverage-padding with real assertions
1 parent 5a77d92 commit fa8bf4f

File tree

1 file changed

+120
-31
lines changed

1 file changed

+120
-31
lines changed

pkg/gosqlx/coverage_test.go

Lines changed: 120 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ func TestParseWithRecovery(t *testing.T) {
6565
if tt.wantStmts && len(stmts) == 0 {
6666
t.Error("expected statements but got none")
6767
}
68+
// Verify parsed statement content for known-valid inputs
69+
if tt.name == "valid single statement" {
70+
if len(stmts) != 1 {
71+
t.Fatalf("expected 1 statement, got %d", len(stmts))
72+
}
73+
if _, ok := stmts[0].(*ast.SelectStatement); !ok {
74+
t.Errorf("expected *ast.SelectStatement, got %T", stmts[0])
75+
}
76+
}
77+
if tt.name == "valid multiple statements" {
78+
if len(stmts) != 2 {
79+
t.Fatalf("expected 2 statements, got %d", len(stmts))
80+
}
81+
for i, stmt := range stmts {
82+
if _, ok := stmt.(*ast.SelectStatement); !ok {
83+
t.Errorf("statement[%d]: expected *ast.SelectStatement, got %T", i, stmt)
84+
}
85+
}
86+
}
87+
if tt.name == "mixed valid and invalid" && len(stmts) > 0 {
88+
if _, ok := stmts[0].(*ast.SelectStatement); !ok {
89+
t.Errorf("recovered statement: expected *ast.SelectStatement, got %T", stmts[0])
90+
}
91+
}
6892
})
6993
}
7094
}
@@ -187,25 +211,33 @@ func TestExtractEdgeCases(t *testing.T) {
187211
t.Run("columns from CAST", func(t *testing.T) {
188212
r := mustParseHelper(t, "SELECT CAST(age AS TEXT) FROM users")
189213
cols := ExtractColumns(r)
190-
_ = cols
214+
if len(cols) == 0 {
215+
t.Error("expected columns from CAST expression")
216+
}
191217
})
192218

193219
t.Run("columns from IN", func(t *testing.T) {
194220
r := mustParseHelper(t, "SELECT * FROM t WHERE status IN ('a', 'b', 'c')")
195221
cols := ExtractColumns(r)
196-
_ = cols
222+
if len(cols) == 0 {
223+
t.Error("expected columns from IN expression")
224+
}
197225
})
198226

199227
t.Run("columns from BETWEEN", func(t *testing.T) {
200228
r := mustParseHelper(t, "SELECT * FROM t WHERE age BETWEEN min_age AND max_age")
201229
cols := ExtractColumns(r)
202-
_ = cols
230+
if len(cols) == 0 {
231+
t.Error("expected columns from BETWEEN expression")
232+
}
203233
})
204234

205235
t.Run("columns from unary NOT", func(t *testing.T) {
206236
r := mustParseHelper(t, "SELECT * FROM t WHERE NOT active")
207237
cols := ExtractColumns(r)
208-
_ = cols
238+
if len(cols) == 0 {
239+
t.Error("expected columns from NOT expression")
240+
}
209241
})
210242

211243
t.Run("tables from UNION", func(t *testing.T) {
@@ -267,7 +299,9 @@ func TestExtractEdgeCases(t *testing.T) {
267299
t.Run("UPDATE", func(t *testing.T) {
268300
r := mustParseHelper(t, "UPDATE users SET name = 'test', email = '[email protected]' WHERE id = 1")
269301
cols := ExtractColumns(r)
270-
_ = cols
302+
if len(cols) == 0 {
303+
t.Error("expected columns from UPDATE")
304+
}
271305
})
272306

273307
t.Run("INSERT", func(t *testing.T) {
@@ -281,155 +315,210 @@ func TestExtractEdgeCases(t *testing.T) {
281315
t.Run("aliased expressions", func(t *testing.T) {
282316
r := mustParseHelper(t, "SELECT name AS user_name, age AS user_age FROM users")
283317
cols := ExtractColumns(r)
284-
_ = cols
318+
if len(cols) == 0 {
319+
t.Error("expected columns from aliased expressions")
320+
}
285321
})
286322

287323
t.Run("qualified columns complex", func(t *testing.T) {
288324
r := mustParseHelper(t, `SELECT u.name, CASE WHEN u.age > 18 THEN 'adult' ELSE 'minor' END,
289325
CAST(u.salary AS TEXT) FROM users u`)
290326
cols := ExtractColumnsQualified(r)
291-
_ = cols
327+
if len(cols) == 0 {
328+
t.Error("expected qualified columns from complex query")
329+
}
292330
})
293331

294332
t.Run("function with filter", func(t *testing.T) {
295333
r := mustParseHelper(t, "SELECT COUNT(*) FILTER (WHERE active) FROM users")
296334
cols := ExtractColumns(r)
297-
_ = cols
335+
if len(cols) == 0 {
336+
t.Error("expected columns from FILTER expression")
337+
}
298338
})
299339

300340
t.Run("multiple function calls", func(t *testing.T) {
301341
r := mustParseHelper(t, `SELECT UPPER(name), LOWER(email), LENGTH(name) FROM users`)
302342
fns := ExtractFunctions(r)
303-
_ = fns
343+
if len(fns) < 3 {
344+
t.Errorf("expected >= 3 functions, got %d", len(fns))
345+
}
304346
cols := ExtractColumns(r)
305-
_ = cols
347+
if len(cols) == 0 {
348+
t.Error("expected columns from function arguments")
349+
}
306350
})
307351

308352
// Cover INSERT/UPDATE/DELETE/CTE/SetOperation paths in all collectors
309353
t.Run("qualified cols from INSERT with SELECT", func(t *testing.T) {
310354
r := mustParseHelper(t, "INSERT INTO t2 (name) SELECT name FROM t1 WHERE id > 0")
311355
cols := ExtractColumnsQualified(r)
312-
_ = cols
356+
if len(cols) == 0 {
357+
t.Error("expected qualified columns from INSERT...SELECT")
358+
}
313359
})
314360

315361
t.Run("qualified cols from UPDATE", func(t *testing.T) {
316362
r := mustParseHelper(t, "UPDATE users SET name = 'test' WHERE id = 1")
317363
cols := ExtractColumnsQualified(r)
318-
_ = cols
364+
if len(cols) == 0 {
365+
t.Error("expected qualified columns from UPDATE")
366+
}
319367
})
320368

321369
t.Run("qualified cols from DELETE", func(t *testing.T) {
322370
r := mustParseHelper(t, "DELETE FROM users WHERE id = 1")
323371
cols := ExtractColumnsQualified(r)
324-
_ = cols
372+
if len(cols) == 0 {
373+
t.Error("expected qualified columns from DELETE")
374+
}
325375
})
326376

327377
t.Run("qualified cols from CTE", func(t *testing.T) {
328378
r := mustParseHelper(t, "WITH cte AS (SELECT u.id, u.name FROM users u) SELECT cte.id FROM cte")
329379
cols := ExtractColumnsQualified(r)
330-
_ = cols
380+
if len(cols) == 0 {
381+
t.Error("expected qualified columns from CTE")
382+
}
331383
})
332384

333385
t.Run("qualified cols from UNION", func(t *testing.T) {
334386
r := mustParseHelper(t, "SELECT u.name FROM users u UNION SELECT o.name FROM orders o")
335387
cols := ExtractColumnsQualified(r)
336-
_ = cols
388+
if len(cols) == 0 {
389+
t.Error("expected qualified columns from UNION")
390+
}
337391
})
338392

339393
t.Run("functions from INSERT with values", func(t *testing.T) {
340394
r := mustParseHelper(t, "INSERT INTO t (name) VALUES (UPPER('test'))")
341395
fns := ExtractFunctions(r)
342-
_ = fns
396+
if len(fns) == 0 {
397+
t.Error("expected functions from INSERT VALUES")
398+
}
343399
})
344400

345401
t.Run("functions from UPDATE", func(t *testing.T) {
346402
r := mustParseHelper(t, "UPDATE users SET name = UPPER('test') WHERE LENGTH(name) > 5")
347403
fns := ExtractFunctions(r)
348-
_ = fns
404+
if len(fns) >= 0 {
405+
// UPPER and LENGTH expected
406+
if len(fns) < 2 {
407+
t.Errorf("expected >= 2 functions from UPDATE, got %d", len(fns))
408+
}
409+
}
349410
})
350411

351412
t.Run("functions from DELETE", func(t *testing.T) {
352413
r := mustParseHelper(t, "DELETE FROM users WHERE LENGTH(name) = 0")
353414
fns := ExtractFunctions(r)
354-
_ = fns
415+
if len(fns) == 0 {
416+
t.Error("expected functions from DELETE WHERE")
417+
}
355418
})
356419

357420
t.Run("functions from CTE", func(t *testing.T) {
358421
r := mustParseHelper(t, "WITH cte AS (SELECT COUNT(*) as cnt FROM users) SELECT cnt FROM cte")
359422
fns := ExtractFunctions(r)
360-
_ = fns
423+
if len(fns) == 0 {
424+
t.Error("expected functions from CTE")
425+
}
361426
})
362427

363428
t.Run("functions from UNION", func(t *testing.T) {
364429
r := mustParseHelper(t, "SELECT COUNT(*) FROM t1 UNION SELECT SUM(x) FROM t2")
365430
fns := ExtractFunctions(r)
366-
_ = fns
431+
if len(fns) < 2 {
432+
t.Errorf("expected >= 2 functions from UNION, got %d", len(fns))
433+
}
367434
})
368435

369436
t.Run("functions from CASE expression", func(t *testing.T) {
370437
r := mustParseHelper(t, "SELECT CASE WHEN COUNT(*) > 0 THEN MAX(x) ELSE MIN(x) END FROM t")
371438
fns := ExtractFunctions(r)
372-
_ = fns
439+
if len(fns) < 2 {
440+
t.Errorf("expected >= 2 functions from CASE, got %d", len(fns))
441+
}
373442
})
374443

375444
t.Run("functions from IN expression", func(t *testing.T) {
376445
r := mustParseHelper(t, "SELECT * FROM t WHERE UPPER(name) IN ('A', 'B')")
377446
fns := ExtractFunctions(r)
378-
_ = fns
447+
if len(fns) == 0 {
448+
t.Error("expected functions from IN expression")
449+
}
379450
})
380451

381452
t.Run("functions from BETWEEN", func(t *testing.T) {
382453
r := mustParseHelper(t, "SELECT * FROM t WHERE ABS(x) BETWEEN 0 AND 100")
383454
fns := ExtractFunctions(r)
384-
_ = fns
455+
if len(fns) == 0 {
456+
t.Error("expected functions from BETWEEN expression")
457+
}
385458
})
386459

387460
t.Run("tables from CTE", func(t *testing.T) {
388461
r := mustParseHelper(t, "WITH cte AS (SELECT * FROM users) SELECT * FROM cte")
389462
tables := ExtractTables(r)
390-
_ = tables
463+
if len(tables) == 0 {
464+
t.Error("expected tables from CTE")
465+
}
391466
})
392467

393468
t.Run("tables from INSERT", func(t *testing.T) {
394469
r := mustParseHelper(t, "INSERT INTO t2 (name) SELECT name FROM t1")
395470
tables := ExtractTables(r)
396-
_ = tables
471+
if len(tables) < 2 {
472+
t.Errorf("expected >= 2 tables from INSERT...SELECT, got %d", len(tables))
473+
}
397474
})
398475

399476
t.Run("tables from UPDATE with subquery in WHERE", func(t *testing.T) {
400477
r := mustParseHelper(t, "UPDATE users SET active = true WHERE id IN (SELECT id FROM admins)")
401478
tables := ExtractTables(r)
402-
_ = tables
479+
if len(tables) < 2 {
480+
t.Errorf("expected >= 2 tables from UPDATE with subquery, got %d", len(tables))
481+
}
403482
})
404483

405484
t.Run("qualified tables from CTE", func(t *testing.T) {
406485
r := mustParseHelper(t, "WITH cte AS (SELECT * FROM users) SELECT * FROM cte")
407486
tables := ExtractTablesQualified(r)
408-
_ = tables
487+
if len(tables) == 0 {
488+
t.Error("expected qualified tables from CTE")
489+
}
409490
})
410491

411492
t.Run("columns from GROUP BY and HAVING", func(t *testing.T) {
412493
r := mustParseHelper(t, "SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*) > 5")
413494
cols := ExtractColumns(r)
414-
_ = cols
495+
if len(cols) == 0 {
496+
t.Error("expected columns from GROUP BY/HAVING")
497+
}
415498
})
416499

417500
t.Run("columns from ORDER BY", func(t *testing.T) {
418501
r := mustParseHelper(t, "SELECT name, age FROM users ORDER BY age DESC")
419502
cols := ExtractColumns(r)
420-
_ = cols
503+
if len(cols) == 0 {
504+
t.Error("expected columns from ORDER BY")
505+
}
421506
})
422507

423508
t.Run("qualified cols with GROUP BY HAVING ORDER BY", func(t *testing.T) {
424509
r := mustParseHelper(t, "SELECT u.dept, COUNT(*) FROM users u GROUP BY u.dept HAVING COUNT(*) > 5 ORDER BY u.dept")
425510
cols := ExtractColumnsQualified(r)
426-
_ = cols
511+
if len(cols) == 0 {
512+
t.Error("expected qualified columns from GROUP BY/HAVING/ORDER BY")
513+
}
427514
})
428515

429516
t.Run("functions from HAVING and ORDER BY", func(t *testing.T) {
430517
r := mustParseHelper(t, "SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING SUM(salary) > 1000 ORDER BY COUNT(*)")
431518
fns := ExtractFunctions(r)
432-
_ = fns
519+
if len(fns) < 2 {
520+
t.Errorf("expected >= 2 functions from HAVING/ORDER BY, got %d", len(fns))
521+
}
433522
})
434523

435524
t.Run("nil AST", func(t *testing.T) {

0 commit comments

Comments
 (0)