Skip to content

Commit

Permalink
documentation update based on lint
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed Feb 8, 2014
1 parent b89aa3a commit c33b881
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 97 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[![Build Status](https://travis-ci.org/DATA-DOG/go-sqlmock.png)](https://travis-ci.org/DATA-DOG/go-sqlmock)
[![GoDoc](https://godoc.org/github.com/DATA-DOG/go-sqlmock?status.png)](https://godoc.org/github.com/DATA-DOG/go-sqlmock)

# Sql driver mock for Golang

This is a **mock** driver as **database/sql/driver** which is very flexible and pragmatic to
manage and mock expected queries. All the expectations should be met and all queries and actions
triggered should be mocked in order to pass a test. See exported [api on godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)
triggered should be mocked in order to pass a test.

## Install

Expand Down Expand Up @@ -289,7 +290,7 @@ to compare them correctly, this may be improved.

## Documentation

See it on [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)
Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)

## TODO

Expand Down
30 changes: 15 additions & 15 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ type conn struct {
active expectation
}

// closes a mock database driver connection. It should
// Close a mock database driver connection. It should
// be always called to ensure that all expectations
// were met successfully
// were met successfully. Returns error if there is any
func (c *conn) Close() (err error) {
for _, e := range mock.conn.expectations {
if !e.fulfilled() {
err = fmt.Errorf("There is a remaining expectation %T which was not matched yet", e)
err = fmt.Errorf("there is a remaining expectation %T which was not matched yet", e)
break
}
}
Expand All @@ -28,12 +28,12 @@ func (c *conn) Close() (err error) {
func (c *conn) Begin() (driver.Tx, error) {
e := c.next()
if e == nil {
return nil, fmt.Errorf("All expectations were already fulfilled, call to Begin transaction was not expected")
return nil, fmt.Errorf("all expectations were already fulfilled, call to begin transaction was not expected")
}

etb, ok := e.(*expectedBegin)
if !ok {
return nil, fmt.Errorf("Call to Begin transaction, was not expected, next expectation is %T as %+v", e, e)
return nil, fmt.Errorf("call to begin transaction, was not expected, next expectation is %T as %+v", e, e)
}
etb.triggered = true
return &transaction{c}, etb.err
Expand All @@ -53,12 +53,12 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
e := c.next()
query = stripQuery(query)
if e == nil {
return nil, fmt.Errorf("All expectations were already fulfilled, call to Exec '%s' query with args %+v was not expected", query, args)
return nil, fmt.Errorf("all expectations were already fulfilled, call to exec '%s' query with args %+v was not expected", query, args)
}

eq, ok := e.(*expectedExec)
if !ok {
return nil, fmt.Errorf("Call to Exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
}

eq.triggered = true
Expand All @@ -67,15 +67,15 @@ func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
}

if eq.result == nil {
return nil, fmt.Errorf("Exec query '%s' with args %+v, must return a database/sql/driver.Result, but it was not set for expectation %T as %+v", query, args, eq, eq)
return nil, fmt.Errorf("exec query '%s' with args %+v, must return a database/sql/driver.result, but it was not set for expectation %T as %+v", query, args, eq, eq)
}

if !eq.queryMatches(query) {
return nil, fmt.Errorf("Exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String())
return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, eq.sqlRegex.String())
}

if !eq.argsMatches(args) {
return nil, fmt.Errorf("Exec query '%s', args %+v does not match expected %+v", query, args, eq.args)
return nil, fmt.Errorf("exec query '%s', args %+v does not match expected %+v", query, args, eq.args)
}

return eq.result, nil
Expand All @@ -89,12 +89,12 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
e := c.next()
query = stripQuery(query)
if e == nil {
return nil, fmt.Errorf("All expectations were already fulfilled, call to Query '%s' with args %+v was not expected", query, args)
return nil, fmt.Errorf("all expectations were already fulfilled, call to query '%s' with args %+v was not expected", query, args)
}

eq, ok := e.(*expectedQuery)
if !ok {
return nil, fmt.Errorf("Call to Query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is %T as %+v", query, args, e, e)
}

eq.triggered = true
Expand All @@ -103,15 +103,15 @@ func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
}

if eq.rows == nil {
return nil, fmt.Errorf("Query '%s' with args %+v, must return a database/sql/driver.Rows, but it was not set for expectation %T as %+v", query, args, eq, eq)
return nil, fmt.Errorf("query '%s' with args %+v, must return a database/sql/driver.rows, but it was not set for expectation %T as %+v", query, args, eq, eq)
}

if !eq.queryMatches(query) {
return nil, fmt.Errorf("Query '%s', does not match regex [%s]", query, eq.sqlRegex.String())
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, eq.sqlRegex.String())
}

if !eq.argsMatches(args) {
return nil, fmt.Errorf("Query '%s', args %+v does not match expected %+v", query, args, eq.args)
return nil, fmt.Errorf("query '%s', args %+v does not match expected %+v", query, args, eq.args)
}

return eq.rows, nil
Expand Down
10 changes: 5 additions & 5 deletions expectations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ func TestQueryExpectationArgComparison(t *testing.T) {
against := []driver.Value{5}

if e.argsMatches(against) {
t.Error("Arguments should not match, since the size is not the same")
t.Error("arguments should not match, since the size is not the same")
}

against = []driver.Value{3, "str"}
if e.argsMatches(against) {
t.Error("Arguments should not match, since the first argument (int value) is different")
t.Error("arguments should not match, since the first argument (int value) is different")
}

against = []driver.Value{5, "st"}
if e.argsMatches(against) {
t.Error("Arguments should not match, since the second argument (string value) is different")
t.Error("arguments should not match, since the second argument (string value) is different")
}

against = []driver.Value{5, "str"}
if !e.argsMatches(against) {
t.Error("Arguments should match, but it did not")
t.Error("arguments should match, but it did not")
}

e.args = []driver.Value{5, time.Now()}
Expand All @@ -38,6 +38,6 @@ func TestQueryExpectationArgComparison(t *testing.T) {

against = []driver.Value{5, tm}
if !e.argsMatches(against) {
t.Error("Arguments should match (time will be compared only by type), but it did not")
t.Error("arguments should match (time will be compared only by type), but it did not")
}
}
27 changes: 15 additions & 12 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package sqlmock

import (
"database/sql/driver"
)

// Result satisfies sql driver Result, which
// holds last insert id and rows affected
// by Exec queries
type Result struct {
lastInsertId int64
type result struct {
insertID int64
rowsAffected int64
}

// Creates a new Result for Exec based query mocks
func NewResult(lastInsertId int64, rowsAffected int64) *Result {
return &Result{
lastInsertId,
// NewResult creates a new sql driver Result
// for Exec based query mocks.
func NewResult(lastInsertID int64, rowsAffected int64) driver.Result {
return &result{
lastInsertID,
rowsAffected,
}
}

// Retrieve last inserted id
func (res *Result) LastInsertId() (int64, error) {
return res.lastInsertId, nil
func (r *result) LastInsertId() (int64, error) {
return r.insertID, nil
}

// Retrieve number rows affected
func (res *Result) RowsAffected() (int64, error) {
return res.rowsAffected, nil
func (r *result) RowsAffected() (int64, error) {
return r.rowsAffected, nil
}
4 changes: 2 additions & 2 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (r *rows) Next(dest []driver.Value) error {
return nil
}

// Create Rows from CSV string
// to be used for mocked queries. Satisfies sql driver Rows interface
// RowsFromCSVString creates Rows from CSV string
// to be used for mocked queries. Returns sql driver Rows interface
func RowsFromCSVString(columns []string, s string) driver.Rows {
rs := &rows{}
rs.cols = columns
Expand Down
24 changes: 12 additions & 12 deletions sqlmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,37 @@ func init() {
sql.Register("mock", mock)
}

// Expect transaction to be started
// ExpectBegin expects transaction to be started
func ExpectBegin() Mock {
e := &expectedBegin{}
mock.conn.expectations = append(mock.conn.expectations, e)
mock.conn.active = e
return mock.conn
}

// Expect transaction to be commited
// ExpectCommit expects transaction to be commited
func ExpectCommit() Mock {
e := &expectedCommit{}
mock.conn.expectations = append(mock.conn.expectations, e)
mock.conn.active = e
return mock.conn
}

// Expect transaction to be rolled back
// ExpectRollback expects transaction to be rolled back
func ExpectRollback() Mock {
e := &expectedRollback{}
mock.conn.expectations = append(mock.conn.expectations, e)
mock.conn.active = e
return mock.conn
}

// The expectation will return an error
// WillReturnError the expectation will return an error
func (c *conn) WillReturnError(err error) Mock {
c.active.setError(err)
return c
}

// Expect database Exec to be triggered, which will match
// ExpectExec expects database Exec to be triggered, which will match
// the given query string as a regular expression
func ExpectExec(sqlRegexStr string) Mock {
e := &expectedExec{}
Expand All @@ -125,7 +125,7 @@ func ExpectExec(sqlRegexStr string) Mock {
return mock.conn
}

// Expect database Query to be triggered, which will match
// ExpectQuery database Query to be triggered, which will match
// the given query string as a regular expression
func ExpectQuery(sqlRegexStr string) Mock {
e := &expectedQuery{}
Expand All @@ -136,14 +136,14 @@ func ExpectQuery(sqlRegexStr string) Mock {
return mock.conn
}

// The expectation should be called with given arguments.
// WithArgs expectation should be called with given arguments.
// Works with Exec and Query expectations
func (c *conn) WithArgs(args ...driver.Value) Mock {
eq, ok := c.active.(*expectedQuery)
if !ok {
ee, ok := c.active.(*expectedExec)
if !ok {
panic(fmt.Sprintf("Arguments may be expected only with query based expectations, current is %T", c.active))
panic(fmt.Sprintf("arguments may be expected only with query based expectations, current is %T", c.active))
}
ee.args = args
} else {
Expand All @@ -152,23 +152,23 @@ func (c *conn) WithArgs(args ...driver.Value) Mock {
return c
}

// The expectation will return a Result.
// WillReturnResult expectation will return a Result.
// Works only with Exec expectations
func (c *conn) WillReturnResult(result driver.Result) Mock {
eq, ok := c.active.(*expectedExec)
if !ok {
panic(fmt.Sprintf("driver.Result may be returned only by Exec expectations, current is %T", c.active))
panic(fmt.Sprintf("driver.result may be returned only by exec expectations, current is %T", c.active))
}
eq.result = result
return c
}

// The expectation will return Rows.
// WillReturnRows expectation will return Rows.
// Works only with Query expectations
func (c *conn) WillReturnRows(rows driver.Rows) Mock {
eq, ok := c.active.(*expectedQuery)
if !ok {
panic(fmt.Sprintf("driver.Rows may be returned only by Query expectations, current is %T", c.active))
panic(fmt.Sprintf("driver.rows may be returned only by query expectations, current is %T", c.active))
}
eq.rows = rows
return c
Expand Down
Loading

0 comments on commit c33b881

Please sign in to comment.