-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtx_test.go
150 lines (116 loc) · 3.25 KB
/
tx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package tx_test
import (
"context"
"fmt"
"github.com/aneshas/tx/v2"
"github.com/aneshas/tx/v2/testutil"
"github.com/aneshas/tx/v2/testutil/mocks"
"github.com/stretchr/testify/assert"
"testing"
)
func TestShould_Report_Transaction_Begin_Error(t *testing.T) {
wantErr := fmt.Errorf("something bad occurred")
db := testutil.NewDB(
t,
testutil.WithUnsuccessfulTransactionStart(wantErr),
)
transactor := tx.New(db)
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return nil
})
assert.ErrorIs(t, err, wantErr)
}
func TestShould_Commit_Transaction_On_No_Error(t *testing.T) {
db := testutil.NewDB(
t,
testutil.WithSuccessfulCommit(),
)
transactor := tx.New(db)
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return nil
})
assert.NoError(t, err)
}
func TestShould_Rollback_Transaction_On_Error(t *testing.T) {
db := testutil.NewDB(
t,
testutil.WithSuccessfulRollback(),
)
transactor := tx.New(db)
wantErr := fmt.Errorf("something bad occurred")
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})
assert.ErrorIs(t, err, wantErr)
}
func TestShould_Report_Unsuccessful_Rollback(t *testing.T) {
wantTxErr := fmt.Errorf("something bad occurred")
db := testutil.NewDB(
t,
testutil.WithUnsuccessfulRollback(wantTxErr),
)
transactor := tx.New(db)
wantErr := fmt.Errorf("process error")
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})
assert.ErrorIs(t, err, wantErr)
assert.ErrorIs(t, err, wantTxErr)
}
func TestShould_Rollback_Transaction_On_Panic_And_RePanic(t *testing.T) {
db := testutil.NewDB(
t,
testutil.WithSuccessfulRollback(),
)
transactor := tx.New(db)
defer func() {
if r := recover(); r == nil {
t.Fatalf("expected panic to be propagated")
}
}()
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
panic("something very bad occurred")
})
}
func TestShould_Still_Commit_On_Ignored_Error_And_Propagate_Error(t *testing.T) {
wantErr := fmt.Errorf("something bad occurred")
db := testutil.NewDB(
t,
testutil.WithSuccessfulCommit(),
)
transactor := tx.New(db, tx.WithIgnoredErrors(wantErr))
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
return wantErr
})
assert.ErrorIs(t, err, wantErr)
}
func TestShould_Retrieve_Tx_From_Context(t *testing.T) {
db := testutil.NewDB(
t,
testutil.WithSuccessfulCommit(),
)
transactor := tx.New(db)
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
ttx, ok := tx.From[tx.Transaction](ctx)
assert.True(t, ok)
assert.IsType(t, &mocks.Transaction{}, ttx)
return nil
})
}
func TestShould_Not_Retrieve_Conn_From_Context_On_Mismatched_Type(t *testing.T) {
db := testutil.NewDB(
t,
testutil.WithSuccessfulCommit(),
)
transactor := tx.New(db)
_ = transactor.WithTransaction(context.TODO(), func(ctx context.Context) error {
_, ok := tx.From[mocks.Transaction](ctx)
assert.False(t, ok)
return nil
})
}
func TestShould_Not_Retrieve_Conn_From_Context_Without_Transaction(t *testing.T) {
ttx, ok := tx.From[*mocks.Transaction](context.TODO())
assert.False(t, ok)
assert.Nil(t, ttx)
}