forked from marcboeker/go-duckdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduckdb_test.go
77 lines (63 loc) · 1.17 KB
/
duckdb_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
package duckdb
import (
"database/sql"
"testing"
)
func TestOpen(t *testing.T) {
db := openDB(t)
if db == nil {
t.Error("database cannot be nil")
}
}
func TestExec(t *testing.T) {
db := openDB(t)
res := createTable(db, t)
if res == nil {
t.Error("result cannot be nil")
}
}
func TestQuery(t *testing.T) {
db := openDB(t)
createTable(db, t)
_, err := db.Exec("INSERT INTO foo VALUES('lala', 12345)")
if err != nil {
t.Fatal(err)
}
rows, err := db.Query("SELECT bar, baz FROM foo WHERE baz > ?", 12344)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
found := false
for rows.Next() {
var (
bar string
baz int
)
err := rows.Scan(&bar, &baz)
if err != nil {
t.Fatal(err)
}
if bar != "lala" || baz != 12345 {
t.Errorf("wrong values for bar [%s] and baz [%d]", bar, baz)
}
found = true
}
if !found {
t.Error("could not find row")
}
}
func openDB(t *testing.T) *sql.DB {
db, err := sql.Open("duckdb", "")
if err != nil {
t.Fatal(err)
}
return db
}
func createTable(db *sql.DB, t *testing.T) *sql.Result {
res, err := db.Exec("CREATE TABLE foo(bar VARCHAR, baz INTEGER)")
if err != nil {
t.Fatal(err)
}
return &res
}