forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tuple_test.go
285 lines (241 loc) · 6.61 KB
/
tuple_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
// +build all integration
package gocql
import (
"reflect"
"testing"
)
func TestTupleSimple(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_test(
id int,
coord frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
err = session.Query("INSERT INTO tuple_test(id, coord) VALUES(?, (?, ?))", 1, 100, -100).Exec()
if err != nil {
t.Fatal(err)
}
var (
id int
coord struct {
x int
y int
}
)
iter := session.Query("SELECT id, coord FROM tuple_test WHERE id=?", 1)
if err := iter.Scan(&id, &coord.x, &coord.y); err != nil {
t.Fatal(err)
}
if id != 1 {
t.Errorf("expected to get id=1 got: %v", id)
} else if coord.x != 100 {
t.Errorf("expected to get coord.x=100 got: %v", coord.x)
} else if coord.y != -100 {
t.Errorf("expected to get coord.y=-100 got: %v", coord.y)
}
}
func TestTuple_NullTuple(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_nil_test(
id int,
coord frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
const id = 1
err = session.Query("INSERT INTO tuple_nil_test(id, coord) VALUES(?, (?, ?))", id, nil, nil).Exec()
if err != nil {
t.Fatal(err)
}
x := new(int)
y := new(int)
iter := session.Query("SELECT coord FROM tuple_nil_test WHERE id=?", id)
if err := iter.Scan(&x, &y); err != nil {
t.Fatal(err)
}
if x != nil {
t.Fatalf("should be nil got %+#v", x)
} else if y != nil {
t.Fatalf("should be nil got %+#v", y)
}
}
func TestTuple_TupleNotSet(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_not_set_test(
id int,
coord frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
const id = 1
err = session.Query("INSERT INTO tuple_not_set_test(id,coord) VALUES(?, (?,?))", id, 1, 2).Exec()
if err != nil {
t.Fatal(err)
}
err = session.Query("INSERT INTO tuple_not_set_test(id) VALUES(?)", id+1).Exec()
if err != nil {
t.Fatal(err)
}
x := new(int)
y := new(int)
iter := session.Query("SELECT coord FROM tuple_not_set_test WHERE id=?", id)
if err := iter.Scan(x, y); err != nil {
t.Fatal(err)
}
if x == nil || *x != 1 {
t.Fatalf("x should be %d got %+#v, value=%d", 1, x, *x)
}
if y == nil || *y != 2 {
t.Fatalf("y should be %d got %+#v, value=%d", 2, y, *y)
}
// Check if the supplied targets are reset to nil
iter = session.Query("SELECT coord FROM tuple_not_set_test WHERE id=?", id+1)
if err := iter.Scan(x, y); err != nil {
t.Fatal(err)
}
if x == nil || *x != 0 {
t.Fatalf("x should be %d got %+#v, value=%d", 0, x, *x)
}
if y == nil || *y != 0 {
t.Fatalf("y should be %d got %+#v, value=%d", 0, y, *y)
}
}
func TestTupleMapScan(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_map_scan(
id int,
val frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
if err := session.Query(`INSERT INTO tuple_map_scan (id, val) VALUES (1, (1, 2));`).Exec(); err != nil {
t.Fatal(err)
}
m := make(map[string]interface{})
err = session.Query(`SELECT * FROM tuple_map_scan`).MapScan(m)
if err != nil {
t.Fatal(err)
}
if m["val[0]"] != 1 {
t.Fatalf("expacted val[0] to be %d but was %d", 1, m["val[0]"])
}
if m["val[1]"] != 2 {
t.Fatalf("expacted val[1] to be %d but was %d", 2, m["val[1]"])
}
}
func TestTupleMapScanNil(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_map_scan_nil(
id int,
val frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
if err := session.Query(`INSERT INTO tuple_map_scan_nil (id, val) VALUES (?,(?,?));`, 1, nil, nil).Exec(); err != nil {
t.Fatal(err)
}
m := make(map[string]interface{})
err = session.Query(`SELECT * FROM tuple_map_scan_nil`).MapScan(m)
if err != nil {
t.Fatal(err)
}
if m["val[0]"] != 0 {
t.Fatalf("expacted val[0] to be %d but was %d", 0, m["val[0]"])
}
if m["val[1]"] != 0 {
t.Fatalf("expacted val[1] to be %d but was %d", 0, m["val[1]"])
}
}
func TestTupleMapScanNotSet(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.tuple_map_scan_not_set(
id int,
val frozen<tuple<int, int>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
if err := session.Query(`INSERT INTO tuple_map_scan_not_set (id) VALUES (?);`, 1).Exec(); err != nil {
t.Fatal(err)
}
m := make(map[string]interface{})
err = session.Query(`SELECT * FROM tuple_map_scan_not_set`).MapScan(m)
if err != nil {
t.Fatal(err)
}
if m["val[0]"] != 0 {
t.Fatalf("expacted val[0] to be %d but was %d", 0, m["val[0]"])
}
if m["val[1]"] != 0 {
t.Fatalf("expacted val[1] to be %d but was %d", 0, m["val[1]"])
}
}
func TestTuple_NestedCollection(t *testing.T) {
session := createSession(t)
defer session.Close()
if session.cfg.ProtoVersion < protoVersion3 {
t.Skip("tuple types are only available of proto>=3")
}
err := createTable(session, `CREATE TABLE gocql_test.nested_tuples(
id int,
val list<frozen<tuple<int, text>>>,
primary key(id))`)
if err != nil {
t.Fatal(err)
}
type typ struct {
A int
B string
}
tests := []struct {
name string
val interface{}
}{
{name: "slice", val: [][]interface{}{{1, "2"}, {3, "4"}}},
{name: "array", val: [][2]interface{}{{1, "2"}, {3, "4"}}},
{name: "struct", val: []typ{{1, "2"}, {3, "4"}}},
}
for i, test := range tests {
t.Run(test.name, func(t *testing.T) {
if err := session.Query(`INSERT INTO nested_tuples (id, val) VALUES (?, ?);`, i, test.val).Exec(); err != nil {
t.Fatal(err)
}
rv := reflect.ValueOf(test.val)
res := reflect.New(rv.Type()).Elem().Addr().Interface()
err = session.Query(`SELECT val FROM nested_tuples WHERE id=?`, i).Scan(res)
if err != nil {
t.Fatal(err)
}
})
}
}