mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-03-25 21:18:08 +02:00
improves error messages, closes #77
This commit is contained in:
parent
a32ff1cda1
commit
18ab7ac1c7
@ -153,7 +153,7 @@ func (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery {
|
||||
|
||||
// String returns string representation
|
||||
func (e *ExpectedQuery) String() string {
|
||||
msg := "ExpectedQuery => expecting Query or QueryRow which:"
|
||||
msg := "ExpectedQuery => expecting Query, QueryContext or QueryRow which:"
|
||||
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
|
||||
|
||||
if len(e.args) == 0 {
|
||||
@ -208,7 +208,7 @@ func (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec {
|
||||
|
||||
// String returns string representation
|
||||
func (e *ExpectedExec) String() string {
|
||||
msg := "ExpectedExec => expecting Exec which:"
|
||||
msg := "ExpectedExec => expecting Exec or ExecContext which:"
|
||||
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
|
||||
|
||||
if len(e.args) == 0 {
|
||||
|
@ -23,7 +23,7 @@ func ExampleNewResult() {
|
||||
result := NewResult(lastInsertID, affected)
|
||||
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
|
||||
fmt.Println(mock.ExpectationsWereMet())
|
||||
// Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec which:
|
||||
// Output: there is a remaining expectation which was not matched: ExpectedExec => expecting Exec or ExecContext which:
|
||||
// - matches sql: '^INSERT (.+)'
|
||||
// - is without arguments
|
||||
// - should return Result having:
|
||||
|
13
rows.go
13
rows.go
@ -49,6 +49,10 @@ func (rs *rowSets) Next(dest []driver.Value) error {
|
||||
|
||||
// transforms to debuggable printable string
|
||||
func (rs *rowSets) String() string {
|
||||
if rs.empty() {
|
||||
return "with empty rows"
|
||||
}
|
||||
|
||||
msg := "should return rows:\n"
|
||||
if len(rs.sets) == 1 {
|
||||
for n, row := range rs.sets[0].rows {
|
||||
@ -65,6 +69,15 @@ func (rs *rowSets) String() string {
|
||||
return strings.TrimSpace(msg)
|
||||
}
|
||||
|
||||
func (rs *rowSets) empty() bool {
|
||||
for _, set := range rs.sets {
|
||||
if len(set.rows) > 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Rows is a mocked collection of rows to
|
||||
// return for Query result
|
||||
type Rows struct {
|
||||
|
20
rows_test.go
20
rows_test.go
@ -263,3 +263,23 @@ func TestWrongNumberOfValues(t *testing.T) {
|
||||
// shouldn't reach here
|
||||
t.Error("expected panic from query")
|
||||
}
|
||||
|
||||
func TestEmptyRowSets(t *testing.T) {
|
||||
rs1 := NewRows([]string{"a"}).AddRow("a")
|
||||
rs2 := NewRows([]string{"b"})
|
||||
rs3 := NewRows([]string{"c"})
|
||||
|
||||
set1 := &rowSets{sets: []*Rows{rs1, rs2}}
|
||||
set2 := &rowSets{sets: []*Rows{rs3, rs2}}
|
||||
set3 := &rowSets{sets: []*Rows{rs2}}
|
||||
|
||||
if set1.empty() {
|
||||
t.Fatalf("expected rowset 1, not to be empty, but it was")
|
||||
}
|
||||
if !set2.empty() {
|
||||
t.Fatalf("expected rowset 2, to be empty, but it was not")
|
||||
}
|
||||
if !set3.empty() {
|
||||
t.Fatalf("expected rowset 3, to be empty, but it was not")
|
||||
}
|
||||
}
|
||||
|
30
sqlmock.go
30
sqlmock.go
@ -247,7 +247,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
||||
break
|
||||
}
|
||||
next.Unlock()
|
||||
return nil, fmt.Errorf("call to exec query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
|
||||
return nil, fmt.Errorf("call to ExecQuery '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
|
||||
}
|
||||
if exec, ok := next.(*ExpectedExec); ok {
|
||||
if err := exec.attemptMatch(query, args); err == nil {
|
||||
@ -258,7 +258,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
||||
next.Unlock()
|
||||
}
|
||||
if expected == nil {
|
||||
msg := "call to exec '%s' query with args %+v was not expected"
|
||||
msg := "call to ExecQuery '%s' with args %+v was not expected"
|
||||
if fulfilled == len(c.expected) {
|
||||
msg = "all expectations were already fulfilled, " + msg
|
||||
}
|
||||
@ -267,11 +267,11 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
||||
defer expected.Unlock()
|
||||
|
||||
if !expected.queryMatches(query) {
|
||||
return nil, fmt.Errorf("exec query '%s', does not match regex '%s'", query, expected.sqlRegex.String())
|
||||
return nil, fmt.Errorf("ExecQuery '%s', does not match regex '%s'", query, expected.sqlRegex.String())
|
||||
}
|
||||
|
||||
if err := expected.argsMatches(args); err != nil {
|
||||
return nil, fmt.Errorf("exec query '%s', arguments do not match: %s", query, err)
|
||||
return nil, fmt.Errorf("ExecQuery '%s', arguments do not match: %s", query, err)
|
||||
}
|
||||
|
||||
expected.triggered = true
|
||||
@ -280,7 +280,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
||||
}
|
||||
|
||||
if expected.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, expected, expected)
|
||||
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)
|
||||
}
|
||||
|
||||
return expected, nil
|
||||
@ -337,7 +337,7 @@ func (c *sqlmock) prepare(query string) (*ExpectedPrepare, error) {
|
||||
}
|
||||
defer expected.Unlock()
|
||||
if !expected.sqlRegex.MatchString(query) {
|
||||
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
|
||||
return nil, fmt.Errorf("Prepare query string '%s', does not match regex [%s]", query, expected.sqlRegex.String())
|
||||
}
|
||||
|
||||
expected.triggered = true
|
||||
@ -394,7 +394,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
||||
break
|
||||
}
|
||||
next.Unlock()
|
||||
return nil, fmt.Errorf("call to query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
|
||||
return nil, fmt.Errorf("call to Query '%s' with args %+v, was not expected, next expectation is: %s", query, args, next)
|
||||
}
|
||||
if qr, ok := next.(*ExpectedQuery); ok {
|
||||
if err := qr.attemptMatch(query, args); err == nil {
|
||||
@ -406,7 +406,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
||||
}
|
||||
|
||||
if expected == nil {
|
||||
msg := "call to query '%s' with args %+v was not expected"
|
||||
msg := "call to Query '%s' with args %+v was not expected"
|
||||
if fulfilled == len(c.expected) {
|
||||
msg = "all expectations were already fulfilled, " + msg
|
||||
}
|
||||
@ -416,11 +416,11 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
||||
defer expected.Unlock()
|
||||
|
||||
if !expected.queryMatches(query) {
|
||||
return nil, fmt.Errorf("query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
|
||||
return nil, fmt.Errorf("Query '%s', does not match regex [%s]", query, expected.sqlRegex.String())
|
||||
}
|
||||
|
||||
if err := expected.argsMatches(args); err != nil {
|
||||
return nil, fmt.Errorf("exec query '%s', arguments do not match: %s", query, err)
|
||||
return nil, fmt.Errorf("Query '%s', arguments do not match: %s", query, err)
|
||||
}
|
||||
|
||||
expected.triggered = true
|
||||
@ -429,7 +429,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
||||
}
|
||||
|
||||
if expected.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, expected, expected)
|
||||
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)
|
||||
}
|
||||
return expected, nil
|
||||
}
|
||||
@ -473,11 +473,11 @@ func (c *sqlmock) Commit() error {
|
||||
|
||||
next.Unlock()
|
||||
if c.ordered {
|
||||
return fmt.Errorf("call to commit transaction, was not expected, next expectation is: %s", next)
|
||||
return fmt.Errorf("call to Commit transaction, was not expected, next expectation is: %s", next)
|
||||
}
|
||||
}
|
||||
if expected == nil {
|
||||
msg := "call to commit transaction was not expected"
|
||||
msg := "call to Commit transaction was not expected"
|
||||
if fulfilled == len(c.expected) {
|
||||
msg = "all expectations were already fulfilled, " + msg
|
||||
}
|
||||
@ -508,11 +508,11 @@ func (c *sqlmock) Rollback() error {
|
||||
|
||||
next.Unlock()
|
||||
if c.ordered {
|
||||
return fmt.Errorf("call to rollback transaction, was not expected, next expectation is: %s", next)
|
||||
return fmt.Errorf("call to Rollback transaction, was not expected, next expectation is: %s", next)
|
||||
}
|
||||
}
|
||||
if expected == nil {
|
||||
msg := "call to rollback transaction was not expected"
|
||||
msg := "call to Rollback transaction was not expected"
|
||||
if fulfilled == len(c.expected) {
|
||||
msg = "all expectations were already fulfilled, " + msg
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user