Repository: nuveo/prest
Pull request #154 (2017-05-30 16:26:09)

a mock of database connection

codecov[bot]: # [Codecov](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=h1) Report
> Merging [#154](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=desc) into [master](https://codecov.io/gh/nuveo/prest/commit/c9fd47e3bb1e3cd857a85386e1e3df003d446b52?src=pr&el=desc) will **decrease** coverage by `0.1%`.
> The diff coverage is `71.42%`.

[![Impacted file tree graph](https://codecov.io/gh/nuveo/prest/pull/154/graphs/tree.svg?token=eVD9urwIEv&height=150&src=pr&width=650)](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #154      +/-   ##
==========================================
- Coverage   85.32%   85.21%   -0.11%     
==========================================
  Files          11       11              
  Lines         879      886       +7     
==========================================
+ Hits          750      755       +5     
- Misses         85       86       +1     
- Partials       44       45       +1
```


| [Impacted Files](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [adapters/postgres/connection/conn.go](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=tree#diff-YWRhcHRlcnMvcG9zdGdyZXMvY29ubmVjdGlvbi9jb25uLmdv) | `74.19% <71.42%> (-0.81%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=footer). Last update [c9fd47e...6eb84da](https://codecov.io/gh/nuveo/prest/pull/154?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/adapters/postgres/connection/conn.go b/adapters/postgres/connection/conn.go
index 7ad3a9c..1d13028 100644
--- a/adapters/postgres/connection/conn.go
+++ b/adapters/postgres/connection/conn.go
@@ -5,6 +5,7 @@ import (
 
 	"database/sql"
 
+	sqlmock "github.com/DATA-DOG/go-sqlmock"
 	"github.com/jmoiron/sqlx"
 	"github.com/nuveo/prest/config"
 	// Used pg drive on sqlx
@@ -12,13 +13,14 @@ import (
 )
 
 var (
-	db  *sqlx.DB
+	// DB connection
+	DB  *sqlx.DB
 	err error
 )
 
 // Get get postgres connection
 func Get() (*sqlx.DB, error) {
-	if db == nil {
+	if DB == nil {
 		dbURI := fmt.Sprintf("user=%s dbname=%s host=%s port=%v sslmode=disable connect_timeout=%d",
 			config.PrestConf.PGUser,
 			config.PrestConf.PGDatabase,
@@ -28,27 +30,39 @@ func Get() (*sqlx.DB, error) {
 		if config.PrestConf.PGPass != "" {
 			dbURI += " password=" + config.PrestConf.PGPass
 		}
-		db, err = sqlx.Connect("postgres", dbURI)
+		DB, err = sqlx.Connect("postgres", dbURI)
 		if err != nil {
 			return nil, err
 		}
-		db.SetMaxIdleConns(config.PrestConf.PGMaxIdleConn)
-		db.SetMaxOpenConns(config.PrestConf.PGMAxOpenConn)
+		DB.SetMaxIdleConns(config.PrestConf.PGMaxIdleConn)
+		DB.SetMaxOpenConns(config.PrestConf.PGMAxOpenConn)
 	}
-	return db, nil
+	return DB, nil
 }
 
 // MustGet get postgres connection
 func MustGet() *sqlx.DB {
 	var err error
-	db, err = Get()
+	DB, err = Get()
 	if err != nil {
 		panic(fmt.Sprintf("Unable to connect to database: %v\n", err))
 	}
-	return db
+	return DB
 }
 
 // SetNativeDB enable to override sqlx native db
 func SetNativeDB(native *sql.DB) {
-	db.DB = native
+	DB.DB = native
+}
+
+// UseMockDB mock database
+func UseMockDB(driverName string) (mock sqlmock.Sqlmock, err error) {
+	var nativeDB *sql.DB
+
+	nativeDB, mock, err = sqlmock.New()
+	if err != nil {
+		return
+	}
+	DB = sqlx.NewDb(nativeDB, driverName)
+	return
 }


Test patch:
diff --git a/adapters/postgres/connection/conn_test.go b/adapters/postgres/connection/conn_test.go
index 371e26f..907b9ae 100644
--- a/adapters/postgres/connection/conn_test.go
+++ b/adapters/postgres/connection/conn_test.go
@@ -54,3 +54,13 @@ func TestSetNativeDB(t *testing.T) {
 		t.Errorf("expected same memory address, but no was! %v %v", db.DB, mockedDB)
 	}
 }
+
+func TestUseMockDB(t *testing.T) {
+	mock, err := UseMockDB("postgres")
+	if err != nil {
+		t.Errorf("expected no error, but has %v", err)
+	}
+	if mock == nil {
+		t.Error("mock is nil")
+	}
+}
diff --git a/adapters/postgres/postgres_test.go b/adapters/postgres/postgres_test.go
index 7492b11..af19a9b 100644
--- a/adapters/postgres/postgres_test.go
+++ b/adapters/postgres/postgres_test.go
@@ -7,6 +7,8 @@ import (
 	"strings"
 	"testing"
 
+	sqlmock "github.com/DATA-DOG/go-sqlmock"
+	"github.com/nuveo/prest/adapters/postgres/connection"
 	"github.com/nuveo/prest/api"
 	"github.com/nuveo/prest/config"
 	"github.com/nuveo/prest/statements"
@@ -812,3 +814,21 @@ func TestTimeout(t *testing.T) {
 		t.Errorf("Error disabling statement_timeout")
 	}
 }
+
+func TestMockUsage(t *testing.T) {
+	mock, err := connection.UseMockDB("postgres")
+	if err != nil {
+		t.Errorf("expected no error, but has %v", err)
+	}
+	mock.ExpectPrepare("SELECT *").
+		ExpectQuery().
+		WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(1, "prest"))
+	jsonData, err := Query("SELECT * FROM test")
+	if err != nil {
+		t.Errorf("expected no error, but has %v", err)
+	}
+	if len(jsonData) <= 0 {
+		t.Errorf("json data is empty")
+	}
+	connection.DB = nil
+}