@@ -2,6 +2,7 @@ package handler
22
33import (
44 "context"
5+ "database/sql"
56 "fmt"
67 "net/http"
78 "net/http/httptest"
@@ -14,8 +15,9 @@ import (
1415
1516 "github.com/pressly/lg"
1617 "github.com/stretchr/testify/assert"
18+ "github.com/stretchr/testify/require"
1719 "github.com/stretchr/testify/suite"
18- "gopkg.in/DATA-DOG/go-sqlmock.v1"
20+ sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
1921 "gopkg.in/bblfsh/sdk.v2/uast/nodes"
2022)
2123
@@ -43,7 +45,7 @@ func (suite *QuerySuite) TestAddLimit() {
4345 ` , "SELECT * FROM repositories LIMIT 100" },
4446 {" SELECT * FROM repositories " , "SELECT * FROM repositories LIMIT 100" },
4547 {" SELECT * FROM repositories ; " , "SELECT * FROM repositories LIMIT 100" },
46- {` SELECT * FROM repositories
48+ {` SELECT * FROM repositories
4749; ` , "SELECT * FROM repositories LIMIT 100" },
4850 {"/* comment */ SELECT * FROM repositories" , "SELECT * FROM repositories LIMIT 100" },
4951 {"SELECT * FROM repositories /* comment */" , "SELECT * FROM repositories LIMIT 100" },
@@ -56,7 +58,7 @@ func (suite *QuerySuite) TestAddLimit() {
5658 {"select * from repositories limit 1 ;" , "select * from repositories limit 1" },
5759 {`select * from repositories limit 1
5860;` , "select * from repositories limit 1" },
59- {`select * from repositories limit 1
61+ {`select * from repositories limit 1
6062 ; ` , "select * from repositories limit 1" },
6163 {"select * from repositories limit 900" , "select * from repositories LIMIT 100" },
6264 {"select * from repositories limit 900;" , "select * from repositories LIMIT 100" },
@@ -98,6 +100,8 @@ func (suite *QuerySuite) TestBadRequest() {
98100}
99101
100102func (suite * QuerySuite ) TestQueryErr () {
103+ mockProcessRows := sqlmock .NewRows ([]string {"Id" }).AddRow (1288 )
104+ suite .mock .ExpectQuery ("SELECT CONNECTION_ID()" ).WillReturnRows (mockProcessRows )
101105 suite .mock .ExpectQuery (".*" ).WillReturnError (fmt .Errorf ("forced err" ))
102106
103107 json := `{"query": "select * from repositories"}`
@@ -108,12 +112,25 @@ func (suite *QuerySuite) TestQueryErr() {
108112 suite .Equal (http .StatusBadRequest , res .Code )
109113}
110114
115+ func (suite * QuerySuite ) TestQueryConnIdErr () {
116+ suite .mock .ExpectQuery ("SELECT CONNECTION_ID()" ).WillReturnError (sql .ErrNoRows )
117+
118+ json := `{"query": "select * from repositories"}`
119+ req , _ := http .NewRequest ("POST" , "/query" , strings .NewReader (json ))
120+ res := httptest .NewRecorder ()
121+ suite .handler .ServeHTTP (res , req )
122+
123+ suite .Equal (http .StatusInternalServerError , res .Code )
124+ }
125+
111126func (suite * QuerySuite ) TestQuery () {
112127 rows := sqlmock .NewRows ([]string {"a" , "b" , "c" , "d" }).
113128 AddRow (1 , "one" , 1.5 , 100 ).
114129 AddRow (nil , nil , nil , nil )
115130
116- suite .mock .ExpectQuery (".*" ).WillReturnRows (rows )
131+ mockProcessRows := sqlmock .NewRows ([]string {"Id" }).AddRow (1288 )
132+ suite .mock .ExpectQuery ("SELECT CONNECTION_ID()" ).WillReturnRows (mockProcessRows )
133+ suite .mock .ExpectQuery (`select \* from repositories` ).WillReturnRows (rows )
117134
118135 json := `{"query": "select * from repositories"}`
119136 req , _ := http .NewRequest ("POST" , "/query" , strings .NewReader (json ))
@@ -199,15 +216,14 @@ func (suite *QuerySuite) TestQueryAbort() {
199216 // Ideally we would test that the sql query context is canceled, but
200217 // go-sqlmock does not have something like ExpectContextCancellation
201218
219+ require := require .New (suite .T ())
220+
221+ mockProcessRows := sqlmock .NewRows ([]string {"Id" }).AddRow (1288 )
222+ suite .mock .ExpectQuery ("SELECT CONNECTION_ID()" ).WillReturnRows (mockProcessRows )
223+
202224 mockRows := sqlmock .NewRows ([]string {"a" , "b" , "c" , "d" }).AddRow (1 , "one" , 1.5 , 100 )
203225 suite .mock .ExpectQuery (`select \* from repositories` ).WillDelayFor (2 * time .Second ).WillReturnRows (mockRows )
204226
205- mockProcessRows := sqlmock .NewRows (
206- []string {"Id" , "User" , "Host" , "db" , "Command" , "Time" , "State" , "Info" }).
207- AddRow (1234 , nil , "localhost:3306" , nil , "query" , 2 , "SquashedTable(refs, commit_files, files)(1/5)" , "select * from files" ).
208- AddRow (1288 , nil , "localhost:3306" , nil , "query" , 2 , "SquashedTable(refs, commit_files, files)(1/5)" , "select * from repositories" )
209- suite .mock .ExpectQuery ("SHOW FULL PROCESSLIST" ).WillReturnRows (mockProcessRows )
210-
211227 suite .mock .ExpectExec ("KILL 1288" )
212228
213229 json := `{"query": "select * from repositories"}`
@@ -224,8 +240,8 @@ func (suite *QuerySuite) TestQueryAbort() {
224240 defer wg .Done ()
225241
226242 _ , err := suite .requestProcessFunc (suite .db )(r )
227- suite .Error (err )
228- suite .Equal (context .Canceled , err )
243+ require .Error (err )
244+ require .Equal (context .Canceled , err )
229245 }
230246
231247 go func () {
@@ -242,5 +258,5 @@ func (suite *QuerySuite) TestQueryAbort() {
242258
243259 wg .Wait ()
244260
245- suite .Equal (context .Canceled , ctx .Err ())
261+ require .Equal (context .Canceled , ctx .Err ())
246262}
0 commit comments