-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver_integration_v0_test.go
179 lines (158 loc) · 5.83 KB
/
driver_integration_v0_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
//go:build integration_v0
// +build integration_v0
package fireboltgosdk
import (
"context"
"database/sql"
"fmt"
"math"
"os"
"reflect"
"runtime/debug"
"testing"
"time"
)
var (
dsnMock string
dsnEngineUrlMock string
dsnDefaultEngineMock string
dsnDefaultAccountMock string
dsnSystemEngineMock string
usernameMock string
passwordMock string
databaseMock string
engineUrlMock string
engineNameMock string
accountNameMock string
serviceAccountClientId string
serviceAccountClientSecret string
clientMock *ClientImplV0
)
const v0Testing = true
// init populates mock variables and client for integration tests
func init() {
usernameMock = os.Getenv("USER_NAME")
passwordMock = os.Getenv("PASSWORD")
databaseMock = os.Getenv("DATABASE_NAME")
engineNameMock = os.Getenv("ENGINE_NAME")
engineUrlMock = os.Getenv("ENGINE_URL")
accountNameMock = os.Getenv("ACCOUNT_NAME")
dsnMock = fmt.Sprintf("firebolt://%s:%s@%s/%s?account_name=%s", usernameMock, passwordMock, databaseMock, engineNameMock, accountNameMock)
dsnEngineUrlMock = fmt.Sprintf("firebolt://%s:%s@%s/%s?account_name=%s", usernameMock, passwordMock, databaseMock, engineUrlMock, accountNameMock)
dsnDefaultEngineMock = fmt.Sprintf("firebolt://%s:%s@%s?account_name=%s", usernameMock, passwordMock, databaseMock, accountNameMock)
dsnDefaultAccountMock = fmt.Sprintf("firebolt://%s:%s@%s", usernameMock, passwordMock, databaseMock)
dsnSystemEngineMock = fmt.Sprintf("firebolt://%s:%s@%s/%s", usernameMock, passwordMock, databaseMock, "system")
client, err := Authenticate(&fireboltSettings{
clientID: usernameMock,
clientSecret: passwordMock,
newVersion: false,
}, GetHostNameURL())
if err != nil {
panic(fmt.Errorf("Error authenticating with username password %s: %v", usernameMock, err))
}
clientMock = client.(*ClientImplV0)
}
// TestDriverQueryResult tests query happy path, as user would do it
func TestDriverQueryResult(t *testing.T) {
loc, _ := time.LoadLocation("UTC")
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
rows, err := db.Query(
"SELECT CAST('2020-01-03 19:08:45' AS DATETIME) as dt, CAST('2020-01-03' AS DATE) as d, CAST(1 AS INT) as i, '-inf'::float as f " +
"UNION " +
"SELECT CAST('2021-01-03 19:38:34' AS DATETIME) as dt, CAST('2000-12-03' AS DATE) as d, CAST(2 AS INT) as i, 'nan'::float as f ORDER BY i")
if err != nil {
t.Errorf("db.Query returned an error: %v", err)
}
var dt, d time.Time
var i int
var f float64
expectedColumns := []string{"dt", "d", "i", "f"}
if columns, err := rows.Columns(); reflect.DeepEqual(expectedColumns, columns) && err != nil {
t.Errorf("columns are not equal (%v != %v) and error is %v", expectedColumns, columns, err)
}
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&dt, &d, &i, &f), nil, t, "Scan returned an error")
assert(dt, time.Date(2020, 01, 03, 19, 8, 45, 0, loc), t, "results not equal for datetime")
assert(d, time.Date(2020, 01, 03, 0, 0, 0, 0, loc), t, "results not equal for date")
assert(i, 1, t, "results not equal for int")
assert(f, math.Inf(-1), t, "results not equal for float")
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&dt, &d, &i, &f), nil, t, "Scan returned an error")
assert(dt, time.Date(2021, 01, 03, 19, 38, 34, 0, loc), t, "results not equal for datetime")
assert(d, time.Date(2000, 12, 03, 0, 0, 0, 0, loc), t, "results not equal for date")
assert(i, 2, t, "results not equal for int")
if !math.IsNaN(f) {
t.Log(string(debug.Stack()))
t.Errorf("results not equal for float Expected: NaN Got: %f", f)
}
if rows.Next() {
t.Errorf("Next didn't returned false, although no data is expected")
}
}
// TestDriverInfNanValues tests query with inf and nan values
func TestDriverInfNanValues(t *testing.T) {
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly with %v", err)
}
rows, err := db.Query("SELECT '-inf'::double as f, 'inf'::double as f2, 'nan'::double as f3, '-nan'::double as f4")
if err != nil {
t.Errorf("db.Query returned an error: %v", err)
}
var f, f2, f3, f4 float64
if !rows.Next() {
t.Errorf("Next returned end of output")
}
assert(rows.Scan(&f, &f2, &f3, &f4), nil, t, "Scan returned an error")
if !math.IsInf(f, -1) {
t.Errorf("results not equal for float Expected: -Inf Got: %f", f)
}
if !math.IsInf(f2, 1) {
t.Errorf("results not equal for float Expected: Inf Got: %f", f2)
}
if !math.IsNaN(f3) {
t.Errorf("results not equal for float Expected: NaN Got: %f", f3)
}
if !math.IsNaN(f4) {
t.Errorf("results not equal for float Expected: NaN Got: %f", f4)
}
}
// TestDriverOpenConnection checks making a connection on opened connector
func TestDriverOpenConnection(t *testing.T) {
db, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("failed unexpectedly")
}
ctx := context.TODO()
if _, err = db.Conn(ctx); err != nil {
t.Errorf("connection is not established correctly: %v", err)
}
}
func runTestDriverExecStatement(t *testing.T, dsn string) {
db, err := sql.Open("firebolt", dsn)
if err != nil {
t.Errorf("failed unexpectedly")
}
if _, err = db.Exec("SELECT 1"); err != nil {
t.Errorf("connection is not established correctly")
}
}
// TestDriverOpenEngineUrl checks opening connector with a default engine
func TestDriverOpenEngineUrl(t *testing.T) {
runTestDriverExecStatement(t, dsnEngineUrlMock)
}
// TestDriverOpenDefaultEngine checks opening connector with a default engine
func TestDriverOpenDefaultEngine(t *testing.T) {
runTestDriverExecStatement(t, dsnDefaultEngineMock)
}
// TestDriverExecStatement checks exec with full dsn
func TestDriverExecStatement(t *testing.T) {
runTestDriverExecStatement(t, dsnMock)
}