forked from firebolt-db/firebolt-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_test.go
63 lines (55 loc) · 1.81 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
package fireboltgosdk
import (
"database/sql"
"net/http"
"net/http/httptest"
"os"
"testing"
)
// TestDriverOpen tests that the connector is opened (happy path)
func TestDriverOpen(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == UsernamePasswordURLSuffix {
_, _ = w.Write(getAuthResponse(10000))
} else if r.URL.Path == DefaultAccountURL {
_, _ = w.Write(getDefaultAccountResponse())
}
}))
defer server.Close()
currentEndpoint := os.Getenv("FIREBOLT_ENDPOINT")
os.Setenv("FIREBOLT_ENDPOINT", server.URL)
defer os.Setenv("FIREBOLT_ENDPOINT", currentEndpoint)
db, err := sql.Open("firebolt", "firebolt://user@fb:pass@db_name/eng.firebolt.io")
if err != nil {
t.Errorf("connection failed unexpectedly: %v", err)
}
if _, ok := db.Driver().(*FireboltDriver); !ok {
t.Errorf("returned connector is not a firebolt connector")
}
}
func getDefaultAccountResponse() []byte {
var response = `{
"account": {
"name": "default_account",
"id": "default_account_id"
}
}`
return []byte(response)
}
// TestDriverOpenFail tests opening a connector with wrong dsn
func TestDriverOpenFail(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == UsernamePasswordURLSuffix {
_, _ = w.Write(getAuthResponse(10000))
} else if r.URL.Path == DefaultAccountURL {
_, _ = w.Write(getDefaultAccountResponse())
}
}))
defer server.Close()
currentEndpoint := os.Getenv("FIREBOLT_ENDPOINT")
os.Setenv("FIREBOLT_ENDPOINT", server.URL)
defer os.Setenv("FIREBOLT_ENDPOINT", currentEndpoint)
if _, err := sql.Open("firebolt", "firebolt://pass@db_name"); err == nil {
t.Errorf("missing username in dsn should result into sql.Open error")
}
}