From 486fb73ee5044f5e3fde8a9d534d078e6a89a881 Mon Sep 17 00:00:00 2001 From: Ian Tan Date: Tue, 21 Nov 2017 12:26:18 +0800 Subject: [PATCH] Add documentation to prevent go lint from complaining --- expecter.go | 3 +++ expecter_adapter.go | 9 +++++++++ expecter_result.go | 15 ++++++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/expecter.go b/expecter.go index 7251dd3a..1dab967c 100644 --- a/expecter.go +++ b/expecter.go @@ -11,6 +11,7 @@ type Recorder struct { stmt string } +// Stmt represents a sql statement. It can be an Exec or Query type Stmt struct { stmtType string sql string @@ -64,10 +65,12 @@ type DefaultNoopDB struct{} // NoopResult is a noop struct that satisfies sql.Result type NoopResult struct{} +// LastInsertId is a noop method for satisfying drive.Result func (r NoopResult) LastInsertId() (int64, error) { return 1, nil } +// RowsAffected is a noop method for satisfying drive.Result func (r NoopResult) RowsAffected() (int64, error) { return 1, nil } diff --git a/expecter_adapter.go b/expecter_adapter.go index 8e3025db..7e3d3da9 100644 --- a/expecter_adapter.go +++ b/expecter_adapter.go @@ -36,6 +36,8 @@ type SqlmockAdapter struct { 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) { 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 } +// ExpectQuery wraps the underlying mock method for setting a query +// expectation func (a *SqlmockAdapter) ExpectQuery(stmt string) ExpectedQuery { q := a.mocker.ExpectQuery(stmt) return &SqlmockQuery{query: q} } +// ExpectExec wraps the underlying mock method for setting a exec +// expectation func (a *SqlmockAdapter) ExpectExec(stmt string) ExpectedExec { e := a.mocker.ExpectExec(stmt) 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 { return a.mocker.ExpectationsWereMet() } diff --git a/expecter_result.go b/expecter_result.go index 16a2a1e0..0f2ecc6e 100644 --- a/expecter_result.go +++ b/expecter_result.go @@ -8,15 +8,21 @@ import ( 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 { 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 { Returns(result driver.Result) ExpectedExec } -// SqlmockQuery implements Query for asserter go-sqlmock +// SqlmockQuery implements Query for go-sqlmock type SqlmockQuery struct { query *sqlmock.ExpectedQuery } @@ -89,6 +95,9 @@ func (q *SqlmockQuery) getRowsForOutType(out interface{}) *sqlmock.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 { rows := q.getRowsForOutType(out) q.query = q.query.WillReturnRows(rows) @@ -96,10 +105,14 @@ func (q *SqlmockQuery) Returns(out interface{}) ExpectedQuery { return q } +// SqlmockExec implements Exec for go-sqlmock type SqlmockExec struct { 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 { e.exec = e.exec.WillReturnResult(result)