Last active
March 8, 2019 05:39
-
-
Save draveness/f7e27dc892f8149dcf91e0b2c3f904d4 to your computer and use it in GitHub Desktop.
SQL Mock in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
_ "github.com/go-sql-driver/mysql" | |
) | |
func recordStats(db *sql.DB, userID, productID int64) (err error) { | |
tx, err := db.Begin() | |
if err != nil { | |
return | |
} | |
defer func() { | |
switch err { | |
case nil: | |
err = tx.Commit() | |
default: | |
tx.Rollback() | |
} | |
}() | |
if _, err = tx.Exec("UPDATE products SET views = views + 1"); err != nil { | |
return | |
} | |
if _, err = tx.Exec("INSERT INTO product_viewers (user_id, product_id) VALUES (?, ?)", userID, productID); err != nil { | |
return | |
} | |
return | |
} | |
func main() { | |
// @NOTE: the real connection is not required for tests | |
db, err := sql.Open("mysql", "root@/blog") | |
if err != nil { | |
panic(err) | |
} | |
defer db.Close() | |
if err = recordStats(db, 1 /*some user id*/, 5 /*some product id*/); err != nil { | |
panic(err) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"testing" | |
"github.com/DATA-DOG/go-sqlmock" | |
) | |
// a successful case | |
func TestShouldUpdateStats(t *testing.T) { | |
db, mock, err := sqlmock.New() | |
if err != nil { | |
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | |
} | |
defer db.Close() | |
mock.ExpectBegin() | |
mock.ExpectExec("UPDATE products").WillReturnResult(sqlmock.NewResult(1, 1)) | |
mock.ExpectExec("INSERT INTO product_viewers").WithArgs(2, 3).WillReturnResult(sqlmock.NewResult(1, 1)) | |
mock.ExpectCommit() | |
// now we execute our method | |
if err = recordStats(db, 2, 3); err != nil { | |
t.Errorf("error was not expected while updating stats: %s", err) | |
} | |
// we make sure that all expectations were met | |
if err := mock.ExpectationsWereMet(); err != nil { | |
t.Errorf("there were unfulfilled expectations: %s", err) | |
} | |
} | |
// a failing test case | |
func TestShouldRollbackStatUpdatesOnFailure(t *testing.T) { | |
db, mock, err := sqlmock.New() | |
if err != nil { | |
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | |
} | |
defer db.Close() | |
mock.ExpectBegin() | |
mock.ExpectExec("UPDATE products").WillReturnResult(sqlmock.NewResult(1, 1)) | |
mock.ExpectExec("INSERT INTO product_viewers"). | |
WithArgs(2, 3). | |
WillReturnError(fmt.Errorf("some error")) | |
mock.ExpectRollback() | |
// now we execute our method | |
if err = recordStats(db, 2, 3); err == nil { | |
t.Errorf("was expecting an error, but there was none") | |
} | |
// we make sure that all expectations were met | |
if err := mock.ExpectationsWereMet(); err != nil { | |
t.Errorf("there were unfulfilled expectations: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/DATA-DOG/go-sqlmock