go-sqlmock
go-sqlmock copied to clipboard
Add WithTXOption expectation to ExpectBegin
trafficstars
closes #316
func TestContextBeginWithTxOptions(t *testing.T) {
t.Parallel()
db, mock, err := New()
if err != nil {
t.Errorf("an error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()
mock.ExpectBegin().WithTxOptions(sql.TxOptions{
Isolation: sql.LevelReadCommitted,
ReadOnly: true,
})
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(time.Millisecond * 10)
cancel()
}()
_, err = db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted, ReadOnly: false})
if err != nil {
t.Errorf("error was not expected, but got: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}