Add documentation to prevent go lint from complaining

This commit is contained in:
Ian Tan 2017-11-21 12:26:18 +08:00
parent e9a29091fa
commit 486fb73ee5
3 changed files with 26 additions and 1 deletions

View File

@ -11,6 +11,7 @@ type Recorder struct {
stmt string stmt string
} }
// Stmt represents a sql statement. It can be an Exec or Query
type Stmt struct { type Stmt struct {
stmtType string stmtType string
sql string sql string
@ -64,10 +65,12 @@ type DefaultNoopDB struct{}
// NoopResult is a noop struct that satisfies sql.Result // NoopResult is a noop struct that satisfies sql.Result
type NoopResult struct{} type NoopResult struct{}
// LastInsertId is a noop method for satisfying drive.Result
func (r NoopResult) LastInsertId() (int64, error) { func (r NoopResult) LastInsertId() (int64, error) {
return 1, nil return 1, nil
} }
// RowsAffected is a noop method for satisfying drive.Result
func (r NoopResult) RowsAffected() (int64, error) { func (r NoopResult) RowsAffected() (int64, error) {
return 1, nil return 1, nil
} }

View File

@ -36,6 +36,8 @@ type SqlmockAdapter struct {
mocker sqlmock.Sqlmock mocker sqlmock.Sqlmock
} }
// NewSqlmockAdapter returns a mock gorm.DB and an Adapter backed by
// go-sqlmock
func NewSqlmockAdapter(dialect string, args ...interface{}) (*DB, Adapter, error) { func NewSqlmockAdapter(dialect string, args ...interface{}) (*DB, Adapter, error) {
gormDb, err := Open("sqlmock", "mock_gorm_dsn") gormDb, err := Open("sqlmock", "mock_gorm_dsn")
@ -46,18 +48,25 @@ func NewSqlmockAdapter(dialect string, args ...interface{}) (*DB, Adapter, error
return gormDb, &SqlmockAdapter{db: db, mocker: mock}, nil return gormDb, &SqlmockAdapter{db: db, mocker: mock}, nil
} }
// ExpectQuery wraps the underlying mock method for setting a query
// expectation
func (a *SqlmockAdapter) ExpectQuery(stmt string) ExpectedQuery { func (a *SqlmockAdapter) ExpectQuery(stmt string) ExpectedQuery {
q := a.mocker.ExpectQuery(stmt) q := a.mocker.ExpectQuery(stmt)
return &SqlmockQuery{query: q} return &SqlmockQuery{query: q}
} }
// ExpectExec wraps the underlying mock method for setting a exec
// expectation
func (a *SqlmockAdapter) ExpectExec(stmt string) ExpectedExec { func (a *SqlmockAdapter) ExpectExec(stmt string) ExpectedExec {
e := a.mocker.ExpectExec(stmt) e := a.mocker.ExpectExec(stmt)
return &SqlmockExec{exec: e} return &SqlmockExec{exec: e}
} }
// AssertExpectations asserts that _all_ expectations for a test have been met
// and returns an error specifying which have not if there are unmet
// expectations
func (a *SqlmockAdapter) AssertExpectations() error { func (a *SqlmockAdapter) AssertExpectations() error {
return a.mocker.ExpectationsWereMet() return a.mocker.ExpectationsWereMet()
} }

View File

@ -8,15 +8,21 @@ import (
sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1" sqlmock "gopkg.in/DATA-DOG/go-sqlmock.v1"
) )
// ExpectedQuery represents an expected query that will be executed and can
// return some rows. It presents a fluent API for chaining calls to other
// expectations
type ExpectedQuery interface { type ExpectedQuery interface {
Returns(model interface{}) ExpectedQuery Returns(model interface{}) ExpectedQuery
} }
// ExpectedExec represents an expected exec that will be executed and can
// return a result. It presents a fluent API for chaining calls to other
// expectations
type ExpectedExec interface { type ExpectedExec interface {
Returns(result driver.Result) ExpectedExec Returns(result driver.Result) ExpectedExec
} }
// SqlmockQuery implements Query for asserter go-sqlmock // SqlmockQuery implements Query for go-sqlmock
type SqlmockQuery struct { type SqlmockQuery struct {
query *sqlmock.ExpectedQuery query *sqlmock.ExpectedQuery
} }
@ -89,6 +95,9 @@ func (q *SqlmockQuery) getRowsForOutType(out interface{}) *sqlmock.Rows {
return rows return rows
} }
// Returns accepts an out type which should either be a struct or slice. Under
// the hood, it converts a gorm model struct to sql.Rows that can be passed to
// the underlying mock db
func (q *SqlmockQuery) Returns(out interface{}) ExpectedQuery { func (q *SqlmockQuery) Returns(out interface{}) ExpectedQuery {
rows := q.getRowsForOutType(out) rows := q.getRowsForOutType(out)
q.query = q.query.WillReturnRows(rows) q.query = q.query.WillReturnRows(rows)
@ -96,10 +105,14 @@ func (q *SqlmockQuery) Returns(out interface{}) ExpectedQuery {
return q return q
} }
// SqlmockExec implements Exec for go-sqlmock
type SqlmockExec struct { type SqlmockExec struct {
exec *sqlmock.ExpectedExec exec *sqlmock.ExpectedExec
} }
// Returns accepts a driver.Result. It is passed directly to the underlying
// mock db. Useful for checking DAO behaviour in the event that the incorrect
// number of rows are affected by an Exec
func (e *SqlmockExec) Returns(result driver.Result) ExpectedExec { func (e *SqlmockExec) Returns(result driver.Result) ExpectedExec {
e.exec = e.exec.WillReturnResult(result) e.exec = e.exec.WillReturnResult(result)