This commit is contained in:
Ian Tan 2017-11-20 10:25:12 +08:00
parent 8c06f3ff4f
commit 62740e8e1e

View File

@ -7,10 +7,13 @@ import (
"regexp" "regexp"
) )
// Recorder satisfies the logger interface
type Recorder struct { type Recorder struct {
stmt string stmt string
} }
// Print just sets the last recorded SQL statement
// TODO: find a better way to extract SQL from log messages
func (r *Recorder) Print(args ...interface{}) { func (r *Recorder) Print(args ...interface{}) {
msgs := LogFormatter(args...) msgs := LogFormatter(args...)
if len(msgs) >= 4 { if len(msgs) >= 4 {
@ -28,17 +31,15 @@ type AdapterFactory func(dialect string, args ...interface{}) (*DB, Adapter, err
type Expecter struct { type Expecter struct {
// globally scoped expecter // globally scoped expecter
adapter Adapter adapter Adapter
noop NoopDB noop SQLCommon
gorm *DB gorm *DB
recorder *Recorder recorder *Recorder
} }
type NoopDB interface { // DefaultNoopDB is a noop db used to get generated sql from gorm.DB
GetStmts() []string
}
type DefaultNoopDB struct{} type DefaultNoopDB struct{}
// NoopResult is a noop struct that satisfies sql.Result
type NoopResult struct{} type NoopResult struct{}
func (r NoopResult) LastInsertId() (int64, error) { func (r NoopResult) LastInsertId() (int64, error) {
@ -49,30 +50,31 @@ func (r NoopResult) RowsAffected() (int64, error) {
return 1, nil return 1, nil
} }
func NewNoopDB() NoopDB { // NewNoopDB initialises a new DefaultNoopDB
func NewNoopDB() SQLCommon {
return &DefaultNoopDB{} return &DefaultNoopDB{}
} }
// Exec simulates a sql.DB.Exec
func (r *DefaultNoopDB) Exec(query string, args ...interface{}) (sql.Result, error) { func (r *DefaultNoopDB) Exec(query string, args ...interface{}) (sql.Result, error) {
return NoopResult{}, nil return NoopResult{}, nil
} }
// Prepare simulates a sql.DB.Prepare
func (r *DefaultNoopDB) Prepare(query string) (*sql.Stmt, error) { func (r *DefaultNoopDB) Prepare(query string) (*sql.Stmt, error) {
return &sql.Stmt{}, nil return &sql.Stmt{}, nil
} }
// Query simulates a sql.DB.Query
func (r *DefaultNoopDB) Query(query string, args ...interface{}) (*sql.Rows, error) { func (r *DefaultNoopDB) Query(query string, args ...interface{}) (*sql.Rows, error) {
return nil, errors.New("noop") return nil, errors.New("noop")
} }
// QueryRow simulates a sql.DB.QueryRow
func (r *DefaultNoopDB) QueryRow(query string, args ...interface{}) *sql.Row { func (r *DefaultNoopDB) QueryRow(query string, args ...interface{}) *sql.Row {
return &sql.Row{} return &sql.Row{}
} }
func (r *DefaultNoopDB) GetStmts() []string {
return []string{"not", "implemented"}
}
// NewDefaultExpecter returns a Expecter powered by go-sqlmock // NewDefaultExpecter returns a Expecter powered by go-sqlmock
func NewDefaultExpecter() (*DB, *Expecter, error) { func NewDefaultExpecter() (*DB, *Expecter, error) {
gormDb, adapter, err := NewSqlmockAdapter("sqlmock", "mock_gorm_dsn") gormDb, adapter, err := NewSqlmockAdapter("sqlmock", "mock_gorm_dsn")
@ -83,7 +85,6 @@ func NewDefaultExpecter() (*DB, *Expecter, error) {
recorder := &Recorder{} recorder := &Recorder{}
noop := &DefaultNoopDB{} noop := &DefaultNoopDB{}
gorm := &DB{ gorm := &DB{
db: noop, db: noop,
logger: recorder, logger: recorder,
@ -111,6 +112,7 @@ func NewExpecter(fn AdapterFactory, dialect string, args ...interface{}) (*DB, *
/* PUBLIC METHODS */ /* PUBLIC METHODS */
// AssertExpectations checks if all expected Querys and Execs were satisfied.
func (h *Expecter) AssertExpectations() error { func (h *Expecter) AssertExpectations() error {
return h.adapter.AssertExpectations() return h.adapter.AssertExpectations()
} }