Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Add WithTxOptions() to ExpectBegin #316

Open
smoynes opened this issue May 16, 2023 · 0 comments
Open

feature: Add WithTxOptions() to ExpectBegin #316

smoynes opened this issue May 16, 2023 · 0 comments

Comments

@smoynes
Copy link

smoynes commented May 16, 2023

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:
    type ExpectedBegin struct {
  • set ExpectedBegin.txOptions in new method (*ExpectedBegin).WithTxOptions(sql.TxOptions)
  • verify expectation in BeginTx():
    func (c *sqlmock) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant