-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection_integration_test.go
373 lines (323 loc) · 10.8 KB
/
connection_integration_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//go:build integration
// +build integration
package fireboltgosdk
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"runtime/debug"
"testing"
"time"
)
func TestConnectionUseDatabase(t *testing.T) {
tableName := "test_use_database"
createTableSQL := "CREATE TABLE IF NOT EXISTS " + tableName + " (id INT)"
selectTableSQL := "SELECT table_name FROM information_schema.tables WHERE table_name = ?"
useDatabaseSQL := "USE DATABASE "
newDatabaseName := databaseMock + "_new"
conn, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("opening a connection failed unexpectedly: %v", err)
t.FailNow()
}
// We need separate connections for the original database and the system engine
// which are not affected by the USE command to clean up the resources properly
original_conn, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf("opening a second connection failed unexpectedly: %v", err)
t.FailNow()
}
system_conn, err := sql.Open("firebolt", dsnSystemEngineWithDatabaseMock)
if err != nil {
t.Errorf("opening a system connection failed unexpectedly: %v", err)
t.FailNow()
}
_, err = conn.ExecContext(context.Background(), useDatabaseSQL+databaseMock)
if err != nil {
t.Errorf("use database statement failed with %v", err)
t.FailNow()
}
_, err = conn.ExecContext(context.Background(), createTableSQL)
if err != nil {
t.Errorf("create table statement failed with %v", err)
t.FailNow()
}
defer original_conn.Exec("DROP TABLE " + tableName)
rows, err := conn.QueryContext(context.Background(), selectTableSQL, tableName)
if err != nil {
t.Errorf("select statement failed with %v", err)
t.FailNow()
}
if !rows.Next() {
t.Errorf("table %s wasn't created", tableName)
t.FailNow()
}
_, err = conn.ExecContext(context.Background(), "CREATE DATABASE IF NOT EXISTS "+newDatabaseName)
if err != nil {
t.Errorf("create database statement failed with %v", err)
t.FailNow()
}
defer system_conn.Exec("DROP DATABASE " + newDatabaseName)
_, err = conn.ExecContext(context.Background(), useDatabaseSQL+newDatabaseName)
if err != nil {
t.Errorf("use database statement failed with %v", err)
t.FailNow()
}
rows, err = conn.QueryContext(context.Background(), selectTableSQL, tableName)
if err != nil {
t.Errorf("select statement failed with %v", err)
t.FailNow()
}
if rows.Next() {
t.Errorf("use database statement didn't update the database")
t.FailNow()
}
}
func TestConnectionUseDatabaseEngine(t *testing.T) {
const createTableSQL = "CREATE TABLE IF NOT EXISTS test_use (id INT)"
const insertSQL = "INSERT INTO test_use VALUES (1)"
const insertSQL2 = "INSERT INTO test_use VALUES (2)"
conn, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("opening a connection failed unexpectedly")
t.FailNow()
}
_, err = conn.Exec(createTableSQL)
if err == nil {
t.Errorf("create table worked on a system engine without a database, while it shouldn't")
t.FailNow()
}
_, err = conn.Exec(fmt.Sprintf("USE DATABASE \"%s\"", databaseMock))
if err != nil {
t.Errorf("use database failed with %v", err)
t.FailNow()
}
_, err = conn.Exec(createTableSQL)
if err != nil {
t.Errorf("create table failed with %v", err)
t.FailNow()
}
_, err = conn.Exec(insertSQL)
if err == nil {
t.Errorf("insert worked on a system engine, while it shouldn't")
t.FailNow()
}
_, err = conn.Exec(fmt.Sprintf("USE ENGINE \"%s\"", engineNameMock))
if err != nil {
t.Errorf("use engine failed with %v", err)
t.FailNow()
}
_, err = conn.Exec(insertSQL)
if err != nil {
t.Errorf("insert failed with %v", err)
t.FailNow()
}
_, err = conn.Exec("USE ENGINE system")
if err != nil {
t.Errorf("use engine failed with %v", err)
t.FailNow()
}
_, err = conn.Exec(insertSQL2)
if err == nil {
t.Errorf("insert worked on a system engine, while it shouldn't")
t.FailNow()
}
}
func TestConnectionUppercaseNames(t *testing.T) {
systemConnection, err := sql.Open("firebolt", dsnSystemEngineMock)
if err != nil {
t.Errorf("opening a system connection failed unexpectedly %v", err)
t.FailNow()
}
engineName := engineNameMock + "_UPPERCASE"
databaseName := databaseMock + "_UPPERCASE"
_, err = systemConnection.Exec(fmt.Sprintf("CREATE DATABASE \"%s\"", databaseName))
if err != nil {
t.Errorf("creating a database failed unexpectedly %v", err)
t.FailNow()
}
defer systemConnection.Exec(fmt.Sprintf("DROP DATABASE \"%s\"", databaseName))
_, err = systemConnection.Exec(fmt.Sprintf("CREATE ENGINE \"%s\"", engineName))
if err != nil {
t.Errorf("creating an engine failed unexpectedly %v", err)
t.FailNow()
}
defer systemConnection.Exec(fmt.Sprintf("DROP ENGINE \"%s\"", engineName))
// defers run in reverse order so we stop the engine before dropping it
defer systemConnection.Exec(fmt.Sprintf("STOP ENGINE \"%s\"", engineName))
dsnUppercase := fmt.Sprintf(
"firebolt:///%s?account_name=%s&engine=%s&client_id=%s&client_secret=%s",
databaseName, accountName, engineName, clientIdMock, clientSecretMock,
)
conn, err := sql.Open("firebolt", dsnUppercase)
if err != nil {
t.Errorf("opening a connection failed unexpectedly")
t.FailNow()
}
_, err = conn.Exec("SELECT 1")
if err != nil {
t.Errorf("query failed with %v", err)
t.FailNow()
}
}
func TestConnectionPreparedStatement(t *testing.T) {
conn, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf(OPEN_CONNECTION_ERROR_MSG)
t.FailNow()
}
_, err = conn.QueryContext(
context.Background(),
"DROP TABLE IF EXISTS test_prepared_statements",
)
if err != nil {
t.Errorf("drop table statement failed with %v", err)
t.FailNow()
}
_, err = conn.QueryContext(
context.Background(),
"CREATE TABLE test_prepared_statements (i INT, l LONG, f FLOAT, d DOUBLE, t TEXT, dt DATE, ts TIMESTAMP, tstz TIMESTAMPTZ, b BOOLEAN, ba BYTEA, ge GEOGRAPHY) PRIMARY INDEX i",
)
if err != nil {
t.Errorf("create table statement failed with %v", err)
t.FailNow()
}
loc, _ := time.LoadLocation("Europe/Berlin")
d := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
ts := time.Date(2021, 1, 1, 2, 10, 20, 3000, time.UTC)
tstz := time.Date(2021, 1, 1, 2, 10, 20, 3000, loc)
ba := []byte("hello_world_123ツ\n\u0048")
ge := "POINT(1 1)"
geEncoded := "0101000020E6100000FEFFFFFFFFFFEF3F000000000000F03F"
_, err = conn.QueryContext(
context.Background(),
"INSERT INTO test_prepared_statements VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
1, int64(2), 0.333333, 0.333333333333, "text", d, ts, tstz, true, ba, ge,
)
if err != nil {
t.Errorf("insert statement failed with %v", err)
t.FailNow()
}
_, err = conn.QueryContext(context.Background(), "SET time_zone=Europe/Berlin")
if err != nil {
t.Errorf("set time_zone statement failed with %v", err)
t.FailNow()
}
rows, err := conn.QueryContext(
context.Background(),
"SELECT * FROM test_prepared_statements",
)
if err != nil {
t.Errorf("select statement failed with %v", err)
t.FailNow()
}
dest := make([]driver.Value, 11)
pointers := make([]interface{}, 11)
for i := range pointers {
pointers[i] = &dest[i]
}
assert(rows.Next(), true, t, NEXT_STATEMENT_ERROR_MSG)
if err = rows.Scan(pointers...); err != nil {
t.Errorf("firebolt rows Scan failed with %v", err)
t.FailNow()
}
assert(dest[0], int32(1), t, "int32 results are not equal")
assert(dest[1], int64(2), t, "int64 results are not equal")
// float is now alias for double so both 32 an 64 bit float values are converted to float64
assert(dest[2], 0.333333, t, "float32 results are not equal")
assert(dest[3], 0.333333333333, t, "float64 results are not equal")
assert(dest[4], "text", t, "string results are not equal")
assert(dest[5], d, t, "date results are not equal")
assert(dest[6], ts.UTC(), t, "timestamp results are not equal")
// Use .Equal to correctly compare timezones
if !dest[7].(time.Time).Equal(tstz) {
t.Errorf("timestamptz results are not equal Expected: %s Got: %s", tstz, dest[7])
}
assert(dest[8], true, t, "boolean results are not equal")
baValue := dest[9].([]byte)
if len(baValue) != len(ba) {
t.Log(string(debug.Stack()))
t.Errorf("bytea results are not equal Expected length: %d Got: %d", len(ba), len(baValue))
}
for i := range ba {
if ba[i] != baValue[i] {
t.Log(string(debug.Stack()))
t.Errorf("bytea results are not equal Expected: %s Got: %s", ba, baValue)
break
}
}
assert(dest[10], geEncoded, t, "geography results are not equal")
}
func TestConnectionQueryGeographyType(t *testing.T) {
conn, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf(OPEN_CONNECTION_ERROR_MSG)
t.FailNow()
}
rows, err := conn.QueryContext(context.TODO(), "SELECT 'POINT(1 1)'::geography")
if err != nil {
t.Errorf(STATEMENT_ERROR_MSG, err)
}
var dest string
assert(rows.Next(), true, t, NEXT_STATEMENT_ERROR_MSG)
if err = rows.Scan(&dest); err != nil {
t.Errorf(SCAN_STATEMENT_ERROR_MSG, err)
}
expected := "0101000020E6100000FEFFFFFFFFFFEF3F000000000000F03F"
if dest != expected {
t.Errorf("Geography type check failed Expected: %s Got: %s", expected, dest)
}
}
func TestConnectionQueryStructType(t *testing.T) {
setupSQL := []string{
"SET advanced_mode=1",
"SET enable_struct=1",
"SET enable_create_table_v2=true",
"SET enable_row_selection=true",
"SET prevent_create_on_information_schema=true",
"SET enable_create_table_with_struct_type=true",
"DROP TABLE IF EXISTS test_struct",
"DROP TABLE IF EXISTS test_struct_helper",
"CREATE TABLE IF NOT EXISTS test_struct(id int not null, s struct(a array(int) not null, b datetime null) not null)",
"CREATE TABLE IF NOT EXISTS test_struct_helper(a array(int) not null, b datetime null)",
"INSERT INTO test_struct_helper(a, b) VALUES ([1, 2], '2019-07-31 01:01:01')",
"INSERT INTO test_struct(id, s) SELECT 1, test_struct_helper FROM test_struct_helper",
}
tearDownSQL := []string{
"DROP TABLE IF EXISTS test_struct",
"DROP TABLE IF EXISTS test_struct_helper",
}
connection, err := sql.Open("firebolt", dsnMock)
if err != nil {
t.Errorf(OPEN_CONNECTION_ERROR_MSG)
t.FailNow()
}
for _, sql := range setupSQL {
_, err = connection.ExecContext(context.Background(), sql)
if err != nil {
t.Errorf("setup failed with %v", err)
t.FailNow()
}
}
for _, sql := range tearDownSQL {
defer connection.ExecContext(context.Background(), sql)
}
rows, err := connection.QueryContext(context.Background(), "SELECT test_struct FROM test_struct")
if err != nil {
t.Errorf(STATEMENT_ERROR_MSG, err)
t.FailNow()
}
var dest map[string]driver.Value
assert(rows.Next(), true, t, NEXT_STATEMENT_ERROR_MSG)
if err = rows.Scan(&dest); err != nil {
t.Errorf(SCAN_STATEMENT_ERROR_MSG, err)
}
assert(dest, map[string]driver.Value{
"id": int32(1),
"s": map[string]driver.Value{
"a": []driver.Value{int32(1), int32(2)},
"b": time.Date(2019, 7, 31, 1, 1, 1, 0, time.UTC),
},
}, t, "struct type check failed")
}