Skip to content

Commit 86e1d6b

Browse files
committed
Use an interface for sql.DB to make easier tests
Signed-off-by: David Pordomingo <[email protected]>
1 parent 67246bb commit 86e1d6b

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

server/handler/query.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/src-d/gitbase-playground/server/serializer"
12+
"github.com/src-d/gitbase-playground/server/service"
1213
"gopkg.in/bblfsh/sdk.v1/uast"
1314

1415
"github.com/go-sql-driver/mysql"
@@ -46,7 +47,7 @@ func genericVals(colTypes []string) []interface{} {
4647

4748
// Query returns a function that forwards an SQL query to gitbase and returns
4849
// the rows as JSON
49-
func Query(db *sql.DB) RequestProcessFunc {
50+
func Query(db service.SQLDB) RequestProcessFunc {
5051
return func(r *http.Request) (*serializer.Response, error) {
5152
var queryRequest queryRequest
5253
body, err := ioutil.ReadAll(r.Body)

server/handler/query_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/src-d/gitbase-playground/server/handler"
1313
"github.com/src-d/gitbase-playground/server/serializer"
14+
"github.com/src-d/gitbase-playground/server/service"
1415

1516
"github.com/kelseyhightower/envconfig"
1617
"github.com/pressly/lg"
@@ -28,11 +29,11 @@ type appConfig struct {
2829

2930
type QuerySuite struct {
3031
suite.Suite
31-
db *sql.DB
32+
db service.SQLDB
3233
handler http.Handler
3334
}
3435

35-
func setupDB(require *require.Assertions) *sql.DB {
36+
func setupDB(require *require.Assertions) service.SQLDB {
3637
var conf appConfig
3738
envconfig.MustProcess("GITBASEPG", &conf)
3839

server/handler/tables.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package handler
22

33
import (
4-
"database/sql"
54
"net/http"
65
"strings"
76

87
"github.com/src-d/gitbase-playground/server/serializer"
8+
"github.com/src-d/gitbase-playground/server/service"
99
)
1010

1111
// Tables returns a function that calls /query with the SQL "SHOW TABLES"
12-
func Tables(db *sql.DB) RequestProcessFunc {
12+
func Tables(db service.SQLDB) RequestProcessFunc {
1313
return func(r *http.Request) (*serializer.Response, error) {
1414
req, _ := http.NewRequest("POST", "/query",
1515
strings.NewReader(`{ "query": "SHOW TABLES" }`))

server/handler/tables_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package handler_test
22

33
import (
4-
"database/sql"
54
"net/http"
65
"net/http/httptest"
76
"testing"
87

98
"github.com/sirupsen/logrus"
109
"github.com/src-d/gitbase-playground/server/handler"
10+
"github.com/src-d/gitbase-playground/server/service"
1111

1212
"github.com/pressly/lg"
1313
"github.com/stretchr/testify/suite"
@@ -18,7 +18,7 @@ import (
1818

1919
type TablesSuite struct {
2020
suite.Suite
21-
db *sql.DB
21+
db service.SQLDB
2222
handler http.Handler
2323
}
2424

server/router.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package server
22

33
import (
4-
"database/sql"
54
"net/http"
65

76
"github.com/src-d/gitbase-playground/server/handler"
7+
"github.com/src-d/gitbase-playground/server/service"
88

99
"github.com/go-chi/chi"
1010
"github.com/go-chi/chi/middleware"
@@ -18,7 +18,7 @@ func Router(
1818
logger *logrus.Logger,
1919
static *handler.Static,
2020
version string,
21-
db *sql.DB,
21+
db service.SQLDB,
2222
) http.Handler {
2323

2424
// cors options

server/service/common.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package service
2+
3+
import "database/sql"
4+
5+
// SQLDB describes a *sql.DB
6+
type SQLDB interface {
7+
Close() error
8+
Query(query string, args ...interface{}) (*sql.Rows, error)
9+
}

0 commit comments

Comments
 (0)