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

Add WithTXOption expectation to ExpectBegin

Open ninadingole opened this issue 1 year ago • 0 comments
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)
	}
}

ninadingole avatar Aug 13 '24 20:08 ninadingole