Skip to content

Commit

Permalink
asserts ordinal argument position, fixes expected query error message
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed Feb 16, 2017
1 parent 53b2cd1 commit a00b6aa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 1 addition & 8 deletions expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,7 @@ func (e *ExpectedQuery) String() string {
}

if e.rows != nil {
msg += "\n - should return rows:\n"
rs, _ := e.rows.(*rowSets)
for _, set := range rs.sets {
for i, row := range set.rows {
msg += fmt.Sprintf(" %d - %+v\n", i, row)
}
}
msg = strings.TrimSpace(msg)
msg += fmt.Sprintf("\n - %s", e.rows)
}

if e.err != nil {
Expand Down
3 changes: 2 additions & 1 deletion expectations_go18.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
// custom argument matcher
matcher, ok := e.args[k].(Argument)
if ok {
// @TODO: does it make sense to pass value instead of named value?
if !matcher.Match(v.Value) {
return fmt.Errorf("matcher %T could not match %d argument %T - %+v", matcher, k, args[k], args[k])
}
Expand All @@ -45,6 +44,8 @@ func (e *queryBasedExpectation) argsMatches(args []namedValue) error {
if v.Name != named.Name {
return fmt.Errorf("named argument %d: name: \"%s\" does not match expected: \"%s\"", k, v.Name, named.Name)
}
} else if k+1 != v.Ordinal {
return fmt.Errorf("argument %d: ordinal position: %d does not match expected: %d", k, k+1, v.Ordinal)
}

// convert to driver converter
Expand Down
File renamed without changes.
19 changes: 19 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlmock
import (
"database/sql/driver"
"encoding/csv"
"fmt"
"io"
"strings"
)
Expand Down Expand Up @@ -46,6 +47,24 @@ func (rs *rowSets) Next(dest []driver.Value) error {
return r.nextErr[r.pos-1]
}

// transforms to debuggable printable string
func (rs *rowSets) String() string {
msg := "should return rows:\n"
if len(rs.sets) == 1 {
for n, row := range rs.sets[0].rows {
msg += fmt.Sprintf(" row %d - %+v\n", n, row)
}
return strings.TrimSpace(msg)
}
for i, set := range rs.sets {
msg += fmt.Sprintf(" result set: %d\n", i)
for n, row := range set.rows {
msg += fmt.Sprintf(" row %d - %+v\n", n, row)
}
}
return strings.TrimSpace(msg)
}

// Rows is a mocked collection of rows to
// return for Query result
type Rows struct {
Expand Down

0 comments on commit a00b6aa

Please sign in to comment.