@@ -18,6 +18,7 @@ import (
1818 "math/rand"
1919 "net/url"
2020 "os"
21+ "path/filepath"
2122 "reflect"
2223 "regexp"
2324 "runtime"
@@ -1080,67 +1081,158 @@ func TestExecer(t *testing.T) {
10801081 defer db .Close ()
10811082
10821083 _ , err = db .Exec (`
1083- create table foo (id integer ); -- one comment
1084- insert into foo(id) values (?);
1085- insert into foo(id) values (?);
1086- insert into foo(id) values (?); -- another comment
1084+ CREATE TABLE foo (id INTEGER ); -- one comment
1085+ INSERT INTO foo(id) VALUES (?);
1086+ INSERT INTO foo(id) VALUES (?);
1087+ INSERT INTO foo(id) VALUES (?); -- another comment
10871088 ` , 1 , 2 , 3 )
10881089 if err != nil {
10891090 t .Error ("Failed to call db.Exec:" , err )
10901091 }
10911092}
10921093
1093- func TestQueryer (t * testing.T ) {
1094- tempFilename := TempFilename (t )
1095- defer os .Remove (tempFilename )
1096- db , err := sql .Open ("sqlite3" , tempFilename )
1094+ func testQuery (t * testing.T , seed bool , test func (t * testing.T , db * sql.DB )) {
1095+ db , err := sql .Open ("sqlite3" , filepath .Join (t .TempDir (), "test.sqlite3" ))
10971096 if err != nil {
10981097 t .Fatal ("Failed to open database:" , err )
10991098 }
11001099 defer db .Close ()
11011100
1102- _ , err = db .Exec (`
1103- create table foo (id integer);
1104- ` )
1105- if err != nil {
1106- t .Error ("Failed to call db.Query:" , err )
1101+ if seed {
1102+ if _ , err := db .Exec (`create table foo (id integer);` ); err != nil {
1103+ t .Fatal (err )
1104+ }
1105+ _ , err := db .Exec (`
1106+ INSERT INTO foo(id) VALUES(?);
1107+ INSERT INTO foo(id) VALUES(?);
1108+ INSERT INTO foo(id) VALUES(?);
1109+ ` , 3 , 2 , 1 )
1110+ if err != nil {
1111+ t .Fatal (err )
1112+ }
11071113 }
11081114
1109- _ , err = db .Exec (`
1110- insert into foo(id) values(?);
1111- insert into foo(id) values(?);
1112- insert into foo(id) values(?);
1113- ` , 3 , 2 , 1 )
1114- if err != nil {
1115- t .Error ("Failed to call db.Exec:" , err )
1116- }
1117- rows , err := db .Query (`
1118- select id from foo order by id;
1119- ` )
1120- if err != nil {
1121- t .Error ("Failed to call db.Query:" , err )
1122- }
1123- defer rows .Close ()
1124- n := 0
1125- for rows .Next () {
1126- var id int
1127- err = rows .Scan (& id )
1115+ // Capture panic so tests can continue
1116+ defer func () {
1117+ if e := recover (); e != nil {
1118+ buf := make ([]byte , 32 * 1024 )
1119+ n := runtime .Stack (buf , false )
1120+ t .Fatalf ("\n panic: %v\n \n %s\n " , e , buf [:n ])
1121+ }
1122+ }()
1123+ test (t , db )
1124+ }
1125+
1126+ func testQueryValues (t * testing.T , query string , args ... interface {}) []interface {} {
1127+ var values []interface {}
1128+ testQuery (t , true , func (t * testing.T , db * sql.DB ) {
1129+ rows , err := db .Query (query , args ... )
11281130 if err != nil {
1129- t .Error ( "Failed to db.Query:" , err )
1131+ t .Fatal ( err )
11301132 }
1131- if id != n + 1 {
1132- t .Error ( "Failed to db.Query: not matched results " )
1133+ if rows == nil {
1134+ t .Fatal ( "nil rows " )
11331135 }
1134- n = n + 1
1136+ for i := 0 ; rows .Next (); i ++ {
1137+ if i > 1_000 {
1138+ t .Fatal ("To many iterations of rows.Next():" , i )
1139+ }
1140+ var v interface {}
1141+ if err := rows .Scan (& v ); err != nil {
1142+ t .Fatal (err )
1143+ }
1144+ values = append (values , v )
1145+ }
1146+ if err := rows .Err (); err != nil {
1147+ t .Fatal (err )
1148+ }
1149+ if err := rows .Close (); err != nil {
1150+ t .Fatal (err )
1151+ }
1152+ })
1153+ return values
1154+ }
1155+
1156+ func TestQuery (t * testing.T ) {
1157+ queries := []struct {
1158+ query string
1159+ args []interface {}
1160+ }{
1161+ {"SELECT id FROM foo ORDER BY id;" , nil },
1162+ {"SELECT id FROM foo WHERE id != ? ORDER BY id;" , []interface {}{4 }},
1163+ {"SELECT id FROM foo WHERE id IN (?, ?, ?) ORDER BY id;" , []interface {}{1 , 2 , 3 }},
1164+
1165+ // Comments
1166+ {"SELECT id FROM foo ORDER BY id; -- comment" , nil },
1167+ {"SELECT id FROM foo ORDER BY id;\n -- comment\n " , nil },
1168+ {
1169+ `-- FOO
1170+ SELECT id FROM foo ORDER BY id; -- BAR
1171+ /* BAZ */` ,
1172+ nil ,
1173+ },
11351174 }
1136- if err := rows .Err (); err != nil {
1137- t .Errorf ("Post-scan failed: %v\n " , err )
1175+ want := []interface {}{
1176+ int64 (1 ),
1177+ int64 (2 ),
1178+ int64 (3 ),
1179+ }
1180+ for _ , q := range queries {
1181+ t .Run ("" , func (t * testing.T ) {
1182+ got := testQueryValues (t , q .query , q .args ... )
1183+ if ! reflect .DeepEqual (got , want ) {
1184+ t .Fatalf ("Query(%q, %v) = %v; want: %v" , q .query , q .args , got , want )
1185+ }
1186+ })
11381187 }
1139- if n != 3 {
1140- t .Errorf ("Expected 3 rows but retrieved %v" , n )
1188+ }
1189+
1190+ func TestQueryNoSQL (t * testing.T ) {
1191+ got := testQueryValues (t , "" )
1192+ if got != nil {
1193+ t .Fatalf ("Query(%q, %v) = %v; want: %v" , "" , nil , got , nil )
11411194 }
11421195}
11431196
1197+ func testQueryError (t * testing.T , query string , args ... interface {}) {
1198+ testQuery (t , true , func (t * testing.T , db * sql.DB ) {
1199+ rows , err := db .Query (query , args ... )
1200+ if err == nil {
1201+ t .Error ("Expected an error got:" , err )
1202+ }
1203+ if rows != nil {
1204+ t .Error ("Returned rows should be nil on error!" )
1205+ // Attempt to iterate over rows to make sure they don't panic.
1206+ for i := 0 ; rows .Next (); i ++ {
1207+ if i > 1_000 {
1208+ t .Fatal ("To many iterations of rows.Next():" , i )
1209+ }
1210+ }
1211+ if err := rows .Err (); err != nil {
1212+ t .Error (err )
1213+ }
1214+ rows .Close ()
1215+ }
1216+ })
1217+ }
1218+
1219+ func TestQueryNotEnoughArgs (t * testing.T ) {
1220+ testQueryError (t , "SELECT FROM foo WHERE id = ? OR id = ?);" , 1 )
1221+ }
1222+
1223+ func TestQueryTooManyArgs (t * testing.T ) {
1224+ // TODO: test error message / kind
1225+ testQueryError (t , "SELECT FROM foo WHERE id = ?);" , 1 , 2 )
1226+ }
1227+
1228+ func TestQueryMultipleStatements (t * testing.T ) {
1229+ testQueryError (t , "SELECT 1; SELECT 2;" )
1230+ }
1231+
1232+ func TestQueryInvalidTable (t * testing.T ) {
1233+ testQueryError (t , "SELECT COUNT(*) FROM does_not_exist;" )
1234+ }
1235+
11441236func TestStress (t * testing.T ) {
11451237 tempFilename := TempFilename (t )
11461238 defer os .Remove (tempFilename )
@@ -2112,7 +2204,6 @@ var benchmarks = []testing.InternalBenchmark{
21122204 {Name : "BenchmarkRows" , F : benchmarkRows },
21132205 {Name : "BenchmarkStmtRows" , F : benchmarkStmtRows },
21142206 {Name : "BenchmarkExecStep" , F : benchmarkExecStep },
2115- {Name : "BenchmarkQueryStep" , F : benchmarkQueryStep },
21162207}
21172208
21182209func (db * TestDB ) mustExec (sql string , args ... any ) sql.Result {
@@ -2580,12 +2671,3 @@ func benchmarkExecStep(b *testing.B) {
25802671 }
25812672 }
25822673}
2583-
2584- func benchmarkQueryStep (b * testing.B ) {
2585- var i int
2586- for n := 0 ; n < b .N ; n ++ {
2587- if err := db .QueryRow (largeSelectStmt ).Scan (& i ); err != nil {
2588- b .Fatal (err )
2589- }
2590- }
2591- }
0 commit comments