-
Notifications
You must be signed in to change notification settings - Fork 69
/
table_test.go
363 lines (331 loc) · 8.7 KB
/
table_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
package gocassa
import (
"fmt"
"math/rand"
"strings"
"testing"
"time"
"github.com/gocql/gocql"
)
func createIf(cs TableChanger, tes *testing.T) {
err := cs.(TableChanger).Recreate()
if err != nil {
tes.Fatal(err)
}
}
// cqlsh> CREATE TABLE test.customer1 (id text, name text, PRIMARY KEY((id, name)));
func TestTables(t *testing.T) {
res, err := ns.(*k).Tables()
if err != nil {
t.Fatal(err)
}
if len(res) == 0 {
t.Fatal("Not found ", len(res))
}
}
func TestCreateTable(t *testing.T) {
rand.Seed(time.Now().Unix())
randy := rand.Int() % 100
name := fmt.Sprintf("customer_%v", randy)
cs := ns.Table(name, Customer{}, Keys{
PartitionKeys: []string{"Id", "Name"},
})
createIf(cs, t)
validateTableName(t, cs.(TableChanger), fmt.Sprintf("customer_%d__Id_Name__", randy))
err := cs.Set(Customer{
Id: "1001",
Name: "Joe",
}).Run()
if err != nil {
t.Fatal(err)
}
res := &[]Customer{}
err = cs.Where(Eq("Id", "1001"), Eq("Name", "Joe")).Read(res).Run()
if err != nil {
t.Fatal(err)
}
if len(*res) != 1 {
t.Fatal("Not found ", len(*res))
}
err = ns.(*k).DropTable(name)
if err != nil {
t.Fatal(err)
}
}
func TestClusteringOrder(t *testing.T) {
options := Options{}.AppendClusteringOrder("Id", DESC)
name := "customer_by_name"
cs := ns.Table(name, Customer{}, Keys{
PartitionKeys: []string{"Name"},
ClusteringColumns: []string{"Id"},
}).WithOptions(options)
createIf(cs, t)
validateTableName(t, cs.(TableChanger), "customer_by_name__Name__Id")
customers := []Customer{
Customer{
Id: "1001",
Name: "Brian",
},
Customer{
Id: "1002",
Name: "Adam",
},
Customer{
Id: "1003",
Name: "Brian",
},
Customer{
Id: "1004",
Name: "Brian",
},
}
for _, c := range customers {
err := cs.Set(c).Run()
if err != nil {
t.Fatal(err)
}
}
res := &[]Customer{}
err := cs.Where(Eq("Name", "Brian")).Read(res).Run()
if err != nil {
t.Fatal(err)
}
expected := []string{"1004", "1003", "1001"}
if len(*res) != len(expected) {
t.Fatal("Expected", len(*res), " results, got", len(*res))
}
for i, id := range expected {
if (*res)[i].Id != id {
t.Fatal("Got result out of order. i:", i, "expected ID:", id, "actual ID:", (*res)[i].Id)
}
}
}
func TestClusteringOrderMultipl(t *testing.T) {
options := Options{}.AppendClusteringOrder("Tag", DESC).AppendClusteringOrder("Id", DESC)
name := "customer_by_name2"
cs := ns.Table(name, Customer2{}, Keys{
PartitionKeys: []string{"Name"},
ClusteringColumns: []string{"Tag", "Id"},
}).WithOptions(options)
createIf(cs, t)
customers := []Customer2{
Customer2{
Id: "1001",
Name: "Brian",
Tag: "B",
},
Customer2{
Id: "1002",
Name: "Adam",
Tag: "A",
},
Customer2{
Id: "1003",
Name: "Brian",
Tag: "A",
},
Customer2{
Id: "1004",
Name: "Brian",
Tag: "B",
},
}
for _, c := range customers {
err := cs.Set(c).Run()
if err != nil {
t.Fatal(err)
}
}
res := &[]Customer2{}
err := cs.Where(Eq("Name", "Brian")).Read(res).Run()
if err != nil {
t.Fatal(err)
}
expected := []struct {
Tag string
Id string
}{
{"B", "1004"},
{"B", "1001"},
{"A", "1003"},
}
if len(*res) != len(expected) {
t.Fatal("Expected", len(*res), " results, got", len(*res))
}
for i, e := range expected {
result := (*res)[i]
if result.Id != e.Id || result.Tag != e.Tag {
t.Fatal("Got result out of order. i:", i, "expected ID:", e.Id, "actual ID:", result.Id, "expected tag:", e.Tag, "actual tag:", result.Tag)
}
}
}
func TestCreateStatement(t *testing.T) {
cs := ns.Table("something", Customer{}, Keys{
PartitionKeys: []string{"Id", "Name"},
})
str, err := cs.CreateStatement()
validateTableName(t, cs.(TableChanger), "something__Id_Name__")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(str, "something") {
t.Fatal(str)
}
str, err = cs.WithOptions(Options{TableName: "funky"}).CreateStatement()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(str, "funky") {
t.Fatal(str)
}
// if clustering order is not specified, it should omit the clustering order by and use the default
if strings.Contains(str, "WITH CLUSTERING ORDER BY") {
t.Fatal(str)
}
}
func TestAllowFiltering(t *testing.T) {
name := "allow_filtering"
cs := ns.Table(name, Customer2{}, Keys{
PartitionKeys: []string{"Name"},
ClusteringColumns: []string{"Tag", "Id"},
})
createIf(cs, t)
c2 := Customer2{}
//This shouldn't contain allow filtering
st, _ := cs.Where(Eq("Name", "Brian")).Read(&c2).GenerateStatement()
if strings.Contains(st, "ALLOW FILTERING") {
t.Error("Allow filtering should be disabled by default")
}
op := Options{AllowFiltering: true}
stAllow, _ := cs.Where(Eq("", "")).Read(&c2).WithOptions(op).GenerateStatement()
if !strings.Contains(stAllow, "ALLOW FILTERING") {
t.Error("Allow filtering show be included in the statement")
}
}
func TestKeysCreation(t *testing.T) {
cs := ns.Table("composite_keys", Customer{}, Keys{
PartitionKeys: []string{"Id", "Name"},
})
str, err := cs.CreateStatement()
if err != nil {
t.Fatal(err)
}
//composite
if !strings.Contains(str, "PRIMARY KEY ((id, name ))") {
t.Fatal(str)
}
cs = ns.Table("compound_keys", Customer{}, Keys{
PartitionKeys: []string{"Id", "Name"},
Compound: true,
})
str, err = cs.CreateStatement()
if err != nil {
t.Fatal(err)
}
//compound
if !strings.Contains(str, "PRIMARY KEY (id, name )") {
t.Fatal(str)
}
cs = ns.Table("clustering_keys", Customer{}, Keys{
PartitionKeys: []string{"Id"},
ClusteringColumns: []string{"Name"},
})
str, err = cs.CreateStatement()
if err != nil {
t.Fatal(err)
}
//with columns
if !strings.Contains(str, "PRIMARY KEY ((id), name)") {
t.Fatal(str)
}
//compound gets ignored when using clustering columns
cs = ns.Table("clustering_keys", Customer{}, Keys{
PartitionKeys: []string{"Id"},
ClusteringColumns: []string{"Name"},
Compound: true,
})
str, err = cs.CreateStatement()
if err != nil {
t.Fatal(err)
}
//with columns
if !strings.Contains(str, "PRIMARY KEY ((id), name)") {
t.Fatal(str)
}
}
// Mock QueryExecutor that keeps track of options passed to it
type OptionCheckingQE struct {
opts *Options
}
func (qe OptionCheckingQE) QueryWithOptions(opts Options, stmt string, params ...interface{}) ([]map[string]interface{}, error) {
qe.opts.Consistency = opts.Consistency
return []map[string]interface{}{}, nil
}
func (qe OptionCheckingQE) Query(stmt string, params ...interface{}) ([]map[string]interface{}, error) {
return qe.QueryWithOptions(Options{}, stmt, params...)
}
func (qe OptionCheckingQE) ExecuteWithOptions(opts Options, stmt string, params ...interface{}) error {
qe.opts.Consistency = opts.Consistency
return nil
}
func (qe OptionCheckingQE) Execute(stmt string, params ...interface{}) error {
return qe.ExecuteWithOptions(Options{}, stmt, params...)
}
func (qe OptionCheckingQE) ExecuteAtomically(stmt []string, params [][]interface{}) error {
return nil
}
func (qe OptionCheckingQE) Close() {
}
func TestQueryWithConsistency(t *testing.T) {
// It's tricky to verify this against a live DB, so mock out the
// query executor and make sure the right options get passed
// through
resultOpts := Options{}
qe := OptionCheckingQE{opts: &resultOpts}
conn := &connection{q: qe}
ks := conn.KeySpace("some ks")
cs := ks.Table("customerWithConsistency", Customer{}, Keys{PartitionKeys: []string{"Id"}})
res := &[]Customer{}
cons := gocql.Quorum
opts := Options{Consistency: &cons}
q := cs.Where(Eq("Id", 1)).Read(res).WithOptions(opts)
if err := q.Run(); err != nil {
t.Fatal(err)
}
if resultOpts.Consistency == nil {
t.Fatal(fmt.Sprint("Expected consistency:", cons, "got: nil"))
}
if resultOpts.Consistency != nil && *resultOpts.Consistency != cons {
t.Fatal(fmt.Sprint("Expected consistency:", cons, "got:", resultOpts.Consistency))
}
}
func TestExecuteWithConsistency(t *testing.T) {
resultOpts := Options{}
qe := OptionCheckingQE{opts: &resultOpts}
conn := &connection{q: qe}
ks := conn.KeySpace("some ks")
cs := ks.Table("customerWithConsistency2", Customer{}, Keys{PartitionKeys: []string{"Id"}})
cons := gocql.All
opts := Options{Consistency: &cons}
// This calls Execute() under the covers
err := cs.Set(Customer{
Id: "100",
Name: "Joe",
}).WithOptions(opts).Run()
if err != nil {
t.Fatal(err)
}
if resultOpts.Consistency == nil {
t.Fatal(fmt.Sprint("Expected consistency:", cons, "got: nil"))
}
if resultOpts.Consistency != nil && *resultOpts.Consistency != cons {
t.Fatal(fmt.Sprint("Expected consistency:", cons, "got:", resultOpts.Consistency))
}
}
func validateTableName(t *testing.T, tbl TableChanger, expected string) bool {
ok := tbl.Name() == expected
if !ok {
t.Fatalf("Table name should be: %s and NOT: %s", expected, tbl.Name())
}
return ok
}