-
Notifications
You must be signed in to change notification settings - Fork 14
/
driver_test.go
476 lines (457 loc) · 11.8 KB
/
driver_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
Copyright 2020 JM Robles (@jmrobles)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package h2go
import (
"database/sql"
"fmt"
"log"
"net"
"os"
"strconv"
"testing"
"time"
)
// Using a testing pattern similar to Go MySQL Driver (https://github.com/go-sql-driver/mysql)
// Main testing entry point
var (
user string
pass string
addr string
dbname string
inMem bool
dsn string
available bool
)
type dbTest struct {
*testing.T
conn *sql.DB
}
func (dt dbTest) checkErr(err error) {
if err != nil {
dt.Errorf("error: %s", err)
}
}
func init() {
env := func(key, defVal string) string {
if val := os.Getenv(key); val != "" {
return val
}
return defVal
}
user = env("H2_TEST_USER", "sa")
pass = env("H2_TEST_PASSWORD", "")
addr = env("H2_TEST_ADDR", "localhost:9092")
dbname = env("H2_TEST_DBNAME", "test")
inMemS := env("H2_TEST_IN_MEMORY", "true")
inMem, err := strconv.ParseBool(inMemS)
if err != nil {
inMem = true
}
if pass != "" {
dsn = fmt.Sprintf("h2://%s:%s@%s/%s?mem=%t", user, pass, addr, dbname, inMem)
} else {
dsn = fmt.Sprintf("h2://%s@%s/%s?mem=%t", user, addr, dbname, inMem)
}
// Check alive
c, err := net.Dial("tcp", addr)
if err == nil {
available = true
c.Close()
} else {
log.Printf("Can't connect: %s", err)
}
}
func runTests(t *testing.T, tests ...func(dt *dbTest)) {
var err error
if !available {
t.Errorf("H2 Server not running on %s", addr)
}
conn, err := sql.Open("h2", dsn)
if err != nil {
t.Fatalf("Can't connect to the H2 server: %s", err)
}
defer conn.Close()
db := &dbTest{t, conn}
for _, test := range tests {
test(db)
conn.Exec("DROP TABLE IF EXISTS test")
}
}
/*
func TestConnection(t *testing.T) {
conn, err := sql.Open("h2", "h2://sa@h2server:9092/test?mem=true")
if err != nil {
t.Errorf("Can't connect to the server: %s", err)
}
err = conn.Ping()
if err != nil {
t.Errorf("Can't do PING: %s", err)
}
}
*/
func TestPing(t *testing.T) {
runTests(t, func(dt *dbTest) {
err := dt.conn.Ping()
dt.checkErr(err)
})
}
func TestSimpleCRUD(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
// Create table
sent := "CREATE TABLE test (id int, name varchar, age int)"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// Insert a row
sent = "INSERT INTO test VALUES (1, 'Paco', 23)"
result, err := dt.conn.Exec(sent)
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
// Query
sent = "SELECT * FROM test"
rows, err := dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
var (
id int
name string
age int
)
err = rows.Scan(&id, &name, &age)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
if age != 23 {
dt.Errorf("Age mismatch (not equal to 23)")
}
}
err = rows.Close()
// Update row
sent = "UPDATE test SET age = 24 WHERE id = 1"
result, err = dt.conn.Exec(sent)
dt.checkErr(err)
nR, err = result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows updated not equal to 1")
}
// Query again
sent = "SELECT * FROM test"
rows, err = dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
var (
id int
name string
age int
)
err = rows.Scan(&id, &name, &age)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
if age != 24 {
dt.Errorf("Age mismatch (not equal to 24)")
}
}
err = rows.Close()
// Insert another row
sent = "INSERT INTO test VALUES (2, 'John', 24)"
result, err = dt.conn.Exec(sent)
dt.checkErr(err)
nR, err = result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
// Delete all
sent = "DELETE FROM test"
result, err = dt.conn.Exec(sent)
dt.checkErr(err)
nR, err = result.RowsAffected()
dt.checkErr(err)
if nR != 2 {
dt.Errorf("Num rows deleted not equal to 2")
}
// Skip DROP TABLE (done by the wrapper)
})
}
func TestCRUDwithParameters(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
var sent string
var (
id int = 1
name string = "Paco"
age int = 23
)
// Create table
sent = "CREATE TABLE test (id int, name varchar, age int)"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// Insert with parameters
sent = "INSERT INTO test VALUES (?, ?, ?)"
result, err := dt.conn.Exec(sent, id, name, age)
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
// Query
sent = "SELECT * FROM test"
rows, err := dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
var (
id int
name string
age int
)
err = rows.Scan(&id, &name, &age)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
if age != 23 {
dt.Errorf("Age mismatch (not equal to 23)")
}
}
err = rows.Close()
})
}
func TestDateTimeTypes(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
var sent string
var (
id int = 1
dtFixed time.Time
)
// Create table
sent = "CREATE TABLE test (id INT, t TIME, ttz TIME WITH TIME ZONE, d DATE, ts TIMESTAMP, tstz TIMESTAMP WITH TIME ZONE)"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// Insert a row
sent = "INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)"
loc, err := time.LoadLocation("Europe/Madrid")
if err != nil {
dt.Skipf("Can't get timezone for Europe/Madrid: %s", err)
}
dtFixed = time.Date(2020, 5, 25, 9, 1, 2, 123, loc)
result, err := dt.conn.Exec(sent, id, dtFixed, dtFixed, dtFixed, dtFixed, dtFixed)
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
// Query
sent = "SELECT * FROM test"
rows, err := dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
var (
vTime time.Time
vTimeTZ time.Time
vDate time.Time
vTimestamp time.Time
vTimestampTZ time.Time
)
err = rows.Scan(&id, &vTime, &vTimeTZ, &vDate, &vTimestamp, &vTimestampTZ)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
// TIME check
if vTime.Hour() != 9 || vTime.Minute() != 1 || vTime.Second() != 2 {
dt.Errorf("Time mismatch: %d %d %d", vTime.Hour(), vTime.Minute(), vTime.Second())
}
// TIME WITH TIME ZONE check
_, nSecOffset := vTimeTZ.Zone()
if vTimeTZ.Hour() != 9 || vTimeTZ.Minute() != 1 || vTimeTZ.Second() != 2 || nSecOffset != 7200 {
dt.Errorf("Time TZ mismatch: %d %d %d %d", vTimeTZ.Hour(), vTimeTZ.Minute(), vTimeTZ.Second(), nSecOffset)
}
// DATE check
if vDate.Day() != 25 || vDate.Month() != 5 || vDate.Year() != 2020 {
dt.Errorf("Date mismatch: %d %d %d", vDate.Day(), vDate.Month(), vDate.Year())
}
// TIMESTAMP check
if vTimestamp.Day() != 25 || vTimestamp.Month() != 5 || vTimestamp.Year() != 2020 || vTimestamp.Hour() != 9 || vTimestamp.Minute() != 1 || vTimestamp.Second() != 2 {
dt.Errorf("Timestamp mismatch: %d %d %d %d %d %d", vTimestamp.Day(), vTimestamp.Month(), vTimestamp.Year(), vTimestamp.Hour(), vTimestamp.Minute(), vTimestamp.Second())
}
// TIMESTAMP WITH TIME Zone check
_, nSecOffset = vTimeTZ.Zone()
if vTimestampTZ.Day() != 25 || vTimestampTZ.Month() != 5 || vTimestampTZ.Year() != 2020 || vTimestampTZ.Hour() != 9 || vTimestampTZ.Minute() != 1 || vTimestampTZ.Second() != 2 || nSecOffset != 7200 {
dt.Errorf("Timestamp TZ mismatch: %d %d %d %d %d %d %d", vTimestampTZ.Day(), vTimestampTZ.Month(), vTimestampTZ.Year(), vTimestampTZ.Hour(), vTimestampTZ.Minute(), vTimestampTZ.Second(), nSecOffset)
}
}
err = rows.Close()
})
}
func TestOtherTypes(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
var sent string
// CREATE TABLE
sent = "CREATE TABLE test (id INT, name VARCHAR(100), height FLOAT, isGood BOOLEAN, numAtoms DOUBLE, age SMALLINT)"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// INSERT
var (
id int = 1
name string = "Paco"
height float32 = 1.88
isGood bool = true
numAtoms float64 = 13213123332132.5
age int16 = 23
)
sent = "INSERT INTO test VALUES (?, ?, ?, ?, ?, ?)"
result, err := dt.conn.Exec(sent, id, name, height, isGood, numAtoms, age)
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
// Query
sent = "SELECT * FROM test"
rows, err := dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
err = rows.Scan(&id, &name, &height, &isGood, &numAtoms, &age)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
if height != 1.88 {
dt.Errorf("Height mismatch (not equal to 1.88)")
}
if !isGood {
dt.Errorf("isGood is false")
}
if numAtoms != 13213123332132.5 {
dt.Errorf("Num atoms mismatch (not equal to 13213123332132.5)")
}
if age != 23 {
dt.Errorf("Age mismatch (not equal to 23)")
}
}
rows.Close()
})
}
func TestStmt(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
var sent string
// CREATE TABLE
sent = "CREATE TABLE test (id INT, name VARCHAR(100))"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// Get Stmt
stmt, err := dt.conn.Prepare("INSERT INTO test VALUES (?,?)")
dt.checkErr(err)
result, err := stmt.Exec(1, "Paco")
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
})
}
func TestTx(t *testing.T) {
runTests(t, func(dt *dbTest) {
var err error
var sent string
// CREATE TABLE
sent = "CREATE TABLE test (id INT, name VARCHAR(100))"
_, err = dt.conn.Exec(sent)
dt.checkErr(err)
// TX with commit
tx, err := dt.conn.Begin()
dt.checkErr(err)
result, err := tx.Exec("INSERT INTO test VALUES (1, 'Paco')")
dt.checkErr(err)
nR, err := result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
err = tx.Commit()
dt.checkErr(err)
// Query
var (
id int
name string
)
sent = "SELECT * FROM test"
rows, err := dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
err = rows.Scan(&id, &name)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
}
rows.Close()
// Tx with rollback
tx, err = dt.conn.Begin()
dt.checkErr(err)
result, err = tx.Exec("INSERT INTO test VALUES (2, 'John')")
dt.checkErr(err)
nR, err = result.RowsAffected()
dt.checkErr(err)
if nR != 1 {
dt.Errorf("Num rows inserted not equal to 1")
}
err = tx.Rollback()
dt.checkErr(err)
// Query
sent = "SELECT * FROM test"
rows, err = dt.conn.Query(sent)
dt.checkErr(err)
for rows.Next() {
err = rows.Scan(&id, &name)
dt.checkErr(err)
if id != 1 {
dt.Errorf("ID mismatch (not equal to 1)")
}
if name != "Paco" {
dt.Errorf("Name mismatch (not equal to 'Paco')")
}
}
rows.Close()
})
}