-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package testtx | ||
|
||
import "context" | ||
|
||
func New() *TX { | ||
return &TX{} | ||
} | ||
|
||
type TX struct { | ||
Err error | ||
} | ||
|
||
func (t *TX) WithTransaction(ctx context.Context, f func(ctx context.Context) error) error { | ||
t.Err = f(ctx) | ||
|
||
return t.Err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package testtx_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aneshas/tx/v2" | ||
"github.com/aneshas/tx/v2/testtx" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
type svc struct { | ||
tx.Transactor | ||
|
||
DidSomething bool | ||
} | ||
|
||
func (s *svc) doSomething(ctx context.Context, err error) error { | ||
return s.WithTransaction(ctx, func(ctx context.Context) error { | ||
s.DidSomething = true | ||
|
||
return err | ||
}) | ||
} | ||
|
||
func TestShould_Delegate_Call(t *testing.T) { | ||
transactor := testtx.New() | ||
|
||
s := &svc{ | ||
Transactor: transactor, | ||
} | ||
|
||
err := s.doSomething(context.TODO(), nil) | ||
|
||
assert.NoError(t, err) | ||
assert.True(t, s.DidSomething) | ||
} | ||
|
||
func TestShould_Save_Err(t *testing.T) { | ||
transactor := testtx.New() | ||
|
||
s := &svc{ | ||
Transactor: transactor, | ||
} | ||
|
||
wantErr := fmt.Errorf("something bad ocurred") | ||
|
||
err := s.doSomething(context.TODO(), wantErr) | ||
|
||
assert.ErrorIs(t, err, wantErr) | ||
assert.ErrorIs(t, transactor.Err, wantErr) | ||
} |