Skip to content

Commit a00b6aa

Browse files
committed
asserts ordinal argument position, fixes expected query error message
1 parent 53b2cd1 commit a00b6aa

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

expectations.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,7 @@ func (e *ExpectedQuery) String() string {
167167
}
168168

169169
if e.rows != nil {
170-
msg += "\n - should return rows:\n"
171-
rs, _ := e.rows.(*rowSets)
172-
for _, set := range rs.sets {
173-
for i, row := range set.rows {
174-
msg += fmt.Sprintf(" %d - %+v\n", i, row)
175-
}
176-
}
177-
msg = strings.TrimSpace(msg)
170+
msg += fmt.Sprintf("\n - %s", e.rows)
178171
}
179172

180173
if e.err != nil {

expectations_go18.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
3232
// custom argument matcher
3333
matcher, ok := e.args[k].(Argument)
3434
if ok {
35-
// @TODO: does it make sense to pass value instead of named value?
3635
if !matcher.Match(v.Value) {
3736
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k])
3837
}
@@ -45,6 +44,8 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
4544
if v.Name != named.Name {
4645
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name)
4746
}
47+
} else if k+1 != v.Ordinal {
48+
return fmt.Errorf("argument %d: ordinal position: %d does not match expected: %d", k, k+1, v.Ordinal)
4849
}
4950

5051
// convert to driver converter

rows.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sqlmock
33
import (
44
"database/sql/driver"
55
"encoding/csv"
6+
"fmt"
67
"io"
78
"strings"
89
)
@@ -46,6 +47,24 @@ func (rs *rowSets) Next(dest []driver.Value) error {
4647
return r.nextErr[r.pos-1]
4748
}
4849

50+
// transforms to debuggable printable string
51+
func (rs *rowSets) String() string {
52+
msg := "should return rows:\n"
53+
if len(rs.sets) == 1 {
54+
for n, row := range rs.sets[0].rows {
55+
msg += fmt.Sprintf(" row %d - %+v\n", n, row)
56+
}
57+
return strings.TrimSpace(msg)
58+
}
59+
for i, set := range rs.sets {
60+
msg += fmt.Sprintf(" result set: %d\n", i)
61+
for n, row := range set.rows {
62+
msg += fmt.Sprintf(" row %d - %+v\n", n, row)
63+
}
64+
}
65+
return strings.TrimSpace(msg)
66+
}
67+
4968
// Rows is a mocked collection of rows to
5069
// return for Query result
5170
type Rows struct {

0 commit comments

Comments
 (0)