-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbtxn_test.go
133 lines (116 loc) · 3.11 KB
/
dbtxn_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
package dbtxn_test
import (
"context"
"database/sql"
"errors"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/imantung/dbtxn"
"github.com/stretchr/testify/require"
)
func TestGet(t *testing.T) {
testcases := []struct {
TestName string
Ctx context.Context
ExpectedContext *dbtxn.Context
}{
{
Ctx: nil,
ExpectedContext: nil,
},
{
Ctx: context.Background(),
ExpectedContext: nil,
},
{
Ctx: context.WithValue(context.Background(), dbtxn.ContextKey, "meh"),
ExpectedContext: nil,
},
{
Ctx: context.WithValue(context.Background(), dbtxn.ContextKey, &dbtxn.Context{}),
ExpectedContext: &dbtxn.Context{},
},
}
for _, tt := range testcases {
t.Run(tt.TestName, func(t *testing.T) {
require.Equal(t, tt.ExpectedContext, dbtxn.Get(tt.Ctx))
})
}
}
func TestUse(t *testing.T) {
testcases := []struct {
TestName string
Ctx context.Context
DB *sql.DB
Expected *dbtxn.UseHandler
ExpectedErr string
}{
{
Ctx: nil,
ExpectedErr: "dbtxn: missing context.Context",
},
{
TestName: "non transactional",
DB: &sql.DB{},
Ctx: context.Background(),
Expected: &dbtxn.UseHandler{DB: &sql.DB{}},
},
{
TestName: "begin error",
DB: func() *sql.DB {
db, mock, _ := sqlmock.New()
mock.ExpectBegin().WillReturnError(errors.New("begin-error"))
return db
}(),
Ctx: context.WithValue(context.Background(), dbtxn.ContextKey, &dbtxn.Context{}),
ExpectedErr: "begin-error",
},
}
for _, tt := range testcases {
t.Run(tt.TestName, func(t *testing.T) {
handler, err := dbtxn.Use(tt.Ctx, tt.DB)
if tt.ExpectedErr != "" {
require.EqualError(t, err, tt.ExpectedErr)
} else {
require.NoError(t, err)
require.Equal(t, tt.Expected, handler)
}
})
}
}
func TestUse_success(t *testing.T) {
ctx := context.WithValue(context.Background(), dbtxn.ContextKey, &dbtxn.Context{TxMap: make(map[*sql.DB]dbtxn.Tx)})
db, mock, _ := sqlmock.New()
var handler *dbtxn.UseHandler
var err error
t.Run("trigger begin transaction when no transaction object", func(t *testing.T) {
mock.ExpectBegin()
handler, err = dbtxn.Use(ctx, db)
require.NoError(t, err)
require.Equal(t, map[*sql.DB]dbtxn.Tx{
db: handler.DB.(dbtxn.Tx),
}, handler.Context.TxMap)
})
t.Run("using available transaction", func(t *testing.T) {
handler2, err := dbtxn.Use(ctx, db)
require.NoError(t, err)
require.Equal(t, handler, handler2)
})
}
func TestAppendError(t *testing.T) {
ctx := context.Background()
t.Run("no txn error before begin", func(t *testing.T) {
require.Nil(t, dbtxn.Error(ctx))
})
t.Run("append multiple error", func(t *testing.T) {
dbtxn.Begin(&ctx)
db, mock, _ := sqlmock.New()
mock.ExpectBegin()
handler, err := dbtxn.Use(ctx, db)
require.NoError(t, err)
require.True(t, handler.AppendError(errors.New("some-error-1")))
require.False(t, handler.AppendError(nil))
require.True(t, handler.AppendError(errors.New("some-error-2")))
require.EqualError(t, dbtxn.Error(ctx), "some-error-1; some-error-2")
})
}