-
Notifications
You must be signed in to change notification settings - Fork 15
/
transaction_test.go
86 lines (69 loc) · 2.23 KB
/
transaction_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
package sqldblogger
import (
"database/sql/driver"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestTransaction_Commit(t *testing.T) {
t.Run("Error", func(t *testing.T) {
txMock := &transactionMock{}
txMock.On("Commit").Return(driver.ErrBadConn)
conn := &transaction{Tx: txMock, logger: testLogger, id: testLogger.opt.uidGenerator.UniqueID()}
err := conn.Commit()
assert.Error(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Commit", output.Message)
assert.Equal(t, LevelError.String(), output.Level)
})
t.Run("Success", func(t *testing.T) {
txMock := &transactionMock{}
txMock.On("Commit").Return(nil)
conn := &transaction{Tx: txMock, logger: testLogger, id: testLogger.opt.uidGenerator.UniqueID()}
err := conn.Commit()
assert.NoError(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Commit", output.Message)
assert.Equal(t, LevelDebug.String(), output.Level)
})
}
func TestTransaction_Rollback(t *testing.T) {
t.Run("Error", func(t *testing.T) {
txMock := &transactionMock{}
txMock.On("Rollback").Return(driver.ErrBadConn)
conn := &transaction{Tx: txMock, logger: testLogger, id: testLogger.opt.uidGenerator.UniqueID()}
err := conn.Rollback()
assert.Error(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Rollback", output.Message)
assert.Equal(t, LevelError.String(), output.Level)
})
t.Run("Success", func(t *testing.T) {
txMock := &transactionMock{}
txMock.On("Rollback").Return(nil)
conn := &transaction{Tx: txMock, logger: testLogger, id: testLogger.opt.uidGenerator.UniqueID()}
err := conn.Rollback()
assert.NoError(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Rollback", output.Message)
assert.Equal(t, LevelDebug.String(), output.Level)
})
}
type transactionMock struct {
mock.Mock
}
func (m *transactionMock) Commit() error {
return m.Called().Error(0)
}
func (m *transactionMock) Rollback() error {
return m.Called().Error(0)
}