-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwrite_test.go
354 lines (303 loc) · 10.3 KB
/
write_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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package gorqlite_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/rqlite/gorqlite"
)
func TestWriteOne(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT)")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
t.Cleanup(func() {
wr, err := globalConnection.WriteOne("DROP TABLE " + testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
t.Run("WriteOne CREATE", func(t *testing.T) {
tableName := RandStringBytes(8)
wr, err := globalConnection.WriteOne("CREATE TABLE " + tableName + " (id integer, name text)")
if err != nil {
t.Errorf("failed during table creation: %s - %s", err.Error(), wr.Err.Error())
}
if err == nil && wr.Err != nil {
t.Errorf("wr.Err must be nil if err is nil: %v", wr.Err.Error())
}
_, err = globalConnection.WriteOne("DROP TABLE IF EXISTS " + tableName)
if err != nil {
t.Errorf("failed during table deletion for: %s - %s", tableName, err.Error())
}
})
t.Run("WriteOne DROP", func(t *testing.T) {
// This is safe as the table was not found
// (the keyword we're executing contains IF EXISTS clause)
tableName := RandStringBytes(8)
wr, err := globalConnection.WriteOne("DROP TABLE IF EXISTS " + tableName)
if err != nil {
t.Errorf("failed during table deletion: %v - %v", err.Error(), wr.Err)
}
if err == nil && wr.Err != nil {
t.Errorf("wr.Err must be nil if err is nil: %v", wr.Err.Error())
}
})
t.Run("WriteOne CTHULHU (should fail, bad SQL)", func(t *testing.T) {
_, err := globalConnection.WriteOne("CTHULHU")
if err == nil {
t.Errorf("err must not be nil")
}
})
t.Run("WriteOne INSERT", func(t *testing.T) {
wr, err := globalConnection.WriteOne("INSERT INTO " + testTableName() + " (id, name) VALUES ( 1, 'aaa bbb ccc' )")
if err != nil {
t.Errorf("failed during insert: %v - %v", err.Error(), wr.Err.Error())
}
if err == nil && wr.Err != nil {
t.Errorf("wr.Err must be nil if err is nil: %v", wr.Err.Error())
}
if wr.RowsAffected != 1 {
t.Errorf("wr.RowsAffected must be 1")
}
})
}
func TestQueueOne(t *testing.T) {
t.Cleanup(func() {
r, err := globalConnection.WriteOne("DROP TABLE IF EXISTS " + testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), r.Err.Error())
}
})
t.Run("QueueOne DROP", func(t *testing.T) {
tableName := RandStringBytes(8)
seq, err := globalConnection.QueueOne("DROP TABLE IF EXISTS " + tableName)
if err != nil {
t.Errorf("failed during table deletion: %v - %v", err.Error(), seq)
}
})
t.Run("QueueOne CREATE", func(t *testing.T) {
tableName := RandStringBytes(8)
seq, err := globalConnection.QueueOne("CREATE TABLE " + tableName + " (id integer, name text)")
if err != nil {
t.Errorf("failed during table creation: %v - %v", err.Error(), seq)
}
seq, err = globalConnection.QueueOne("DROP TABLE IF EXISTS " + tableName)
if err != nil {
t.Errorf("failed during table deletion: %s - %d", err.Error(), seq)
}
})
t.Run("QueueOne INSERT", func(t *testing.T) {
seq, err := globalConnection.QueueOne("INSERT INTO " + testTableName() + " (id, name) VALUES ( 10, 'aaa bbb ccc' )")
if err != nil {
t.Errorf("failed during insert: %v - %v", err.Error(), seq)
}
if seq == 0 {
t.Errorf("seq must not be 0")
}
})
}
func TestQueueOneContext(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT)")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "DROP TABLE "+testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
seq, err := globalConnection.QueueOneContext(ctx, fmt.Sprintf("INSERT INTO "+testTableName()+" (id, name) VALUES ( %d, %s )", 120, "aaa bbb ccc"))
if err != nil {
t.Errorf("failed during insert: %v - %v", err.Error(), seq)
}
}
func TestWriteOneParameterized(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT)")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "DROP TABLE "+testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
t.Run("CTHULHU (should fail, bad SQL)", func(t *testing.T) {
_, err := globalConnection.WriteOneParameterized(
gorqlite.ParameterizedStatement{
Query: "CTHULHU",
},
)
if err == nil {
t.Error("expected an error, got nil instead")
}
})
t.Run("CREATE", func(t *testing.T) {
_, err := globalConnection.WriteOneParameterized(
gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id integer, name text)", testTableName()),
},
)
if err != nil {
t.Errorf("creating table: %s", err.Error())
}
})
t.Run("INSERT", func(t *testing.T) {
wr, err := globalConnection.WriteOneParameterized(
gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES ( 1, 'aaa bbb ccc' )", testTableName()),
},
)
if err != nil {
t.Errorf("insert query: %s", err.Error())
}
if wr.RowsAffected != 1 {
t.Errorf("expected 1 row affected, got %d", wr.RowsAffected)
}
})
}
func TestQueueOneParameterized(t *testing.T) {
_, err := globalConnection.QueueOneParameterized(
gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id integer, name text)", testTableName()),
},
)
if err != nil {
t.Errorf("failed during table creation: %s", err.Error())
}
t.Cleanup(func() {
_, err := globalConnection.QueueOneParameterized(
gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("DROP TABLE %s", testTableName()),
},
)
if err != nil {
t.Errorf("failed during dropping table: %s", err.Error())
}
})
t.Run("QueryOneParameterized INSERT", func(t *testing.T) {
seq, err := globalConnection.QueueOneParameterized(gorqlite.ParameterizedStatement{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES ( 1, 'aaa bbb ccc' )", testTableName()),
})
if err != nil {
t.Errorf("failed during insert: %s", err.Error())
}
if seq == 0 {
t.Error("expected a sequence ID, got 0")
}
})
}
func TestWrite(t *testing.T) {
t.Cleanup(func() {
wr, err := globalConnection.WriteOne("DROP TABLE IF EXISTS " + testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
t.Run("Write DROP & CREATE", func(t *testing.T) {
tableName := RandStringBytes(8)
s := make([]string, 0)
s = append(s, "CREATE TABLE "+tableName+" (id integer, name text)")
s = append(s, "DROP TABLE IF EXISTS "+tableName)
results, err := globalConnection.Write(s)
if err != nil {
t.Errorf("failed during table creation: %v - %v", err.Error(), results[0].Err.Error())
}
})
t.Run("Write INSERT", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT)")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
s := make([]string, 0)
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 21, 'aaa bbb ccc' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 22, 'ddd eee fff' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 23, 'ggg hhh iii' )")
s = append(s, "INSERT INTO "+testTableName()+" (id, name) VALUES ( 24, 'jjj kkk lll' )")
results, err := globalConnection.Write(s)
if err != nil {
for _, result := range results {
if result.Err != nil {
t.Errorf("result error: %v", result.Err)
}
}
}
if len(results) != 4 {
t.Errorf("expected results to be length of 4, got: %d", len(results))
}
})
}
func TestWriteParameterized(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "CREATE TABLE "+testTableName()+" (id INTEGER, name TEXT)")
if err != nil {
t.Fatalf("creating table: %s - %s", err.Error(), wr.Err.Error())
}
t.Cleanup(func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
wr, err := globalConnection.WriteOneContext(ctx, "DROP TABLE "+testTableName())
if err != nil {
t.Errorf("dropping table: %s - %s", err.Error(), wr.Err.Error())
}
})
t.Run("DROP", func(t *testing.T) {
tableName := RandStringBytes(8)
_, err := globalConnection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: fmt.Sprintf("CREATE TABLE %s (id integer, name text)", tableName),
},
{
Query: fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName),
},
},
)
if err != nil {
t.Errorf("failed during dropping & creating table: %s", err.Error())
}
})
t.Run("INSERT", func(t *testing.T) {
results, err := globalConnection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES (?, ?)", testTableName()),
Arguments: []interface{}{1, "aaa bbb ccc"},
},
{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES (?, ?)", testTableName()),
Arguments: []interface{}{1, "aaa bbb ccc"},
},
{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES (?, ?)", testTableName()),
Arguments: []interface{}{1, "aaa bbb ccc"},
},
{
Query: fmt.Sprintf("INSERT INTO %s (id, name) VALUES (?, ?)", testTableName()),
Arguments: []interface{}{1, "aaa bbb ccc"},
},
},
)
if err != nil {
t.Errorf("writing multiple parameterized queries: %s", err.Error())
}
if len(results) != 4 {
t.Errorf("expected 4 results, got %d", len(results))
}
})
}