Skip to content

Commit

Permalink
Changed method name as per feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aneshas committed Mar 15, 2024
1 parent 15d996c commit 4fd190a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
test:
@go test -tags="integration" -race -coverprofile=profile.cov -v $(shell go list ./... | grep -vE 'cmd|mocks|testdata|testutil')
@go test -tags="integration" -race -coverprofile=profile.cov -v $(shell go list ./... | grep -vE 'example|mocks|testdata|testutil')
@go tool cover -func=profile.cov | grep total
1 change: 1 addition & 0 deletions example/transfer_money.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package example
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/aneshas/tx
module github.com/aneshas/tx/v2

go 1.21.0

Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type Option func(tx *TX)
// not cause the transaction to be rolled back.
//
// The transaction will still be committed but the actual error will be returned
// by the Do method.
// by the WithTransaction method.
func WithIgnoredErrors(errs ...error) Option {
return func(tx *TX) {
tx.ignoreErrs = append(tx.ignoreErrs, errs...)
Expand Down
2 changes: 1 addition & 1 deletion pgxtxv5/pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestShould_Rollback_Pgx_Transaction(t *testing.T) {
func doPgx(t *testing.T, transactor *tx.TX, name string, fail bool) {
t.Helper()

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
ttx, _ := pgxtxv5.From(ctx)

_, err := ttx.Exec(ctx, `insert into cats (name) values($1)`, name)
Expand Down
2 changes: 1 addition & 1 deletion sqltx/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestShould_Rollback_Sql_Transaction(t *testing.T) {
func doSql(t *testing.T, transactor *tx.TX, name string, fail bool) {
t.Helper()

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
ttx, _ := sqltx.From(ctx)

_, err := ttx.Exec(`insert into cats (name) values($1)`, name)
Expand Down
7 changes: 0 additions & 7 deletions testutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@ func WithUnsuccessfulTransactionStart(with error) Option {
}
}

func WithSuccessfulTransactionStart() Option {
return func(db *DB) {
tx := mocks.NewTransaction(db.t)
db.EXPECT().Begin(mock.Anything).Return(tx, nil).Once()
}
}

func WithSuccessfulCommit() Option {
return func(db *DB) {
tx := mocks.NewTransaction(db.t)
Expand Down
22 changes: 11 additions & 11 deletions testutil/mocks/transactor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import (
type key struct{}

// Transactor is a helper transactor interface added for brevity purposes, so you don't have to define your own
// See TX
type Transactor interface {
Do(ctx context.Context, f func(ctx context.Context) error) error
// WithTransaction will wrap f in a sql transaction depending on the DB provider.
// This is mostly useful for when we want to control the transaction scope from
// application layer, for example application service/command handler.
// If f fails with an error, transactor will automatically try to roll the transaction back and report back any errors,
// otherwise, the implicit transaction will be committed.
WithTransaction(ctx context.Context, f func(ctx context.Context) error) error
}

// DB represents an interface to a db capable of starting a transaction
Expand Down Expand Up @@ -41,12 +47,12 @@ type TX struct {
ignoreErrs []error
}

// Do will execute func f in a sql transaction.
// WithTransaction will wrap f in a sql transaction depending on the DB provider.
// This is mostly useful for when we want to control the transaction scope from
// application layer, for example application service/command handler.
// If f fails with an error, transactor will automatically try to roll back the transaction and report back any errors,
// otherwise the implicit transaction will be committed.
func (t *TX) Do(ctx context.Context, f func(ctx context.Context) error) error {
// If f fails with an error, transactor will automatically try to roll the transaction back and report back any errors,
// otherwise, the implicit transaction will be committed.
func (t *TX) WithTransaction(ctx context.Context, f func(ctx context.Context) error) error {
tx, err := t.db.Begin(ctx) // add tx options if different isolation levels are needed
if err != nil {
return fmt.Errorf("tx: could not start transaction: %w", err)
Expand Down
16 changes: 8 additions & 8 deletions tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestShould_Report_Transaction_Begin_Error(t *testing.T) {
)
transactor := tx.New(db)

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return nil
})

Expand All @@ -33,7 +33,7 @@ func TestShould_Commit_Transaction_On_No_Error(t *testing.T) {
)
transactor := tx.New(db)

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return nil
})

Expand All @@ -49,7 +49,7 @@ func TestShould_Rollback_Transaction_On_Error(t *testing.T) {

wantErr := fmt.Errorf("something bad occurred")

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})

Expand All @@ -67,7 +67,7 @@ func TestShould_Report_Unsuccessful_Rollback(t *testing.T) {

wantErr := fmt.Errorf("process error")

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})

Expand All @@ -88,7 +88,7 @@ func TestShould_Rollback_Transaction_On_Panic_And_RePanic(t *testing.T) {
}
}()

_ = transactor.Do(context.TODO(), func(ctx context.Context) error {
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
panic("something very bad occurred")
})
}
Expand All @@ -102,7 +102,7 @@ func TestShould_Still_Commit_On_Ignored_Error_And_Propagate_Error(t *testing.T)
)
transactor := tx.New(db, tx.WithIgnoredErrors(wantErr))

err := transactor.Do(context.TODO(), func(ctx context.Context) error {
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})

Expand All @@ -116,7 +116,7 @@ func TestShould_Retrieve_Tx_From_Context(t *testing.T) {
)
transactor := tx.New(db)

_ = transactor.Do(context.TODO(), func(ctx context.Context) error {
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
ttx, ok := tx.From[tx.Transaction](ctx)

assert.True(t, ok)
Expand All @@ -133,7 +133,7 @@ func TestShould_Not_Retrieve_Conn_From_Context_On_Mismatched_Type(t *testing.T)
)
transactor := tx.New(db)

_ = transactor.Do(context.TODO(), func(ctx context.Context) error {
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
_, ok := tx.From[mocks.Transaction](ctx)

assert.False(t, ok)
Expand Down

0 comments on commit 4fd190a

Please sign in to comment.