-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_trace_connection_pool_test.go
139 lines (105 loc) · 4.7 KB
/
stack_trace_connection_pool_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
134
135
136
137
138
139
package sqldb
import (
"context"
"errors"
"testing"
"github.com/smarty/assertions/should"
"github.com/smarty/gunit"
)
func TestStackTraceConnectionPoolFixture(t *testing.T) {
gunit.Run(new(StackTraceConnectionPoolFixture), t)
}
type StackTraceConnectionPoolFixture struct {
*gunit.Fixture
pool *FakeConnectionPool
adapter *StackTraceConnectionPool
}
func (this *StackTraceConnectionPoolFixture) Setup() {
this.pool = &FakeConnectionPool{}
this.adapter = NewStackTraceConnectionPool(this.pool)
this.adapter.StackTrace = ContrivedStackTrace("HELLO, WORLD!")
}
func (this *StackTraceConnectionPoolFixture) TestPing_WhenSuccessful_NoStackTraceIncluded() {
err := this.adapter.Ping(context.Background())
this.So(err, should.BeNil)
this.So(this.pool.pingCalls, should.Equal, 1)
}
func (this *StackTraceConnectionPoolFixture) TestPing_WhenFails_StackTraceAppendedToErr() {
this.pool.pingError = errors.New("PING ERROR")
err := this.adapter.Ping(context.Background())
this.So(this.pool.pingCalls, should.Equal, 1)
this.So(err, should.NotBeNil)
this.So(err.Error(), should.Equal, "PING ERROR\nStack Trace:\nHELLO, WORLD!")
}
func (this *StackTraceConnectionPoolFixture) TestClose_WhenSuccessful_NoStackTraceIncluded() {
err := this.adapter.Close()
this.So(err, should.BeNil)
this.So(this.pool.closeCalls, should.Equal, 1)
}
func (this *StackTraceConnectionPoolFixture) TestClose_WhenFails_StackTraceAppendedToErr() {
this.pool.closeError = errors.New("CLOSE ERROR")
err := this.adapter.Close()
this.So(this.pool.closeCalls, should.Equal, 1)
this.So(err, should.NotBeNil)
this.So(err.Error(), should.Equal, "CLOSE ERROR\nStack Trace:\nHELLO, WORLD!")
}
func (this *StackTraceConnectionPoolFixture) TestBeginTransaction_WhenSuccessful_NoStackTraceIncluded() {
transaction := new(FakeTransaction)
this.pool.transaction = transaction
tx, err := this.adapter.BeginTransaction(context.Background())
this.So(err, should.BeNil)
this.So(this.pool.transactionCalls, should.Equal, 1)
this.So(tx.(*StackTraceTransaction).inner, should.Equal, transaction)
}
func (this *StackTraceConnectionPoolFixture) TestBeginTransaction_WhenFails_StackTraceAppendedToErr() {
transaction := new(FakeTransaction)
this.pool.transaction = transaction
this.pool.transactionError = errors.New("TX ERROR")
tx, err := this.adapter.BeginTransaction(context.Background())
this.So(this.pool.transactionCalls, should.Equal, 1)
this.So(tx, should.BeNil)
this.So(err, should.NotBeNil)
this.So(err.Error(), should.Equal, "TX ERROR\nStack Trace:\nHELLO, WORLD!")
}
func (this *StackTraceConnectionPoolFixture) TestExecute_WhenSuccessful_NoStackTraceIncluded() {
this.pool.executeResult = 42
result, err := this.adapter.Execute(context.Background(), "QUERY", 1, 2, 3)
this.So(result, should.Equal, 42)
this.So(err, should.BeNil)
this.So(this.pool.executeCalls, should.Equal, 1)
this.So(this.pool.executeStatement, should.Equal, "QUERY")
this.So(this.pool.executeParameters, should.Resemble, []interface{}{1, 2, 3})
}
func (this *StackTraceConnectionPoolFixture) TestExecute_WhenFails_StackTraceAppendedToErr() {
this.pool.executeError = errors.New("EXECUTE ERROR")
this.pool.executeResult = 42
result, err := this.adapter.Execute(context.Background(), "QUERY", 1, 2, 3)
this.So(result, should.Equal, 42)
this.So(err, should.NotBeNil)
this.So(err.Error(), should.Equal, "EXECUTE ERROR\nStack Trace:\nHELLO, WORLD!")
this.So(this.pool.executeCalls, should.Equal, 1)
this.So(this.pool.executeStatement, should.Equal, "QUERY")
this.So(this.pool.executeParameters, should.Resemble, []interface{}{1, 2, 3})
}
func (this *StackTraceConnectionPoolFixture) TestSelect_WhenSuccessful_NoStackTraceIncluded() {
expectedResult := new(FakeSelectResult)
this.pool.selectResult = expectedResult
result, err := this.adapter.Select(context.Background(), "QUERY", 1, 2, 3)
this.So(result, should.Equal, expectedResult)
this.So(err, should.BeNil)
this.So(this.pool.selectCalls, should.Equal, 1)
this.So(this.pool.selectStatement, should.Equal, "QUERY")
this.So(this.pool.selectParameters, should.Resemble, []interface{}{1, 2, 3})
}
func (this *StackTraceConnectionPoolFixture) TestSelect_WhenFails_StackTraceAppendedToErr() {
expectedResult := new(FakeSelectResult)
this.pool.selectResult = expectedResult
this.pool.selectError = errors.New("SELECT ERROR")
result, err := this.adapter.Select(context.Background(), "QUERY", 1, 2, 3)
this.So(result, should.Equal, expectedResult)
this.So(err, should.NotBeNil)
this.So(err.Error(), should.Equal, "SELECT ERROR\nStack Trace:\nHELLO, WORLD!")
this.So(this.pool.selectCalls, should.Equal, 1)
this.So(this.pool.selectStatement, should.Equal, "QUERY")
this.So(this.pool.selectParameters, should.Resemble, []interface{}{1, 2, 3})
}