1
0
mirror of https://github.com/zhashkevych/go-sqlxmock.git synced 2024-11-24 08:12:13 +02:00

Merge pull request #58 from gliptak/commitclose1

Additional tests for Close
This commit is contained in:
Gediminas Morkevicius 2016-11-27 21:11:37 +02:00 committed by GitHub
commit 8e610725b3

View File

@ -806,7 +806,7 @@ func TestRollbackThrow(t *testing.T) {
// ensure all expectations have been met
if err = mock.ExpectationsWereMet(); err != nil {
fmt.Printf("unmet expectation error: %s", err)
t.Errorf("unmet expectation error: %s", err)
}
// Output:
}
@ -833,13 +833,26 @@ func TestUnexpectedCommitOrder(t *testing.T) {
return
}
mock.ExpectBegin()
mock.ExpectRollback()
mock.ExpectRollback().WillReturnError(fmt.Errorf("Rollback failed"))
tx, _ := db.Begin()
if err := tx.Commit(); err == nil {
t.Error("an error was expected when calling commit, but got none")
}
}
func TestExpectedCommitOrder(t *testing.T) {
// Open new mock database
db, mock, err := New()
if err != nil {
fmt.Println("error creating mock database")
return
}
mock.ExpectCommit().WillReturnError(fmt.Errorf("Commit failed"))
if _, err := db.Begin(); err == nil {
t.Error("an error was expected when calling begin, but got none")
}
}
func TestUnexpectedRollback(t *testing.T) {
// Open new mock database
db, mock, err := New()
@ -938,4 +951,48 @@ func TestPrepareQuery(t *testing.T) {
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func TestExpectedCloseError(t *testing.T) {
// Open new mock database
db, mock, err := New()
if err != nil {
fmt.Println("error creating mock database")
return
}
mock.ExpectClose().WillReturnError(fmt.Errorf("Close failed"))
if err := db.Close(); err == nil {
t.Error("an error was expected when calling close, but got none")
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expections: %s", err)
}
}
func TestExpectedCloseOrder(t *testing.T) {
// Open new mock database
db, mock, err := New()
if err != nil {
fmt.Println("error creating mock database")
return
}
defer db.Close()
mock.ExpectClose().WillReturnError(fmt.Errorf("Close failed"))
db.Begin()
if err := mock.ExpectationsWereMet(); err == nil {
t.Error("expected error on ExpectationsWereMet")
}
}
func TestExpectedBeginOrder(t *testing.T) {
// Open new mock database
db, mock, err := New()
if err != nil {
fmt.Println("error creating mock database")
return
}
mock.ExpectBegin().WillReturnError(fmt.Errorf("Begin failed"))
if err := db.Close(); err == nil {
t.Error("an error was expected when calling close, but got none")
}
}