-
Notifications
You must be signed in to change notification settings - Fork 1
/
query_test.go
200 lines (185 loc) · 5.09 KB
/
query_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
package astra
import (
"math"
"math/big"
"net"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/shopspring/decimal"
)
func TestClient_Query_Exec_allTypes(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
c, err := createTestClient()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
id := uuid.MustParse("f066f76d-5e96-4b52-8d8a-0f51387df76b")
timeUUID := uuid.MustParse("30821634-13ad-11eb-adc1-0242ac120002")
dec, err := decimal.NewFromString("-1.23456789")
if err != nil {
t.Fatalf("unexpected error while creating decimal: %v", err)
}
vals := []interface{}{
id, // id
"alpha", // ascii_col
"bravo", // text_col
"charlie", // varchar_col
[]byte("foo"), // blob_col
true, // boolean_col
dec, // decimalvalue
2.2, // double_col
float32(3.3), // float_col
net.ParseIP("127.0.0.1"), // inet_col
1, // bigint_col
2, // int_col
3, // smallint_col
5, // tinyint_col
big.NewInt(0).Sub(big.NewInt(math.MinInt64), big.NewInt(math.MaxInt64)), // varintvalue
&timeUUID, // timeuuid_col
map[int]string{1: "a", 2: "b", 3: "c"}, // map_col
map[string][]int{"a": {1, 2}, "b": {3, 4}}, // map_list_col
[]string{"a", "b", "c"}, // list_col
[][]string{{"a", "b"}, {"c", "d"}}, // list_list_col
[]string{"a", "b", "c"}, // set_col
[]interface{}{3, "bar", float32(2.1)}, // tuple_col
}
_, err = c.Query(`INSERT INTO test.all_types (
id,
ascii_col,
text_col,
varchar_col,
blob_col,
boolean_col,
decimal_col,
double_col,
float_col,
inet_col,
bigint_col,
int_col,
smallint_col,
tinyint_col,
varint_col,
timeuuid_col,
map_col,
map_list_col,
list_col,
list_list_col,
set_col,
tuple_col
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
vals...,
).Exec()
if err != nil {
t.Fatalf("failed to insert values into table: %v", err)
}
got, err := c.Query(`
SELECT
id,
ascii_col,
text_col,
varchar_col,
blob_col,
boolean_col,
decimal_col,
double_col,
float_col,
inet_col,
bigint_col,
int_col,
smallint_col,
tinyint_col,
varint_col,
timeuuid_col,
map_col,
map_list_col,
list_col,
list_list_col,
set_col,
tuple_col
FROM test.all_types WHERE id = ?
`, id).Exec()
if err != nil {
t.Fatalf("failed to select values: %v", err)
}
wantVals := []interface{}{
id, // id
"alpha", // ascii_col
"bravo", // text_col
"charlie", // varchar_col
[]byte("foo"), // blob_col
true, // boolean_col
dec, // decimalvalue
2.2, // double_col
float32(3.3), // float_col
net.ParseIP("127.0.0.1"), // inet_col
int64(1), // bigint_col
int64(2), // int_col
int64(3), // smallint_col
int64(5), // tinyint_col
big.NewInt(0).Sub(big.NewInt(math.MinInt64), big.NewInt(math.MaxInt64)), // varintvalue
timeUUID, // timeuuid_col
map[int64]string{1: "a", 2: "b", 3: "c"}, // map_col
map[string][]int64{"a": {1, 2}, "b": {3, 4}}, // map_list_col
[]string{"a", "b", "c"}, // list_col
[][]string{{"a", "b"}, {"c", "d"}}, // list_list_col
[]string{"a", "b", "c"}, // set_col
[]interface{}{int64(3), "bar", float32(2.1)}, // tuple_col
}
if diff := cmp.Diff(wantVals, got[0].Values(), cmp.AllowUnexported(big.Int{})); diff != "" {
t.Fatalf("got[0].Values() unexpected difference (-want +got):\n%s", diff)
}
}
func TestClient_Query_Exec_emptyCollections(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
c, err := createTestClient()
if err != nil {
t.Fatalf("failed to create client: %v", err)
}
id := uuid.MustParse("f066f76d-5e96-4b52-8d8a-0f51387df76b")
vals := []interface{}{
id, // id
map[int]string{}, // map_col
[]string{}, // list_col
[]string{}, // set_col
[]interface{}{}, // tuple_col
}
_, err = c.Query(`INSERT INTO test.all_types (
id,
map_col,
list_col,
set_col,
tuple_col
) VALUES (?, ?, ?, ?, ?)`,
vals...,
).Exec()
if err != nil {
t.Fatalf("failed to insert values into table: %v", err)
}
got, err := c.Query(`
SELECT
id,
map_col,
list_col,
set_col,
tuple_col
FROM test.all_types WHERE id = ?
`, id).Exec()
if err != nil {
t.Fatalf("failed to select values: %v", err)
}
wantVals := []interface{}{
id, // id
nil, // map_col
nil, // list_col
nil, // set_col
nil, // tuple_col
}
if diff := cmp.Diff(wantVals, got[0].Values(), cmp.AllowUnexported(big.Int{})); diff != "" {
t.Fatalf("got[0].Values() unexpected difference (-want +got):\n%s", diff)
}
}