Skip to content

Commit 18ab7ac

Browse files
committed
improves error messages, closes #77
1 parent a32ff1c commit 18ab7ac

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

expectations.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery {
153153

154154
// String returns string representation
155155
func (e *ExpectedQuery) String() string {
156-
msg := "ExpectedQuery => expecting Query or QueryRow which:"
156+
msg := "ExpectedQuery => expecting Query, QueryContext or QueryRow which:"
157157
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
158158

159159
if len(e.args) == 0 {
@@ -208,7 +208,7 @@ func (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec {
208208

209209
// String returns string representation
210210
func (e *ExpectedExec) String() string {
211-
msg := "ExpectedExec => expecting Exec which:"
211+
msg := "ExpectedExec => expecting Exec or ExecContext which:"
212212
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
213213

214214
if len(e.args) == 0 {

result_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func ExampleNewResult() {
2323
result := NewResult(lastInsertID, affected)
2424
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
2525
fmt.Println(mock.ExpectationsWereMet())
26-
// Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec which:
26+
// Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec or ExecContext which:
2727
// - matches sql: '^INSERT (.+)'
2828
// - is without arguments
2929
// - should return Result having:

rows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ func (rs *rowSets) Next(dest []driver.Value) error {
4949

5050
// transforms to debuggable printable string
5151
func (rs *rowSets) String() string {
52+
if rs.empty() {
53+
return "with empty rows"
54+
}
55+
5256
msg := "should return rows:\n"
5357
if len(rs.sets) == 1 {
5458
for n, row := range rs.sets[0].rows {
@@ -65,6 +69,15 @@ func (rs *rowSets) String() string {
6569
return strings.TrimSpace(msg)
6670
}
6771

72+
func (rs *rowSets) empty() bool {
73+
for _, set := range rs.sets {
74+
if len(set.rows) > 0 {
75+
return false
76+
}
77+
}
78+
return true
79+
}
80+
6881
// Rows is a mocked collection of rows to
6982
// return for Query result
7083
type Rows struct {

rows_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,23 @@ func TestWrongNumberOfValues(t *testing.T) {
263263
// shouldn't reach here
264264
t.Error("expected panic from query")
265265
}
266+
267+
func TestEmptyRowSets(t *testing.T) {
268+
rs1 := NewRows([]string{"a"}).AddRow("a")
269+
rs2 := NewRows([]string{"b"})
270+
rs3 := NewRows([]string{"c"})
271+
272+
set1 := &rowSets{sets: []*Rows{rs1, rs2}}
273+
set2 := &rowSets{sets: []*Rows{rs3, rs2}}
274+
set3 := &rowSets{sets: []*Rows{rs2}}
275+
276+
if set1.empty() {
277+
t.Fatalf("expected rowset 1, not to be empty, but it was")
278+
}
279+
if !set2.empty() {
280+
t.Fatalf("expected rowset 2, to be empty, but it was not")
281+
}
282+
if !set3.empty() {
283+
t.Fatalf("expected rowset 3, to be empty, but it was not")
284+
}
285+
}

sqlmock.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
247247
break
248248
}
249249
next.Unlock()
250-
return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
250+
return nil, fmt.Errorf("call to ExecQuery '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
251251
}
252252
if exec, ok := next.(*ExpectedExec); ok {
253253
if err := exec.attemptMatch(query, args); err == nil {
@@ -258,7 +258,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
258258
next.Unlock()
259259
}
260260
if expected == nil {
261-
msg := "call to exec '%s' query with args %+v was not expected"
261+
msg := "call to ExecQuery '%s' with args %+v was not expected"
262262
if fulfilled == len(c.expected) {
263263
msg = "all expectations were already fulfilled, " + msg
264264
}
@@ -267,11 +267,11 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
267267
defer expected.Unlock()
268268

269269
if !expected.queryMatches(query) {
270-
return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, expected.sqlRegex.String())
270+
return nil, fmt.Errorf("ExecQuery '%s', does not match regex '%s'", query, expected.sqlRegex.String())
271271
}
272272

273273
if err := expected.argsMatches(args); err != nil {
274-
return nil, fmt.Errorf("exec query '%s', arguments do not match: %s", query, err)
274+
return nil, fmt.Errorf("ExecQuery '%s', arguments do not match: %s", query, err)
275275
}
276276

277277
expected.triggered = true
@@ -280,7 +280,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
280280
}
281281

282282
if expected.result == nil {
283-
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, expected, expected)
283+
return nil, fmt.Errorf("ExecQuery '%s' with args %+v, must return a database/sql/driver.Result, but it was not set for expectation %T as %+v", query, args, expected, expected)
284284
}
285285

286286
return expected, nil
@@ -337,7 +337,7 @@ func (c *sqlmock) prepare(query string) (*ExpectedPrepare, error) {
337337
}
338338
defer expected.Unlock()
339339
if !expected.sqlRegex.MatchString(query) {
340-
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
340+
return nil, fmt.Errorf("Prepare query string '%s', does not match regex [%s]", query, expected.sqlRegex.String())
341341
}
342342

343343
expected.triggered = true
@@ -394,7 +394,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
394394
break
395395
}
396396
next.Unlock()
397-
return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
397+
return nil, fmt.Errorf("call to Query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
398398
}
399399
if qr, ok := next.(*ExpectedQuery); ok {
400400
if err := qr.attemptMatch(query, args); err == nil {
@@ -406,7 +406,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
406406
}
407407

408408
if expected == nil {
409-
msg := "call to query '%s' with args %+v was not expected"
409+
msg := "call to Query '%s' with args %+v was not expected"
410410
if fulfilled == len(c.expected) {
411411
msg = "all expectations were already fulfilled, " + msg
412412
}
@@ -416,11 +416,11 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
416416
defer expected.Unlock()
417417

418418
if !expected.queryMatches(query) {
419-
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
419+
return nil, fmt.Errorf("Query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
420420
}
421421

422422
if err := expected.argsMatches(args); err != nil {
423-
return nil, fmt.Errorf("exec query '%s', arguments do not match: %s", query, err)
423+
return nil, fmt.Errorf("Query '%s', arguments do not match: %s", query, err)
424424
}
425425

426426
expected.triggered = true
@@ -429,7 +429,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
429429
}
430430

431431
if expected.rows == nil {
432-
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, expected, expected)
432+
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, expected, expected)
433433
}
434434
return expected, nil
435435
}
@@ -473,11 +473,11 @@ func (c *sqlmock) Commit() error {
473473

474474
next.Unlock()
475475
if c.ordered {
476-
return fmt.Errorf("call to commit transaction, was not expected, next expectation is: %s", next)
476+
return fmt.Errorf("call to Commit transaction, was not expected, next expectation is: %s", next)
477477
}
478478
}
479479
if expected == nil {
480-
msg := "call to commit transaction was not expected"
480+
msg := "call to Commit transaction was not expected"
481481
if fulfilled == len(c.expected) {
482482
msg = "all expectations were already fulfilled, " + msg
483483
}
@@ -508,11 +508,11 @@ func (c *sqlmock) Rollback() error {
508508

509509
next.Unlock()
510510
if c.ordered {
511-
return fmt.Errorf("call to rollback transaction, was not expected, next expectation is: %s", next)
511+
return fmt.Errorf("call to Rollback transaction, was not expected, next expectation is: %s", next)
512512
}
513513
}
514514
if expected == nil {
515-
msg := "call to rollback transaction was not expected"
515+
msg := "call to Rollback transaction was not expected"
516516
if fulfilled == len(c.expected) {
517517
msg = "all expectations were already fulfilled, " + msg
518518
}

0 commit comments

Comments
 (0)