go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

feature: Add WithTxOptions() to ExpectBegin

Open smoynes opened this issue 2 years ago • 0 comments
trafficstars

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

Proposal

  • add txOptions *TxOptions field to ExpectedBegin: https://github.com/DATA-DOG/go-sqlmock/blob/3476f31d8fc4d652040f8d534380f5b54a8d3a42/expectations.go#L54
  • set ExpectedBegin.txOptions in new method (*ExpectedBegin).WithTxOptions(sql.TxOptions)
  • verify expectation in BeginTx(): https://github.com/DATA-DOG/go-sqlmock/blob/3476f31d8fc4d652040f8d534380f5b54a8d3a42/sqlmock_go18.go#L68

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)

smoynes avatar May 16 '23 14:05 smoynes