DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.16k stars 407 forks source link

Add WithTXOption expectation to ExpectBegin #340

Closed ninadingole closed 2 months ago

ninadingole commented 3 months ago

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)
    }
}
kpeu3i commented 2 months ago

there is a bug in the current implementation, please check https://github.com/DATA-DOG/go-sqlmock/issues/342