@@ -2,6 +2,7 @@ package handler
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
"fmt"
6
7
"net/http"
7
8
"net/http/httptest"
@@ -14,8 +15,9 @@ import (
14
15
15
16
"github.com/pressly/lg"
16
17
"github.com/stretchr/testify/assert"
18
+ "github.com/stretchr/testify/require"
17
19
"github.com/stretchr/testify/suite"
18
- "gopkg.in/DATA-DOG/go-sqlmock.v1"
20
+ sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
19
21
"gopkg.in/bblfsh/sdk.v2/uast/nodes"
20
22
)
21
23
@@ -43,7 +45,7 @@ func (suite *QuerySuite) TestAddLimit() {
43
45
` , "SELECT * FROM repositories LIMIT 100" },
44
46
{" SELECT * FROM repositories " , "SELECT * FROM repositories LIMIT 100" },
45
47
{" SELECT * FROM repositories ; " , "SELECT * FROM repositories LIMIT 100" },
46
- {` SELECT * FROM repositories
48
+ {` SELECT * FROM repositories
47
49
; ` , "SELECT * FROM repositories LIMIT 100" },
48
50
{"/* comment */ SELECT * FROM repositories" , "SELECT * FROM repositories LIMIT 100" },
49
51
{"SELECT * FROM repositories /* comment */" , "SELECT * FROM repositories LIMIT 100" },
@@ -56,7 +58,7 @@ func (suite *QuerySuite) TestAddLimit() {
56
58
{"select * from repositories limit 1 ;" , "select * from repositories limit 1" },
57
59
{`select * from repositories limit 1
58
60
;` , "select * from repositories limit 1" },
59
- {`select * from repositories limit 1
61
+ {`select * from repositories limit 1
60
62
; ` , "select * from repositories limit 1" },
61
63
{"select * from repositories limit 900" , "select * from repositories LIMIT 100" },
62
64
{"select * from repositories limit 900;" , "select * from repositories LIMIT 100" },
@@ -98,6 +100,8 @@ func (suite *QuerySuite) TestBadRequest() {
98
100
}
99
101
100
102
func (suite * QuerySuite ) TestQueryErr () {
103
+ mockProcessRows := sqlmock .NewRows ([]string {"Id" }).AddRow (1288 )
104
+ suite .mock .ExpectQuery ("SELECT CONNECTION_ID()" ).WillReturnRows (mockProcessRows )
101
105
suite .mock .ExpectQuery (".*" ).WillReturnError (fmt .Errorf ("forced err" ))
102
106
103
107
json := `{"query": "select * from repositories"}`
@@ -108,12 +112,25 @@ func (suite *QuerySuite) TestQueryErr() {
108
112
suite .Equal (http .StatusBadRequest , res .Code )
109
113
}
110
114
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
+
111
126
func (suite * QuerySuite ) TestQuery () {
112
127
rows := sqlmock .NewRows ([]string {"a" , "b" , "c" , "d" }).
113
128
AddRow (1 , "one" , 1.5 , 100 ).
114
129
AddRow (nil , nil , nil , nil )
115
130
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 )
117
134
118
135
json := `{"query": "select * from repositories"}`
119
136
req , _ := http .NewRequest ("POST" , "/query" , strings .NewReader (json ))
@@ -199,15 +216,14 @@ func (suite *QuerySuite) TestQueryAbort() {
199
216
// Ideally we would test that the sql query context is canceled, but
200
217
// go-sqlmock does not have something like ExpectContextCancellation
201
218
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
+
202
224
mockRows := sqlmock .NewRows ([]string {"a" , "b" , "c" , "d" }).AddRow (1 , "one" , 1.5 , 100 )
203
225
suite .mock .ExpectQuery (`select \* from repositories` ).WillDelayFor (2 * time .Second ).WillReturnRows (mockRows )
204
226
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
-
211
227
suite .mock .ExpectExec ("KILL 1288" )
212
228
213
229
json := `{"query": "select * from repositories"}`
@@ -224,8 +240,8 @@ func (suite *QuerySuite) TestQueryAbort() {
224
240
defer wg .Done ()
225
241
226
242
_ , 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 )
229
245
}
230
246
231
247
go func () {
@@ -242,5 +258,5 @@ func (suite *QuerySuite) TestQueryAbort() {
242
258
243
259
wg .Wait ()
244
260
245
- suite .Equal (context .Canceled , ctx .Err ())
261
+ require .Equal (context .Canceled , ctx .Err ())
246
262
}
0 commit comments