Skip to content

Commit

Permalink
closes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed Apr 21, 2014
1 parent 2e154ee commit fa31f40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ import (
// will test that order with a different status, cannot be cancelled
func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) {
// open database stub
db, err := sql.Open("mock", "")
db, err := sqlmock.New()
if err != nil {
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestShouldNotCancelOrderWithNonPendingStatus(t *testing.T) {
// will test order cancellation
func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) {
// open database stub
db, err := sql.Open("mock", "")
db, err := sqlmock.New()
if err != nil {
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
}
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestShouldRefundUserWhenOrderIsCancelled(t *testing.T) {
// will test order cancellation
func TestShouldRollbackOnError(t *testing.T) {
// open database stub
db, err := sql.Open("mock", "")
db, err := sqlmock.New()
if err != nil {
t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
}
Expand Down Expand Up @@ -320,6 +320,10 @@ Visit [godoc](http://godoc.org/github.com/DATA-DOG/go-sqlmock)

## Changes

- **2014-04-21** introduce **sqlmock.New()** to open a mock database connection for tests. This method
calls sql.DB.Ping to ensure that connection is open, see [issue](https://github.com/DATA-DOG/go-sqlmock/issues/4).
This way on Close it will surely assert if all expectations are met, even if database was not triggered at all.
The old way is still available, but it is advisable to call db.Ping manually before asserting with db.Close.
- **2014-02-14** RowsFromCSVString is now a part of Rows interface named as FromCSVString.
It has changed to allow more ways to construct rows and to easily extend this API in future.
See [issue 1](https://github.com/DATA-DOG/go-sqlmock/issues/1)
Expand Down
13 changes: 13 additions & 0 deletions sqlmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func init() {
sql.Register("mock", mock)
}

// New creates sqlmock database connection
// and pings it so that all expectations could be
// asserted on Close.
func New() (db *sql.DB, err error) {
db, err = sql.Open("mock", "")
if err != nil {
return
}
// ensure open connection, otherwise Close does not assert expectations
db.Ping()
return
}

// ExpectBegin expects transaction to be started
func ExpectBegin() Mock {
e := &expectedBegin{}
Expand Down
16 changes: 16 additions & 0 deletions sqlmock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"time"
)

// test the case when db is not triggered and expectations
// are not asserted on close
func TestIssue4(t *testing.T) {
db, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
ExpectQuery("some sql query which will not be called").
WillReturnRows(NewRows([]string{"id"}))

err = db.Close()
if err == nil {
t.Errorf("Was expecting an error, since expected query was not matched")
}
}

func TestMockQuery(t *testing.T) {
db, err := sql.Open("mock", "")
if err != nil {
Expand Down

0 comments on commit fa31f40

Please sign in to comment.