-
Notifications
You must be signed in to change notification settings - Fork 44
/
connector_test.go
executable file
·97 lines (80 loc) · 2.08 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
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
// +build go1.10
package instrumentedsql
import (
"context"
"database/sql/driver"
"fmt"
"testing"
)
func TestConnectorWithDriverContext(t *testing.T) {
err := fmt.Errorf("a generic error")
tests := []struct {
name string
openConnectorErr error
expectErr bool
}{
{
name: "should properly open connector and wrap it",
},
{
name: "should fail when calling OpenConnector",
openConnectorErr: err,
expectErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
d := WrappedDriver{parent: &driverContextMock{err: test.openConnectorErr}}
conn, err := d.OpenConnector("some-dsn")
if err != nil {
if test.expectErr {
return
}
t.Fatalf("unexpected error from wrapped OpenConnector impl: %+v\n", err)
}
wc, ok := conn.(wrappedConnector)
if !ok {
t.Fatal("expected wrapped OpenConnector to return wrappedConnector instance")
}
_, ok = wc.parent.(*connMock)
if !ok {
t.Error("expected wrappedConnector to have connMock as parent")
}
})
}
}
func TestConnectorWithDriver(t *testing.T) {
d := WrappedDriver{parent: &driverMock{}}
conn, err := d.OpenConnector("some-dsn")
if err != nil {
t.Fatalf("unexpected error from wrapped OpenConnector impl: %+v\n", err)
}
wc, ok := conn.(wrappedConnector)
if !ok {
t.Fatal("expected wrapped OpenConnector to return wrappedConnector instance")
}
_, ok = wc.parent.(dsnConnector)
if !ok {
t.Error("expected wrappedConnector to have dsnConnector as parent")
}
}
type driverMock struct{}
func (d *driverMock) Open(name string) (driver.Conn, error) {
panic("not implemented")
}
type driverContextMock struct {
err error
}
func (d *driverContextMock) Open(name string) (driver.Conn, error) {
panic("not implemented")
}
func (d *driverContextMock) OpenConnector(name string) (driver.Connector, error) {
return &connMock{}, d.err
}
type connMock struct{}
func (c *connMock) Connect(context.Context) (driver.Conn, error) {
panic("not implemented")
}
func (c *connMock) Driver() driver.Driver {
panic("not implemented")
}