Skip to content

Commit c33b881

Browse files
committed
documentation update based on lint
1 parent b89aa3a commit c33b881

8 files changed

+100
-97
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[![Build Status](https://travis-ci.org/DATA-DOG/go-sqlmock.png)](https://travis-ci.org/DATA-DOG/go-sqlmock)
2+
[![GoDoc](https://godoc.org/github.com/DATA-DOG/go-sqlmock?status.png)](https://godoc.org/github.com/DATA-DOG/go-sqlmock)
23

34
# Sql driver mock for Golang
45

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

910
## Install
1011

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

290291
## Documentation
291292

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

294295
## TODO
295296

connection.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ type conn struct {
1010
active expectation
1111
}
1212

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

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

5959
eq, ok := e.(*expectedExec)
6060
if !ok {
61-
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)
61+
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)
6262
}
6363

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

6969
if eq.result == nil {
70-
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)
70+
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)
7171
}
7272

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

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

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

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

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

105105
if eq.rows == nil {
106-
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)
106+
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)
107107
}
108108

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

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

117117
return eq.rows, nil

expectations_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ func TestQueryExpectationArgComparison(t *testing.T) {
1313
against := []driver.Value{5}
1414

1515
if e.argsMatches(against) {
16-
t.Error("Arguments should not match, since the size is not the same")
16+
t.Error("arguments should not match, since the size is not the same")
1717
}
1818

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

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

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

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

3939
against = []driver.Value{5, tm}
4040
if !e.argsMatches(against) {
41-
t.Error("Arguments should match (time will be compared only by type), but it did not")
41+
t.Error("arguments should match (time will be compared only by type), but it did not")
4242
}
4343
}

result.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
package sqlmock
22

3+
import (
4+
"database/sql/driver"
5+
)
6+
37
// Result satisfies sql driver Result, which
48
// holds last insert id and rows affected
59
// by Exec queries
6-
type Result struct {
7-
lastInsertId int64
10+
type result struct {
11+
insertID int64
812
rowsAffected int64
913
}
1014

11-
// Creates a new Result for Exec based query mocks
12-
func NewResult(lastInsertId int64, rowsAffected int64) *Result {
13-
return &Result{
14-
lastInsertId,
15+
// NewResult creates a new sql driver Result
16+
// for Exec based query mocks.
17+
func NewResult(lastInsertID int64, rowsAffected int64) driver.Result {
18+
return &result{
19+
lastInsertID,
1520
rowsAffected,
1621
}
1722
}
1823

19-
// Retrieve last inserted id
20-
func (res *Result) LastInsertId() (int64, error) {
21-
return res.lastInsertId, nil
24+
func (r *result) LastInsertId() (int64, error) {
25+
return r.insertID, nil
2226
}
2327

24-
// Retrieve number rows affected
25-
func (res *Result) RowsAffected() (int64, error) {
26-
return res.rowsAffected, nil
28+
func (r *result) RowsAffected() (int64, error) {
29+
return r.rowsAffected, nil
2730
}

rows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func (r *rows) Next(dest []driver.Value) error {
4040
return nil
4141
}
4242

43-
// Create Rows from CSV string
44-
// to be used for mocked queries. Satisfies sql driver Rows interface
43+
// RowsFromCSVString creates Rows from CSV string
44+
// to be used for mocked queries. Returns sql driver Rows interface
4545
func RowsFromCSVString(columns []string, s string) driver.Rows {
4646
rs := &rows{}
4747
rs.cols = columns

sqlmock.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,37 +85,37 @@ func init() {
8585
sql.Register("mock", mock)
8686
}
8787

88-
// Expect transaction to be started
88+
// ExpectBegin expects transaction to be started
8989
func ExpectBegin() Mock {
9090
e := &expectedBegin{}
9191
mock.conn.expectations = append(mock.conn.expectations, e)
9292
mock.conn.active = e
9393
return mock.conn
9494
}
9595

96-
// Expect transaction to be commited
96+
// ExpectCommit expects transaction to be commited
9797
func ExpectCommit() Mock {
9898
e := &expectedCommit{}
9999
mock.conn.expectations = append(mock.conn.expectations, e)
100100
mock.conn.active = e
101101
return mock.conn
102102
}
103103

104-
// Expect transaction to be rolled back
104+
// ExpectRollback expects transaction to be rolled back
105105
func ExpectRollback() Mock {
106106
e := &expectedRollback{}
107107
mock.conn.expectations = append(mock.conn.expectations, e)
108108
mock.conn.active = e
109109
return mock.conn
110110
}
111111

112-
// The expectation will return an error
112+
// WillReturnError the expectation will return an error
113113
func (c *conn) WillReturnError(err error) Mock {
114114
c.active.setError(err)
115115
return c
116116
}
117117

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

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

139-
// The expectation should be called with given arguments.
139+
// WithArgs expectation should be called with given arguments.
140140
// Works with Exec and Query expectations
141141
func (c *conn) WithArgs(args ...driver.Value) Mock {
142142
eq, ok := c.active.(*expectedQuery)
143143
if !ok {
144144
ee, ok := c.active.(*expectedExec)
145145
if !ok {
146-
panic(fmt.Sprintf("Arguments may be expected only with query based expectations, current is %T", c.active))
146+
panic(fmt.Sprintf("arguments may be expected only with query based expectations, current is %T", c.active))
147147
}
148148
ee.args = args
149149
} else {
@@ -152,23 +152,23 @@ func (c *conn) WithArgs(args ...driver.Value) Mock {
152152
return c
153153
}
154154

155-
// The expectation will return a Result.
155+
// WillReturnResult expectation will return a Result.
156156
// Works only with Exec expectations
157157
func (c *conn) WillReturnResult(result driver.Result) Mock {
158158
eq, ok := c.active.(*expectedExec)
159159
if !ok {
160-
panic(fmt.Sprintf("driver.Result may be returned only by Exec expectations, current is %T", c.active))
160+
panic(fmt.Sprintf("driver.result may be returned only by exec expectations, current is %T", c.active))
161161
}
162162
eq.result = result
163163
return c
164164
}
165165

166-
// The expectation will return Rows.
166+
// WillReturnRows expectation will return Rows.
167167
// Works only with Query expectations
168168
func (c *conn) WillReturnRows(rows driver.Rows) Mock {
169169
eq, ok := c.active.(*expectedQuery)
170170
if !ok {
171-
panic(fmt.Sprintf("driver.Rows may be returned only by Query expectations, current is %T", c.active))
171+
panic(fmt.Sprintf("driver.rows may be returned only by query expectations, current is %T", c.active))
172172
}
173173
eq.rows = rows
174174
return c

0 commit comments

Comments
 (0)