DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
5.95k stars 404 forks source link

feature: Add WithTxOptions() to ExpectBegin #316

Open smoynes opened 1 year ago

smoynes commented 1 year ago

It would be really handy if sqlmock.ExpectBegin() allowed the user to verify the options passed to BeginTx().

Proposal

Use-cases

Verifying transaction isolation level:

db, mock, err := sqlmock.New()
defer mock.ExpectationsWereMet()

txOpts := sql.TxOptions{Isolation: sql.LevelReadUncommitted}
mock.ExpectBegin().WithTxOptions(txOpts)

tx, err := db.BeginTx(context.TODO(), &txOpts)

Verifying read-only transactions:

db, mock, err := sqlmock.New()
defer mock.ExpectationsWereMet()

txOpts := sql.TxOptions{ReadOnly: true}
mock.ExpectBegin().WithTxOptions(txOpts)

tx, err := db.BeginTx(context.TODO(), &txOpts)