-
Notifications
You must be signed in to change notification settings - Fork 19
/
crate_test.go
117 lines (85 loc) · 1.85 KB
/
crate_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
// NOTE: this tests were written posteriorly
package crate
import (
"database/sql"
"os"
"testing"
)
var CRATE_URL string
func init() {
CRATE_URL = os.Getenv("CRATE_URL")
if CRATE_URL == "" {
CRATE_URL = "http://localhost:4200"
}
}
func connect() (*sql.DB, error) {
return sql.Open("crate", CRATE_URL)
}
func TestConnect(t *testing.T) {
_, err := connect()
if err != nil {
t.Fatalf("Error connecting: %s", err.Error())
}
}
func TestQuery(t *testing.T) {
db, _ := connect()
rows, err := db.Query("select count(*) from sys.cluster limit ?", 1)
if err != nil {
t.Fatalf("Error on db.Query: %s", err.Error())
}
cols, _ := rows.Columns()
n := len(cols)
if n != 1 {
t.Error(
"rows.Columns expected 1, but got,",
n,
cols,
)
}
rows, err = db.Query("select column_name from information_schema.columns")
for rows.Next() {
var column string
if err = rows.Scan(&column); err != nil {
t.Error(err)
}
}
}
func TestExec(t *testing.T) {
db, _ := connect()
_, err := db.Exec("create table go_crate (id int, str string)")
if err != nil {
t.Error(err)
}
_, err = db.Exec("drop table go_crate")
if err != nil {
t.Error(err)
}
}
func TestQueryRowBigInt(t *testing.T) {
db, _ := connect()
row := db.QueryRow("select 655300500 from sys.cluster ")
var test int
err := row.Scan(&test)
if err != nil {
t.Fatalf("Error on QueryRow.Scan - %v", err)
}
if test != 655300500 {
t.Error("Expected 1 on test, but got", test)
}
}
func TestPreparedStmtExec(t *testing.T) {
db, _ := connect()
stmt, err := db.Prepare("create table go_crate (id int, str string)")
stmt2, err2 := db.Prepare("drop table go_crate")
if err != nil || err2 != nil {
t.Fatalf("Error on db.Prepared() - %v - %v", err, err2)
}
_, err = stmt.Exec()
if err != nil {
t.Error(err)
}
_, err = stmt2.Exec()
if err != nil {
t.Error(err)
}
}