-
Notifications
You must be signed in to change notification settings - Fork 3
/
transaction_test.go
224 lines (192 loc) · 5.86 KB
/
transaction_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
package fb
import (
"os"
"strconv"
"testing"
)
func TestTransactionStart(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
if conn.TransactionStarted() {
t.Fatal("No transaction should be started yet.")
}
if err := conn.TransactionStart(""); err != nil {
t.Fatalf("Unexpected error starting 1st transaction: %s", err)
}
if !conn.TransactionStarted() {
t.Fatal("1st transaction should be started.")
}
if err := conn.Commit(); err != nil {
t.Fatalf("Unexpected error committing 1st transaction: %s", err)
}
if conn.TransactionStarted() {
t.Fatal("1st transaction should no longer be started.")
}
if err := conn.TransactionStart(""); err != nil {
t.Fatalf("Unexpected error starting 2nd transaction: %s", err)
}
if !conn.TransactionStarted() {
t.Fatal("2nd transaction should be started.")
}
if err := conn.Rollback(); err != nil {
t.Fatalf("Unexpected error rolling back 2nd transaction: %s", err)
}
if conn.TransactionStarted() {
t.Fatal("2nd transaction should no longer be started.")
}
}
func TestTransactionStart_bogus(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
err = conn.TransactionStart("BOGUS")
if err == nil {
t.Fatal("Expected error starting transaction.")
}
if err.Error() != "Illegal transaction option was specified" {
t.Errorf("Unexpected transaction error message: %v", err)
}
if conn.TransactionStarted() {
t.Fatal("No transaction should have been started.")
}
}
func TestAutoTransactionInsertWithError(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
const sqlSchema = "CREATE TABLE TEST (ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(20))"
const sqlInsert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
if _, err = conn.Execute(sqlSchema); err != nil {
t.Fatalf("Error executing schema: %s", err)
}
if conn.TransactionStarted() {
t.Fatal("No transaction should be started by schema statement.")
}
if _, err = conn.Execute(sqlInsert, 1, "one"); err != nil {
t.Fatalf("Error executing insert: %s", err)
}
if conn.TransactionStarted() {
t.Fatal("Auto transaction should already be committed.")
}
if _, err = conn.Execute(sqlInsert, 1, "two"); err == nil {
t.Fatal("Expecting error executing insert.")
}
if conn.TransactionStarted() {
t.Fatal("Auto transaction should not be left open.")
}
}
func TestQueryInTransaction(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
if conn.TransactionStarted() {
t.Fatal("No transaction should be started.")
}
if err := conn.TransactionStart(""); err != nil {
t.Fatalf("Unexpected error starting transaction: %s", err)
}
if !conn.TransactionStarted() {
t.Fatal("Transaction should be started.")
}
cursor, err := conn.Execute("select * from rdb$database")
if err != nil {
t.Fatalf("Unexpected error executing query: %s", err)
}
defer cursor.Close()
if !conn.TransactionStarted() {
t.Fatal("Transaction should still be open.")
}
if err := conn.Commit(); err != nil {
t.Fatalf("Unexpected error committing transaction: %s", err)
}
if conn.TransactionStarted() {
t.Fatal("Transaction should no longer be open.")
}
}
func TestInsertCommit(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
const sqlSchema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
const sqlInsert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
const sqlSelect = "SELECT * FROM TEST ORDER BY ID"
if _, err = conn.Execute(sqlSchema); err != nil {
t.Fatalf("Error executing schema: %s", err)
}
if err := conn.TransactionStart(""); err != nil {
t.Fatalf("Unexpected error starting transaction: %s", err)
}
for i := 0; i < 10; i++ {
conn.Execute(sqlInsert, i, strconv.Itoa(i))
}
if err := conn.Commit(); err != nil {
t.Fatalf("Unexpected error committing transaction: %s", err)
}
var cursor *Cursor
if cursor, err = conn.Execute(sqlSelect); err != nil {
t.Fatalf("Unexpected error in select: %s", err)
}
defer cursor.Close()
for i := 0; i < 10; i++ {
if !cursor.Next() {
t.Fatalf("Error in fetch: %s", cursor.Err())
}
vals := cursor.Row()
if vals[0].(int32) != int32(i) {
t.Fatalf("Expected %d, got %v", i, vals[0])
}
if vals[1].(string) != strconv.Itoa(i) {
t.Fatalf("Expected %s, got %v", strconv.Itoa(i), vals[1])
}
}
if cursor.Next() {
t.Fatal("Expected error due to cursor being at end of data.")
}
}
func TestInsertRollback(t *testing.T) {
os.Remove(TestFilename)
conn, err := Create(TestConnectionString)
if err != nil {
t.Fatalf("Unexpected error creating database: %s", err)
}
defer conn.Drop()
const sqlSchema = "CREATE TABLE TEST (ID INT, NAME VARCHAR(20))"
const sqlInsert = "INSERT INTO TEST (ID, NAME) VALUES (?, ?)"
const sqlSelect = "SELECT * FROM TEST ORDER BY ID"
if _, err = conn.Execute(sqlSchema); err != nil {
t.Fatalf("Error executing schema: %s", err)
}
if err := conn.TransactionStart(""); err != nil {
t.Fatalf("Unexpected error starting transaction: %s", err)
}
for i := 0; i < 10; i++ {
conn.Execute(sqlInsert, i, strconv.Itoa(i))
}
if err := conn.Rollback(); err != nil {
t.Fatalf("Unexpected error rolling back transaction: %s", err)
}
var cursor *Cursor
if cursor, err = conn.Execute(sqlSelect); err != nil {
t.Fatalf("Unexpected error in select: %s", err)
}
defer cursor.Close()
if cursor.Next() {
t.Fatal("Expected error due to cursor being at end of data.")
}
}