1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2025-06-06 21:46:11 +02:00
This commit is contained in:
gedi 2015-09-10 21:31:35 +03:00
parent febff80c09
commit 081a694b0a
4 changed files with 74 additions and 8 deletions

View File

@ -4,9 +4,11 @@ go:
- 1.2 - 1.2
- 1.3 - 1.3
- 1.4 - 1.4
- 1.5
- release - release
- tip - tip
script: script:
- go test -v ./... - go get -t
- go test -race ./... - go test -v
- go test -race

View File

@ -39,7 +39,7 @@ func (d *mockDriver) Open(dsn string) (driver.Conn, error) {
// and a mock to manage expectations. // and a mock to manage expectations.
// Pings db so that all expectations could be // Pings db so that all expectations could be
// asserted. // asserted.
func New() (db *sql.DB, mock Sqlmock, err error) { func New() (*sql.DB, Sqlmock, error) {
pool.Lock() pool.Lock()
dsn := fmt.Sprintf("sqlmock_db_%d", pool.counter) dsn := fmt.Sprintf("sqlmock_db_%d", pool.counter)
pool.counter++ pool.counter++
@ -48,9 +48,31 @@ func New() (db *sql.DB, mock Sqlmock, err error) {
pool.conns[dsn] = smock pool.conns[dsn] = smock
pool.Unlock() pool.Unlock()
db, err = sql.Open("sqlmock", dsn) return smock.open()
if err != nil { }
return
} // NewWithDSN creates sqlmock database connection
return db, smock, db.Ping() // with a specific DSN and a mock to manage expectations.
// Pings db so that all expectations could be asserted.
//
// This method is introduced because of sql abstraction
// libraries, which do not provide a way to initialize
// with sql.DB instance. For example GORM library.
//
// Note, it will error if attempted to create with an
// already used dsn
//
// It is not recommended to use this method, unless you
// really need it and there is no other way around.
func NewWithDSN(dsn string) (*sql.DB, Sqlmock, error) {
pool.Lock()
if _, ok := pool.conns[dsn]; ok {
pool.Unlock()
return nil, nil, fmt.Errorf("cannot create a new mock database with the same dsn: %s", dsn)
}
smock := &sqlmock{dsn: dsn, drv: pool, ordered: true}
pool.conns[dsn] = smock
pool.Unlock()
return smock.open()
} }

View File

@ -3,8 +3,14 @@ package sqlmock
import ( import (
"fmt" "fmt"
"testing" "testing"
"github.com/jinzhu/gorm"
) )
type void struct{}
func (void) Print(...interface{}) {}
func ExampleNew() { func ExampleNew() {
db, mock, err := New() db, mock, err := New()
if err != nil { if err != nil {
@ -16,6 +22,33 @@ func ExampleNew() {
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call")) mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
} }
func ExampleNewWithDSN() {
_, mock, err := NewWithDSN("mydsn for gorm")
if err != nil {
fmt.Println("expected no error, but got:", err)
return
}
// use the same dsn to initialize a connection
db, err := gorm.Open("sqlmock", "mydsn for gorm")
if err != nil {
fmt.Println("expected no error from gorm, but got:", err)
return
}
defer db.Close()
db.SetLogger(void{}) // sad - nil panics
// now we can expect operations performed on db
mock.ExpectBegin().WillReturnError(fmt.Errorf("an error will occur on db.Begin() call"))
txconn := db.Begin()
for _, err := range txconn.GetErrors() {
fmt.Println(err.Error())
}
// Output: `sqlmock` is not officially supported, running under compatibility mode.
// an error will occur on db.Begin() call
}
func TestShouldOpenConnectionIssue15(t *testing.T) { func TestShouldOpenConnectionIssue15(t *testing.T) {
db, mock, err := New() db, mock, err := New()
if err != nil { if err != nil {

View File

@ -12,6 +12,7 @@ are also supported.
package sqlmock package sqlmock
import ( import (
"database/sql"
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"reflect" "reflect"
@ -79,6 +80,14 @@ type sqlmock struct {
expected []expectation expected []expectation
} }
func (s *sqlmock) open() (*sql.DB, Sqlmock, error) {
db, err := sql.Open("sqlmock", s.dsn)
if err != nil {
return db, s, err
}
return db, s, db.Ping()
}
func (c *sqlmock) ExpectClose() *ExpectedClose { func (c *sqlmock) ExpectClose() *ExpectedClose {
e := &ExpectedClose{} e := &ExpectedClose{}
c.expected = append(c.expected, e) c.expected = append(c.expected, e)