Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
/ rowerr Public archive

Provides a mechanism for constructing a *sql.Row rigged to return an error when you call Scan() on it.

Notifications You must be signed in to change notification settings

geckoboard/rowerr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

rowerr

rowerr provides a mechanism for constructing a *sql.Row rigged to return an error when you call Scan() on it.

This is useful for testing error-handling when you're using the QueryRow() and QueryRowContext() methods on *sql.DB and *sql.Tx.

Example

// foo.go
type driver interface {
    QueryRow(string, ...interface{}) *sql.Row
}

func foo(db driver) error {
    var v int
    err := db.QueryRow("SELECT ... FROM ...").Scan(&v)
    if err == sql.ErrNoRows {
        return nil
    }
    return err
}

// foo_test.go
type fakeDriver struct {
    err error
}

func (f fakeDriver) QueryRow(string, ...interface{}) *sql.Row {
    return rowerr.New(f.err)
}

func TestFoo_ErrNoRows(t *testing.T) {
    driver := fakeDriver{err: sql.ErrNoRows}
    if err := foo(driver); err != nil {
        t.Error("expected no error, got: %v", err)
    }
}

License

rowerr is licensed under the MIT License.

About

Provides a mechanism for constructing a *sql.Row rigged to return an error when you call Scan() on it.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages