-
Notifications
You must be signed in to change notification settings - Fork 27
/
driver_test.go
165 lines (139 loc) · 3.43 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
package gohive
import (
"database/sql"
"fmt"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
var username = "sqlflow"
var password = "sqlflow"
func newDB(dbName string) (*sql.DB, error) {
connStr := "127.0.0.1:10000"
pamAuth := os.Getenv("WITH_HS2_PAM_AUTH")
if dbName != "" {
connStr = fmt.Sprintf("%s/%s", connStr, dbName)
}
if pamAuth == "ON" {
connStr = fmt.Sprintf("%s:%s@%s?auth=PLAIN", username, password, connStr)
}
return sql.Open("hive", connStr)
}
func TestOpenConnection(t *testing.T) {
db, err := newDB("")
assert.Nil(t, err)
defer db.Close()
}
func TestOpenConnectionAgainstAuth(t *testing.T) {
if os.Getenv("WITH_HS2_PAM_AUTH") != "ON" {
db, _ := sql.Open("hive", "127.0.0.1:10000/churn?auth=PLAIN")
rows, err := db.Query("SELECT customerID, gender FROM train")
assert.EqualError(t, err, "Bad SASL negotiation status: 4 ()")
defer db.Close()
if err == nil {
defer rows.Close()
}
}
}
func TestQuery(t *testing.T) {
db, _ := newDB("churn")
rows, err := db.Query("SELECT customerID, gender FROM train")
assert.Nil(t, err)
defer db.Close()
defer rows.Close()
n := 0
customerid := ""
gender := ""
for rows.Next() {
err := rows.Scan(&customerid, &gender)
assert.Nil(t, err)
n++
}
assert.Nil(t, rows.Err())
assert.Equal(t, 82, n) // The imported data size is 82.
}
func TestColumnName(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
rows, err := db.Query("SELECT customerID, gender FROM train;")
assert.Nil(t, err)
defer db.Close()
defer rows.Close()
cl, err := rows.Columns()
a.NoError(err)
a.Equal(cl, []string{"customerid", "gender"})
}
func TestColumnTypeName(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
rows, err := db.Query("SELECT customerID, gender FROM train")
assert.Nil(t, err)
defer db.Close()
defer rows.Close()
ct, err := rows.ColumnTypes()
a.NoError(err)
for _, c := range ct {
assert.Equal(t, c.DatabaseTypeName(), "VARCHAR_TYPE")
}
}
func TestColumnType(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
rows, err := db.Query("SELECT customerID, gender FROM train")
defer db.Close()
defer rows.Close()
cts, err := rows.ColumnTypes()
a.NoError(err)
for _, ct := range cts {
assert.Equal(t, reflect.TypeOf("string"), ct.ScanType())
}
}
func TestShowCreateTable(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
rows, err := db.Query("show create table train")
defer db.Close()
defer rows.Close()
cts, err := rows.ColumnTypes()
a.NoError(err)
for _, ct := range cts {
assert.Equal(t, reflect.TypeOf("string"), ct.ScanType())
}
}
func TestDescribeTable(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
rows, err := db.Query("describe train")
defer db.Close()
defer rows.Close()
cts, err := rows.ColumnTypes()
a.NoError(err)
for _, ct := range cts {
assert.Equal(t, reflect.TypeOf("string"), ct.ScanType())
}
}
func TestShowDatabases(t *testing.T) {
a := assert.New(t)
db, _ := newDB("")
rows, err := db.Query("show databases")
defer db.Close()
defer rows.Close()
cts, err := rows.ColumnTypes()
a.NoError(err)
for _, ct := range cts {
assert.Equal(t, reflect.TypeOf("string"), ct.ScanType())
}
}
func TestPing(t *testing.T) {
db, _ := newDB("churn")
err := db.Ping()
assert.Nil(t, err)
}
func TestExec(t *testing.T) {
a := assert.New(t)
db, _ := newDB("churn")
_, err := db.Exec("insert into churn.test (gender) values ('Female')")
defer db.Close()
a.NoError(err)
}