You've already forked go-sqlmock
mirror of
https://github.com/DATA-DOG/go-sqlmock.git
synced 2025-06-16 23:57:35 +02:00
improves error messages, closes #77
This commit is contained in:
@ -153,7 +153,7 @@ func (e *ExpectedQuery) WillDelayFor(duration time.Duration) *ExpectedQuery {
|
|||||||
|
|
||||||
// String returns string representation
|
// String returns string representation
|
||||||
func (e *ExpectedQuery) String() string {
|
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() + "'"
|
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
|
||||||
|
|
||||||
if len(e.args) == 0 {
|
if len(e.args) == 0 {
|
||||||
@ -208,7 +208,7 @@ func (e *ExpectedExec) WillDelayFor(duration time.Duration) *ExpectedExec {
|
|||||||
|
|
||||||
// String returns string representation
|
// String returns string representation
|
||||||
func (e *ExpectedExec) String() string {
|
func (e *ExpectedExec) String() string {
|
||||||
msg := "ExpectedExec => expecting Exec which:"
|
msg := "ExpectedExec => expecting Exec or ExecContext which:"
|
||||||
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
|
msg += "\n - matches sql: '" + e.sqlRegex.String() + "'"
|
||||||
|
|
||||||
if len(e.args) == 0 {
|
if len(e.args) == 0 {
|
||||||
|
@ -23,7 +23,7 @@ func ExampleNewResult() {
|
|||||||
result := NewResult(lastInsertID, affected)
|
result := NewResult(lastInsertID, affected)
|
||||||
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
|
mock.ExpectExec("^INSERT (.+)").WillReturnResult(result)
|
||||||
fmt.Println(mock.ExpectationsWereMet())
|
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 (.+)'
|
// - matches sql: '^INSERT (.+)'
|
||||||
// - is without arguments
|
// - is without arguments
|
||||||
// - should return Result having:
|
// - 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
|
// transforms to debuggable printable string
|
||||||
func (rs *rowSets) String() string {
|
func (rs *rowSets) String() string {
|
||||||
|
if rs.empty() {
|
||||||
|
return "with empty rows"
|
||||||
|
}
|
||||||
|
|
||||||
msg := "should return rows:\n"
|
msg := "should return rows:\n"
|
||||||
if len(rs.sets) == 1 {
|
if len(rs.sets) == 1 {
|
||||||
for n, row := range rs.sets[0].rows {
|
for n, row := range rs.sets[0].rows {
|
||||||
@ -65,6 +69,15 @@ func (rs *rowSets) String() string {
|
|||||||
return strings.TrimSpace(msg)
|
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
|
// Rows is a mocked collection of rows to
|
||||||
// return for Query result
|
// return for Query result
|
||||||
type Rows struct {
|
type Rows struct {
|
||||||
|
20
rows_test.go
20
rows_test.go
@ -263,3 +263,23 @@ func TestWrongNumberOfValues(t *testing.T) {
|
|||||||
// shouldn't reach here
|
// shouldn't reach here
|
||||||
t.Error("expected panic from query")
|
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
|
break
|
||||||
}
|
}
|
||||||
next.Unlock()
|
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 exec, ok := next.(*ExpectedExec); ok {
|
||||||
if err := exec.attemptMatch(query, args); err == nil {
|
if err := exec.attemptMatch(query, args); err == nil {
|
||||||
@ -258,7 +258,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
|||||||
next.Unlock()
|
next.Unlock()
|
||||||
}
|
}
|
||||||
if expected == nil {
|
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) {
|
if fulfilled == len(c.expected) {
|
||||||
msg = "all expectations were already fulfilled, " + msg
|
msg = "all expectations were already fulfilled, " + msg
|
||||||
}
|
}
|
||||||
@ -267,11 +267,11 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
|||||||
defer expected.Unlock()
|
defer expected.Unlock()
|
||||||
|
|
||||||
if !expected.queryMatches(query) {
|
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 {
|
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
|
expected.triggered = true
|
||||||
@ -280,7 +280,7 @@ func (c *sqlmock) exec(query string, args []namedValue) (*ExpectedExec, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if expected.result == nil {
|
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
|
return expected, nil
|
||||||
@ -337,7 +337,7 @@ func (c *sqlmock) prepare(query string) (*ExpectedPrepare, error) {
|
|||||||
}
|
}
|
||||||
defer expected.Unlock()
|
defer expected.Unlock()
|
||||||
if !expected.sqlRegex.MatchString(query) {
|
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
|
expected.triggered = true
|
||||||
@ -394,7 +394,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
next.Unlock()
|
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 qr, ok := next.(*ExpectedQuery); ok {
|
||||||
if err := qr.attemptMatch(query, args); err == nil {
|
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 {
|
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) {
|
if fulfilled == len(c.expected) {
|
||||||
msg = "all expectations were already fulfilled, " + msg
|
msg = "all expectations were already fulfilled, " + msg
|
||||||
}
|
}
|
||||||
@ -416,11 +416,11 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
|||||||
defer expected.Unlock()
|
defer expected.Unlock()
|
||||||
|
|
||||||
if !expected.queryMatches(query) {
|
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 {
|
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
|
expected.triggered = true
|
||||||
@ -429,7 +429,7 @@ func (c *sqlmock) query(query string, args []namedValue) (*ExpectedQuery, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if expected.rows == nil {
|
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
|
return expected, nil
|
||||||
}
|
}
|
||||||
@ -473,11 +473,11 @@ func (c *sqlmock) Commit() error {
|
|||||||
|
|
||||||
next.Unlock()
|
next.Unlock()
|
||||||
if c.ordered {
|
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 {
|
if expected == nil {
|
||||||
msg := "call to commit transaction was not expected"
|
msg := "call to Commit transaction was not expected"
|
||||||
if fulfilled == len(c.expected) {
|
if fulfilled == len(c.expected) {
|
||||||
msg = "all expectations were already fulfilled, " + msg
|
msg = "all expectations were already fulfilled, " + msg
|
||||||
}
|
}
|
||||||
@ -508,11 +508,11 @@ func (c *sqlmock) Rollback() error {
|
|||||||
|
|
||||||
next.Unlock()
|
next.Unlock()
|
||||||
if c.ordered {
|
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 {
|
if expected == nil {
|
||||||
msg := "call to rollback transaction was not expected"
|
msg := "call to Rollback transaction was not expected"
|
||||||
if fulfilled == len(c.expected) {
|
if fulfilled == len(c.expected) {
|
||||||
msg = "all expectations were already fulfilled, " + msg
|
msg = "all expectations were already fulfilled, " + msg
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user