Skip to content

Commit

Permalink
enable to mock database (#98)
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Oliveira <felipeweb.programador@gmail.com>
  • Loading branch information
felipeweb authored and Thiago Avelino committed Feb 7, 2017
1 parent 3e41cb3 commit a50fcf1
Show file tree
Hide file tree
Showing 13 changed files with 1,414 additions and 1 deletion.
9 changes: 8 additions & 1 deletion adapters/postgres/connection/conn.go
Expand Up @@ -3,6 +3,8 @@ package connection
import (
"fmt"

"database/sql"

"github.com/jmoiron/sqlx"
"github.com/nuveo/prest/config"
// Used pg drive on sqlx
Expand All @@ -18,7 +20,7 @@ var (
// MustGet get postgres connection
func MustGet() *sqlx.DB {
if db == nil {
cfg := config.Prest{}
cfg = config.Prest{}
config.Parse(&cfg)
dbURI := fmt.Sprintf("user=%s dbname=%s host=%s port=%v sslmode=disable", cfg.PGUser, cfg.PGDatabase, cfg.PGHost, cfg.PGPort)
if cfg.PGPass != "" {
Expand All @@ -33,3 +35,8 @@ func MustGet() *sqlx.DB {
}
return db
}

// SetNativeDB enable to override sqlx native db
func SetNativeDB(native *sql.DB) {
db.DB = native
}
18 changes: 18 additions & 0 deletions adapters/postgres/connection/conn_test.go
Expand Up @@ -2,6 +2,8 @@ package connection

import (
"testing"

sqlmock "github.com/DATA-DOG/go-sqlmock"
)

func TestMustGet(t *testing.T) {
Expand All @@ -17,3 +19,19 @@ func TestMustGet(t *testing.T) {
t.Errorf("expected no error, but got: %v", err)
}
}

func TestSetNativeDB(t *testing.T) {
t.Log("Open connection")
db := MustGet()
if db == nil {
t.Errorf("expected db connection, but no was!")
}
mockedDB, _, err := sqlmock.New()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}
SetNativeDB(mockedDB)
if db.DB != mockedDB {
t.Errorf("expected same memory address, but no was! %v %v", db.DB, mockedDB)
}
}
28 changes: 28 additions & 0 deletions vendor/github.com/DATA-DOG/go-sqlmock/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

223 changes: 223 additions & 0 deletions vendor/github.com/DATA-DOG/go-sqlmock/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions vendor/github.com/DATA-DOG/go-sqlmock/argument.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a50fcf1

Please sign in to comment.