From 4fd190ae677a96db7d4299fc16a2ac10bbdb1305 Mon Sep 17 00:00:00 2001 From: Anes Hasicic Date: Fri, 15 Mar 2024 14:26:41 +0100 Subject: [PATCH] Changed method name as per feedback --- Makefile | 2 +- example/transfer_money.go | 1 + go.mod | 2 +- option.go | 2 +- pgxtxv5/pgx_test.go | 2 +- sqltx/sql_test.go | 2 +- testutil/db.go | 7 ------- testutil/mocks/transactor.go | 22 +++++++++++----------- tx.go | 16 +++++++++++----- tx_test.go | 16 ++++++++-------- 10 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 example/transfer_money.go diff --git a/Makefile b/Makefile index 6983735..205df06 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/example/transfer_money.go b/example/transfer_money.go new file mode 100644 index 0000000..f7ec372 --- /dev/null +++ b/example/transfer_money.go @@ -0,0 +1 @@ +package example diff --git a/go.mod b/go.mod index 857a22f..8d85661 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/aneshas/tx +module github.com/aneshas/tx/v2 go 1.21.0 diff --git a/option.go b/option.go index a51bc4b..c44864a 100644 --- a/option.go +++ b/option.go @@ -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...) diff --git a/pgxtxv5/pgx_test.go b/pgxtxv5/pgx_test.go index 394266c..d84753f 100644 --- a/pgxtxv5/pgx_test.go +++ b/pgxtxv5/pgx_test.go @@ -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) diff --git a/sqltx/sql_test.go b/sqltx/sql_test.go index 726199e..d08349e 100644 --- a/sqltx/sql_test.go +++ b/sqltx/sql_test.go @@ -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) diff --git a/testutil/db.go b/testutil/db.go index fc1a3f0..f848752 100644 --- a/testutil/db.go +++ b/testutil/db.go @@ -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) diff --git a/testutil/mocks/transactor.go b/testutil/mocks/transactor.go index 447208f..fcf1632 100644 --- a/testutil/mocks/transactor.go +++ b/testutil/mocks/transactor.go @@ -21,12 +21,12 @@ func (_m *Transactor) EXPECT() *Transactor_Expecter { return &Transactor_Expecter{mock: &_m.Mock} } -// Do provides a mock function with given fields: ctx, f -func (_m *Transactor) Do(ctx context.Context, f func(context.Context) error) error { +// WithTransaction provides a mock function with given fields: ctx, f +func (_m *Transactor) WithTransaction(ctx context.Context, f func(context.Context) error) error { ret := _m.Called(ctx, f) if len(ret) == 0 { - panic("no return value specified for Do") + panic("no return value specified for WithTransaction") } var r0 error @@ -39,31 +39,31 @@ func (_m *Transactor) Do(ctx context.Context, f func(context.Context) error) err return r0 } -// Transactor_Do_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'Do' -type Transactor_Do_Call struct { +// Transactor_WithTransaction_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'WithTransaction' +type Transactor_WithTransaction_Call struct { *mock.Call } -// Do is a helper method to define mock.On call +// WithTransaction is a helper method to define mock.On call // - ctx context.Context // - f func(context.Context) error -func (_e *Transactor_Expecter) Do(ctx interface{}, f interface{}) *Transactor_Do_Call { - return &Transactor_Do_Call{Call: _e.mock.On("Do", ctx, f)} +func (_e *Transactor_Expecter) WithTransaction(ctx interface{}, f interface{}) *Transactor_WithTransaction_Call { + return &Transactor_WithTransaction_Call{Call: _e.mock.On("WithTransaction", ctx, f)} } -func (_c *Transactor_Do_Call) Run(run func(ctx context.Context, f func(context.Context) error)) *Transactor_Do_Call { +func (_c *Transactor_WithTransaction_Call) Run(run func(ctx context.Context, f func(context.Context) error)) *Transactor_WithTransaction_Call { _c.Call.Run(func(args mock.Arguments) { run(args[0].(context.Context), args[1].(func(context.Context) error)) }) return _c } -func (_c *Transactor_Do_Call) Return(_a0 error) *Transactor_Do_Call { +func (_c *Transactor_WithTransaction_Call) Return(_a0 error) *Transactor_WithTransaction_Call { _c.Call.Return(_a0) return _c } -func (_c *Transactor_Do_Call) RunAndReturn(run func(context.Context, func(context.Context) error) error) *Transactor_Do_Call { +func (_c *Transactor_WithTransaction_Call) RunAndReturn(run func(context.Context, func(context.Context) error) error) *Transactor_WithTransaction_Call { _c.Call.Return(run) return _c } diff --git a/tx.go b/tx.go index c8a445a..230c18f 100644 --- a/tx.go +++ b/tx.go @@ -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 @@ -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) diff --git a/tx_test.go b/tx_test.go index 28e2207..0b43400 100644 --- a/tx_test.go +++ b/tx_test.go @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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") }) } @@ -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 }) @@ -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) @@ -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)