-
Notifications
You must be signed in to change notification settings - Fork 15
/
connector_test.go
53 lines (43 loc) · 1.48 KB
/
connector_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
package sqldblogger
import (
"context"
"database/sql/driver"
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestConnector_Connect(t *testing.T) {
t.Run("Connect Error", func(t *testing.T) {
mockDriver := &driverMock{}
mockDriver.On("Open", mock.Anything).Return(&driverConnMock{}, driver.ErrBadConn)
con := &connector{dsn: "test", driver: mockDriver, logger: testLogger}
_, err := con.Connect(context.TODO())
assert.Error(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Connect", output.Message)
assert.Equal(t, LevelError.String(), output.Level)
assert.NotEmpty(t, output.Data[testOpts.connIDFieldname])
})
t.Run("Connect Success", func(t *testing.T) {
mockDriver := &driverMock{}
mockDriver.On("Open", mock.Anything).Return(&driverConnMock{}, nil)
con := &connector{dsn: "test", driver: mockDriver, logger: testLogger}
_, err := con.Connect(context.TODO())
assert.NoError(t, err)
var output bufLog
err = json.Unmarshal(bufLogger.Bytes(), &output)
assert.NoError(t, err)
assert.Equal(t, "Connect", output.Message)
assert.Equal(t, LevelDebug.String(), output.Level)
assert.NotEmpty(t, output.Data[testOpts.connIDFieldname])
})
}
func TestConnector_Driver(t *testing.T) {
mockDriver := &driverMock{}
con := &connector{dsn: "test", driver: mockDriver, logger: testLogger}
drv := con.Driver()
assert.Equal(t, mockDriver, drv)
}