go-sqlmock
go-sqlmock copied to clipboard
feature: Add WithTxOptions() to ExpectBegin
trafficstars
It would be really handy if sqlmock.ExpectBegin() allowed the user to verify the options passed to BeginTx().
Proposal
- add
txOptions *TxOptionsfield toExpectedBegin: https://github.com/DATA-DOG/go-sqlmock/blob/3476f31d8fc4d652040f8d534380f5b54a8d3a42/expectations.go#L54 - set
ExpectedBegin.txOptionsin 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)