forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbtxmanager_test.go
97 lines (90 loc) · 3.03 KB
/
dbtxmanager_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
package jsonrpc
import (
"context"
"errors"
"testing"
"github.com/jackc/pgx/v4"
"github.com/stretchr/testify/assert"
)
func TestNewDbTxScope(t *testing.T) {
type testCase struct {
Name string
Fn dbTxScopedFn
ExpectedResult interface{}
ExpectedError rpcError
SetupMocks func(s *stateMock, d *dbTxMock)
}
testCases := []testCase{
{
Name: "Run scoped func commits DB tx",
Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return 1, nil
},
ExpectedResult: 1,
ExpectedError: nil,
SetupMocks: func(s *stateMock, d *dbTxMock) {
d.On("Commit", context.Background()).Return(nil).Once()
s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
},
},
{
Name: "Run scoped func rollbacks DB tx",
Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return nil, newRPCError(defaultErrorCode, "func returned an error")
},
ExpectedResult: nil,
ExpectedError: newRPCError(defaultErrorCode, "func returned an error"),
SetupMocks: func(s *stateMock, d *dbTxMock) {
d.On("Rollback", context.Background()).Return(nil).Once()
s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
},
},
{
Name: "Run scoped func but fails create a db tx",
Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return nil, nil
},
ExpectedResult: nil,
ExpectedError: newRPCError(defaultErrorCode, "failed to connect to the state"),
SetupMocks: func(s *stateMock, d *dbTxMock) {
s.On("BeginStateTransaction", context.Background()).Return(nil, errors.New("failed to create db tx")).Once()
},
},
{
Name: "Run scoped func but fails to commit DB tx",
Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return 1, nil
},
ExpectedResult: nil,
ExpectedError: newRPCError(defaultErrorCode, "failed to commit db transaction"),
SetupMocks: func(s *stateMock, d *dbTxMock) {
d.On("Commit", context.Background()).Return(errors.New("failed to commit db tx")).Once()
s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
},
},
{
Name: "Run scoped func but fails to rollbacks DB tx",
Fn: func(ctx context.Context, dbTx pgx.Tx) (interface{}, rpcError) {
return nil, newRPCError(defaultErrorCode, "func returned an error")
},
ExpectedResult: nil,
ExpectedError: newRPCError(defaultErrorCode, "failed to rollback db transaction"),
SetupMocks: func(s *stateMock, d *dbTxMock) {
d.On("Rollback", context.Background()).Return(errors.New("failed to rollback db tx")).Once()
s.On("BeginStateTransaction", context.Background()).Return(d, nil).Once()
},
},
}
dbTxManager := dbTxManager{}
s := newStateMock(t)
d := newDbTxMock(t)
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
tc := testCase
tc.SetupMocks(s, d)
result, err := dbTxManager.NewDbTxScope(s, tc.Fn)
assert.Equal(t, tc.ExpectedResult, result)
assert.Equal(t, tc.ExpectedError, err)
})
}
}