-
Notifications
You must be signed in to change notification settings - Fork 15
/
result_test.go
94 lines (79 loc) · 2.68 KB
/
result_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
package sqldblogger
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestResult_LastInsertId(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resMock := &resultMock{}
resMock.On("LastInsertId").Return(0, errors.New("dummy"))
r := &result{Result: resMock, logger: testLogger, connID: testLogger.opt.uidGenerator.UniqueID(), query: "SELECT 1"}
id, err := r.LastInsertId()
assert.Equal(t, int64(0), id)
assert.Error(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, LevelError.String(), output.Level)
assert.Equal(t, "dummy", output.Data[testOpts.errorFieldname])
assert.NotEmpty(t, output.Data[testOpts.connIDFieldname])
assert.NotEmpty(t, output.Data[testOpts.sqlQueryFieldname])
bufLogger.Reset()
})
t.Run("Success", func(t *testing.T) {
resMock := &resultMock{}
resMock.On("LastInsertId").Return(1, nil)
r := &result{Result: resMock, logger: testLogger, connID: testLogger.opt.uidGenerator.UniqueID(), query: "SELECT 1"}
id, err := r.LastInsertId()
assert.Equal(t, int64(1), id)
assert.NoError(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.Error(t, err)
bufLogger.Reset()
})
}
func TestResult_RowsAffected(t *testing.T) {
t.Run("Error", func(t *testing.T) {
resMock := &resultMock{}
resMock.On("RowsAffected").Return(0, errors.New("dummy"))
r := &result{Result: resMock, logger: testLogger, connID: testLogger.opt.uidGenerator.UniqueID(), query: "SELECT 1"}
id, err := r.RowsAffected()
assert.Equal(t, int64(0), id)
assert.Error(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, LevelError.String(), output.Level)
assert.Equal(t, "dummy", output.Data[testOpts.errorFieldname])
assert.NotEmpty(t, output.Data[testOpts.connIDFieldname])
assert.NotEmpty(t, output.Data[testOpts.sqlQueryFieldname])
bufLogger.Reset()
})
t.Run("Success", func(t *testing.T) {
resMock := &resultMock{}
resMock.On("RowsAffected").Return(1, nil)
r := &result{Result: resMock, logger: testLogger, connID: testLogger.opt.uidGenerator.UniqueID()}
id, err := r.RowsAffected()
assert.Equal(t, int64(1), id)
assert.NoError(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.Error(t, err)
bufLogger.Reset()
})
}
type resultMock struct {
mock.Mock
}
func (m *resultMock) LastInsertId() (int64, error) {
arg := m.Called()
return int64(arg.Int(0)), arg.Error(1)
}
func (m *resultMock) RowsAffected() (int64, error) {
arg := m.Called()
return int64(arg.Int(0)), arg.Error(1)
}