-
Notifications
You must be signed in to change notification settings - Fork 12
/
session_test.go
307 lines (275 loc) · 7.35 KB
/
session_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package tds
import (
"context"
"database/sql"
"database/sql/driver"
"reflect"
"testing"
)
func TestSimpleExec(t *testing.T) {
db := getConn(t)
if db == nil {
return
}
defer db.Close()
res, err := db.simpleExec(context.Background(), "set textsize 5000")
if err != nil {
t.Error("Conn.simpleExec failed:", err.Error())
return
}
if cnt, _ := res.RowsAffected(); cnt != 0 {
t.Errorf("Conn.simpleExec: Result.RowsAffected: exepected 0 rows affected, got %d", cnt)
return
}
}
func TestSingleValue(t *testing.T) {
db := getConn(t)
if db == nil {
return
}
defer db.Close()
res, err := db.SelectValue(context.Background(), "select 1")
if err != nil {
t.Error("Conn.selectValue failed:", err.Error())
return
}
intVal, ok := res.(int64)
if !ok {
t.Errorf("Conn.selectValue, expected int, got %s", reflect.TypeOf(res))
return
}
if intVal != 1 {
t.Errorf("Conn.selectValue, expected 1, got %d", intVal)
return
}
}
func TestTransactions(t *testing.T) {
db := getConn(t)
if db == nil {
return
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
t.Error("Conn.Begin failed:", err.Error())
return
}
res, err := db.SelectValue(context.Background(), "select @@trancount")
if err != nil {
t.Error("Conn.Begin: Conn.selectValue failed:", err.Error())
return
}
intVal, ok := res.(int64)
if !ok {
t.Errorf("Conn.Begin: Conn.selectValue, expected int, got %s", reflect.TypeOf(res))
return
}
if intVal != 1 {
t.Errorf("Conn.Begin: Conn.selectValue, expected 1, got %d", intVal)
return
}
tx.Rollback()
res, err = db.SelectValue(context.Background(), "select @@trancount")
if err != nil {
t.Error("Conn.Rollback: Conn.selectValue failed:", err.Error())
return
}
intVal, ok = res.(int64)
if !ok {
t.Errorf("Conn.Rollback: Conn.selectValue, expected int, got %s", reflect.TypeOf(res))
return
}
if intVal != 0 {
t.Errorf("Conn.Rollback: Conn.selectValue, expected 0, got %d", intVal)
return
}
tx, err = db.Begin()
if err != nil {
t.Error("Conn.Begin failed:", err.Error())
return
}
res, err = db.SelectValue(context.Background(), "select @@trancount -- begin")
if err != nil {
t.Error("Conn.Begin: Conn.selectValue failed:", err.Error())
return
}
intVal, ok = res.(int64)
if !ok {
t.Errorf("Conn.Begin: Conn.selectValue, expected int, got %s", reflect.TypeOf(res))
return
}
if intVal != 1 {
t.Errorf("Conn.Begin: Conn.selectValue, expected 1, got %d", intVal)
return
}
tx.Rollback()
res, err = db.SelectValue(context.Background(), "select @@trancount -- rollback")
if err != nil {
t.Error("Conn.Rollback: Conn.selectValue failed:", err.Error())
return
}
intVal, ok = res.(int64)
if !ok {
t.Errorf("Conn.Rollback: Conn.selectValue, expected int, got %s", reflect.TypeOf(res))
return
}
if intVal != 0 {
t.Errorf("Conn.Rollback: Conn.selectValue, expected 0, got %d", intVal)
return
}
}
type isolationTest struct {
input *sql.TxOptions
err error
output int
}
var isolationTests = []isolationTest{
{input: &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelDefault}, err: ErrNoReadOnly},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelReadUncommitted}, err: nil, output: 0},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelReadCommitted}, err: nil, output: 1},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelRepeatableRead}, err: nil, output: 2},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelSerializable}, err: nil, output: 3},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelSnapshot}, err: ErrInvalidIsolationLevel},
{input: &sql.TxOptions{ReadOnly: false, Isolation: sql.LevelLinearizable}, err: ErrInvalidIsolationLevel},
}
// test all the transaction levels
func TestIsolationLevels(t *testing.T) {
db := connect(t)
if db == nil {
return
}
defer db.Close()
for _, value := range isolationTests {
tx, err := db.BeginTx(context.Background(), value.input)
if err != value.err {
t.Fatal(err)
}
if value.err != nil {
continue
}
var isolation int
//err = tx.QueryRow("select @@isolation").Scan(&isolation)
err = tx.QueryRow("select @@isolation").Scan(&isolation)
if err != nil {
t.Fatal(err)
}
if value.output != isolation {
t.Errorf("wrong isolation level: %d != %d", isolation, value.output)
}
var trancount int
//err = tx.QueryRow("select @@isolation").Scan(&isolation)
err = tx.QueryRow("select @@trancount").Scan(&trancount)
if err != nil {
t.Fatal(err)
}
if trancount != 1 {
t.Error("Expected @@trancount set to 1")
}
tx.Rollback()
}
}
func TestBeginTranError(t *testing.T) {
conn := getConn(t)
if conn == nil {
t.Fatal("connect failed")
}
defer conn.Close()
// close actual connection to make begin transaction to fail during sending of a packet
conn.c.Close()
ctx := context.Background()
opts := driver.TxOptions{Isolation: driver.IsolationLevel(sql.LevelSerializable)}
_, err := conn.BeginTx(ctx, opts)
if err == nil || conn.valid {
t.Errorf("begin should fail as a bad connection, err=%v", err)
}
}
func TestCommitTranError(t *testing.T) {
conn := getConn(t)
if conn == nil {
t.Fatal("connect failed")
}
defer conn.Close()
// close actual connection to make begin transaction to fail during sending of a packet
conn.c.Close()
err := conn.Commit()
if err == nil || conn.valid {
t.Errorf("begin should fail as a bad connection, err=%v", err)
}
}
func TestRollbackTranError(t *testing.T) {
conn := getConn(t)
if conn == nil {
t.Fatal("connect failed")
}
defer conn.Close()
// close actual connection to make begin transaction to fail during sending of a packet
conn.c.Close()
err := conn.Rollback()
if err == nil || conn.valid {
t.Errorf("begin should fail as a bad connection, err=%v", err)
}
}
func TestBeginTxtReadOnlyNotSupported(t *testing.T) {
conn := connect(t)
if conn == nil {
t.Fatal("connect failed")
}
defer conn.Close()
opts := &sql.TxOptions{ReadOnly: true}
_, err := conn.BeginTx(context.Background(), opts)
if err == nil {
t.Error("BeginTx expected to fail for read only transaction because sybase doesn't support it, but it succeeded")
}
}
func TestConn_BeginTx(t *testing.T) {
conn := connect(t)
defer conn.Close()
conn.Exec("drop table test")
_, err := conn.Exec("create table test (f int)")
if err != nil {
t.Fatal("create table failed with error", err)
}
defer conn.Exec("drop table test")
tx1, err := conn.BeginTx(context.Background(), nil)
if err != nil {
t.Fatal("BeginTx failed with error", err)
}
tx2, err := conn.BeginTx(context.Background(), nil)
if err != nil {
t.Fatal("BeginTx failed with error", err)
}
_, err = tx1.Exec("insert into test (f) values (1)")
if err != nil {
t.Fatal("insert failed with error", err)
}
tx1.Rollback()
_, err = tx2.Exec("insert into test (f) values (2)")
if err != nil {
t.Fatal("insert failed with error", err)
}
tx2.Commit()
rows, err := conn.Query("select f from test")
if err != nil {
t.Fatal("select failed with error", err)
}
values := []int64{}
for rows.Next() {
var val int64
err = rows.Scan(&val)
if err != nil {
t.Fatal("scan failed with error", err)
}
values = append(values, val)
}
if !reflect.DeepEqual(values, []int64{2}) {
t.Errorf("Values is expected to be [1] but it is %v", values)
}
}
func TestPinger(t *testing.T) {
conn := connect(t)
defer conn.Close()
err := conn.Ping()
if err != nil {
t.Errorf("Failed to hit database")
}
}