forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata_test.go
815 lines (783 loc) · 29.9 KB
/
metadata_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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
// Copyright (c) 2015 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
import (
"strconv"
"testing"
)
// Tests V1 and V2 metadata "compilation" from example data which might be returned
// from metadata schema queries (see getKeyspaceMetadata, getTableMetadata, and getColumnMetadata)
func TestCompileMetadata(t *testing.T) {
// V1 tests - these are all based on real examples from the integration test ccm cluster
keyspace := &KeyspaceMetadata{
Name: "V1Keyspace",
}
tables := []TableMetadata{
TableMetadata{
// This table, found in the system keyspace, has no key aliases or column aliases
Keyspace: "V1Keyspace",
Name: "Schema",
KeyValidator: "org.apache.cassandra.db.marshal.BytesType",
Comparator: "org.apache.cassandra.db.marshal.UTF8Type",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{},
ColumnAliases: []string{},
ValueAlias: "",
},
TableMetadata{
// This table, found in the system keyspace, has key aliases, column aliases, and a value alias.
Keyspace: "V1Keyspace",
Name: "hints",
KeyValidator: "org.apache.cassandra.db.marshal.UUIDType",
Comparator: "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.TimeUUIDType,org.apache.cassandra.db.marshal.Int32Type)",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{"target_id"},
ColumnAliases: []string{"hint_id", "message_version"},
ValueAlias: "mutation",
},
TableMetadata{
// This table, found in the system keyspace, has a comparator with collections, but no column aliases
Keyspace: "V1Keyspace",
Name: "peers",
KeyValidator: "org.apache.cassandra.db.marshal.InetAddressType",
Comparator: "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.ColumnToCollectionType(746f6b656e73:org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.UTF8Type)))",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{"peer"},
ColumnAliases: []string{},
ValueAlias: "",
},
TableMetadata{
// This table, found in the system keyspace, has a column alias, but not a composite comparator
Keyspace: "V1Keyspace",
Name: "IndexInfo",
KeyValidator: "org.apache.cassandra.db.marshal.UTF8Type",
Comparator: "org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.UTF8Type)",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{"table_name"},
ColumnAliases: []string{"index_name"},
ValueAlias: "",
},
TableMetadata{
// This table, found in the gocql_test keyspace following an integration test run, has a composite comparator with collections as well as a column alias
Keyspace: "V1Keyspace",
Name: "wiki_page",
KeyValidator: "org.apache.cassandra.db.marshal.UTF8Type",
Comparator: "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.TimeUUIDType,org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.ColumnToCollectionType(74616773:org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.UTF8Type),6174746163686d656e7473:org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.BytesType)))",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{"title"},
ColumnAliases: []string{"revid"},
ValueAlias: "",
},
TableMetadata{
// This is a made up example with multiple unnamed aliases
Keyspace: "V1Keyspace",
Name: "no_names",
KeyValidator: "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.UUIDType,org.apache.cassandra.db.marshal.UUIDType)",
Comparator: "org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.Int32Type)",
DefaultValidator: "org.apache.cassandra.db.marshal.BytesType",
KeyAliases: []string{},
ColumnAliases: []string{},
ValueAlias: "",
},
}
columns := []ColumnMetadata{
// Here are the regular columns from the peers table for testing regular columns
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "data_center", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "host_id", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UUIDType"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "rack", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "release_version", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "rpc_address", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.InetAddressType"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "schema_version", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UUIDType"},
ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "tokens", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.UTF8Type)"},
}
compileMetadata(1, keyspace, tables, columns)
assertKeyspaceMetadata(
t,
keyspace,
&KeyspaceMetadata{
Name: "V1Keyspace",
Tables: map[string]*TableMetadata{
"Schema": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "key",
Type: NativeType{typ: TypeBlob},
},
},
ClusteringColumns: []*ColumnMetadata{},
Columns: map[string]*ColumnMetadata{
"key": &ColumnMetadata{
Name: "key",
Type: NativeType{typ: TypeBlob},
Kind: PARTITION_KEY,
},
},
},
"hints": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "target_id",
Type: NativeType{typ: TypeUUID},
},
},
ClusteringColumns: []*ColumnMetadata{
&ColumnMetadata{
Name: "hint_id",
Type: NativeType{typ: TypeTimeUUID},
Order: ASC,
},
&ColumnMetadata{
Name: "message_version",
Type: NativeType{typ: TypeInt},
Order: ASC,
},
},
Columns: map[string]*ColumnMetadata{
"target_id": &ColumnMetadata{
Name: "target_id",
Type: NativeType{typ: TypeUUID},
Kind: PARTITION_KEY,
},
"hint_id": &ColumnMetadata{
Name: "hint_id",
Type: NativeType{typ: TypeTimeUUID},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"message_version": &ColumnMetadata{
Name: "message_version",
Type: NativeType{typ: TypeInt},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"mutation": &ColumnMetadata{
Name: "mutation",
Type: NativeType{typ: TypeBlob},
Kind: REGULAR,
},
},
},
"peers": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "peer",
Type: NativeType{typ: TypeInet},
},
},
ClusteringColumns: []*ColumnMetadata{},
Columns: map[string]*ColumnMetadata{
"peer": &ColumnMetadata{
Name: "peer",
Type: NativeType{typ: TypeInet},
Kind: PARTITION_KEY,
},
"data_center": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "data_center", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type", Type: NativeType{typ: TypeVarchar}},
"host_id": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "host_id", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UUIDType", Type: NativeType{typ: TypeUUID}},
"rack": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "rack", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type", Type: NativeType{typ: TypeVarchar}},
"release_version": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "release_version", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UTF8Type", Type: NativeType{typ: TypeVarchar}},
"rpc_address": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "rpc_address", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.InetAddressType", Type: NativeType{typ: TypeInet}},
"schema_version": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "schema_version", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.UUIDType", Type: NativeType{typ: TypeUUID}},
"tokens": &ColumnMetadata{Keyspace: "V1Keyspace", Table: "peers", Kind: REGULAR, Name: "tokens", ComponentIndex: 0, Validator: "org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.UTF8Type)", Type: CollectionType{NativeType: NativeType{typ: TypeSet}}},
},
},
"IndexInfo": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "table_name",
Type: NativeType{typ: TypeVarchar},
},
},
ClusteringColumns: []*ColumnMetadata{
&ColumnMetadata{
Name: "index_name",
Type: NativeType{typ: TypeVarchar},
Order: DESC,
},
},
Columns: map[string]*ColumnMetadata{
"table_name": &ColumnMetadata{
Name: "table_name",
Type: NativeType{typ: TypeVarchar},
Kind: PARTITION_KEY,
},
"index_name": &ColumnMetadata{
Name: "index_name",
Type: NativeType{typ: TypeVarchar},
Order: DESC,
Kind: CLUSTERING_KEY,
},
"value": &ColumnMetadata{
Name: "value",
Type: NativeType{typ: TypeBlob},
Kind: REGULAR,
},
},
},
"wiki_page": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "title",
Type: NativeType{typ: TypeVarchar},
},
},
ClusteringColumns: []*ColumnMetadata{
&ColumnMetadata{
Name: "revid",
Type: NativeType{typ: TypeTimeUUID},
Order: ASC,
},
},
Columns: map[string]*ColumnMetadata{
"title": &ColumnMetadata{
Name: "title",
Type: NativeType{typ: TypeVarchar},
Kind: PARTITION_KEY,
},
"revid": &ColumnMetadata{
Name: "revid",
Type: NativeType{typ: TypeTimeUUID},
Kind: CLUSTERING_KEY,
},
},
},
"no_names": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "key",
Type: NativeType{typ: TypeUUID},
},
&ColumnMetadata{
Name: "key2",
Type: NativeType{typ: TypeUUID},
},
},
ClusteringColumns: []*ColumnMetadata{
&ColumnMetadata{
Name: "column",
Type: NativeType{typ: TypeInt},
Order: ASC,
},
&ColumnMetadata{
Name: "column2",
Type: NativeType{typ: TypeInt},
Order: ASC,
},
&ColumnMetadata{
Name: "column3",
Type: NativeType{typ: TypeInt},
Order: ASC,
},
},
Columns: map[string]*ColumnMetadata{
"key": &ColumnMetadata{
Name: "key",
Type: NativeType{typ: TypeUUID},
Kind: PARTITION_KEY,
},
"key2": &ColumnMetadata{
Name: "key2",
Type: NativeType{typ: TypeUUID},
Kind: PARTITION_KEY,
},
"column": &ColumnMetadata{
Name: "column",
Type: NativeType{typ: TypeInt},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"column2": &ColumnMetadata{
Name: "column2",
Type: NativeType{typ: TypeInt},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"column3": &ColumnMetadata{
Name: "column3",
Type: NativeType{typ: TypeInt},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"value": &ColumnMetadata{
Name: "value",
Type: NativeType{typ: TypeBlob},
Kind: REGULAR,
},
},
},
},
},
)
// V2 test - V2+ protocol is simpler so here are some toy examples to verify that the mapping works
keyspace = &KeyspaceMetadata{
Name: "V2Keyspace",
}
tables = []TableMetadata{
TableMetadata{
Keyspace: "V2Keyspace",
Name: "Table1",
},
TableMetadata{
Keyspace: "V2Keyspace",
Name: "Table2",
},
}
columns = []ColumnMetadata{
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table1",
Name: "KEY1",
Kind: PARTITION_KEY,
ComponentIndex: 0,
Validator: "org.apache.cassandra.db.marshal.UTF8Type",
},
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table1",
Name: "Key1",
Kind: PARTITION_KEY,
ComponentIndex: 0,
Validator: "org.apache.cassandra.db.marshal.UTF8Type",
},
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table2",
Name: "Column1",
Kind: PARTITION_KEY,
ComponentIndex: 0,
Validator: "org.apache.cassandra.db.marshal.UTF8Type",
},
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table2",
Name: "Column2",
Kind: CLUSTERING_KEY,
ComponentIndex: 0,
Validator: "org.apache.cassandra.db.marshal.UTF8Type",
},
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table2",
Name: "Column3",
Kind: CLUSTERING_KEY,
ComponentIndex: 1,
Validator: "org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.UTF8Type)",
},
ColumnMetadata{
Keyspace: "V2Keyspace",
Table: "Table2",
Name: "Column4",
Kind: REGULAR,
Validator: "org.apache.cassandra.db.marshal.UTF8Type",
},
}
compileMetadata(2, keyspace, tables, columns)
assertKeyspaceMetadata(
t,
keyspace,
&KeyspaceMetadata{
Name: "V2Keyspace",
Tables: map[string]*TableMetadata{
"Table1": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "Key1",
Type: NativeType{typ: TypeVarchar},
},
},
ClusteringColumns: []*ColumnMetadata{},
Columns: map[string]*ColumnMetadata{
"KEY1": &ColumnMetadata{
Name: "KEY1",
Type: NativeType{typ: TypeVarchar},
Kind: PARTITION_KEY,
},
"Key1": &ColumnMetadata{
Name: "Key1",
Type: NativeType{typ: TypeVarchar},
Kind: PARTITION_KEY,
},
},
},
"Table2": &TableMetadata{
PartitionKey: []*ColumnMetadata{
&ColumnMetadata{
Name: "Column1",
Type: NativeType{typ: TypeVarchar},
},
},
ClusteringColumns: []*ColumnMetadata{
&ColumnMetadata{
Name: "Column2",
Type: NativeType{typ: TypeVarchar},
Order: ASC,
},
&ColumnMetadata{
Name: "Column3",
Type: NativeType{typ: TypeVarchar},
Order: DESC,
},
},
Columns: map[string]*ColumnMetadata{
"Column1": &ColumnMetadata{
Name: "Column1",
Type: NativeType{typ: TypeVarchar},
Kind: PARTITION_KEY,
},
"Column2": &ColumnMetadata{
Name: "Column2",
Type: NativeType{typ: TypeVarchar},
Order: ASC,
Kind: CLUSTERING_KEY,
},
"Column3": &ColumnMetadata{
Name: "Column3",
Type: NativeType{typ: TypeVarchar},
Order: DESC,
Kind: CLUSTERING_KEY,
},
"Column4": &ColumnMetadata{
Name: "Column4",
Type: NativeType{typ: TypeVarchar},
Kind: REGULAR,
},
},
},
},
},
)
}
// Helper function for asserting that actual metadata returned was as expected
func assertKeyspaceMetadata(t *testing.T, actual, expected *KeyspaceMetadata) {
if len(expected.Tables) != len(actual.Tables) {
t.Errorf("Expected len(%s.Tables) to be %v but was %v", expected.Name, len(expected.Tables), len(actual.Tables))
}
for keyT := range expected.Tables {
et := expected.Tables[keyT]
at, found := actual.Tables[keyT]
if !found {
t.Errorf("Expected %s.Tables[%s] but was not found", expected.Name, keyT)
} else {
if keyT != at.Name {
t.Errorf("Expected %s.Tables[%s].Name to be %v but was %v", expected.Name, keyT, keyT, at.Name)
}
if len(et.PartitionKey) != len(at.PartitionKey) {
t.Errorf("Expected len(%s.Tables[%s].PartitionKey) to be %v but was %v", expected.Name, keyT, len(et.PartitionKey), len(at.PartitionKey))
} else {
for i := range et.PartitionKey {
if et.PartitionKey[i].Name != at.PartitionKey[i].Name {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].Name to be '%v' but was '%v'", expected.Name, keyT, i, et.PartitionKey[i].Name, at.PartitionKey[i].Name)
}
if expected.Name != at.PartitionKey[i].Keyspace {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].Keyspace to be '%v' but was '%v'", expected.Name, keyT, i, expected.Name, at.PartitionKey[i].Keyspace)
}
if keyT != at.PartitionKey[i].Table {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].Table to be '%v' but was '%v'", expected.Name, keyT, i, keyT, at.PartitionKey[i].Table)
}
if et.PartitionKey[i].Type.Type() != at.PartitionKey[i].Type.Type() {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].Type.Type to be %v but was %v", expected.Name, keyT, i, et.PartitionKey[i].Type.Type(), at.PartitionKey[i].Type.Type())
}
if i != at.PartitionKey[i].ComponentIndex {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].ComponentIndex to be %v but was %v", expected.Name, keyT, i, i, at.PartitionKey[i].ComponentIndex)
}
if PARTITION_KEY != at.PartitionKey[i].Kind {
t.Errorf("Expected %s.Tables[%s].PartitionKey[%d].Kind to be '%v' but was '%v'", expected.Name, keyT, i, PARTITION_KEY, at.PartitionKey[i].Kind)
}
}
}
if len(et.ClusteringColumns) != len(at.ClusteringColumns) {
t.Errorf("Expected len(%s.Tables[%s].ClusteringColumns) to be %v but was %v", expected.Name, keyT, len(et.ClusteringColumns), len(at.ClusteringColumns))
} else {
for i := range et.ClusteringColumns {
if at.ClusteringColumns[i] == nil {
t.Fatalf("Unexpected nil value: %s.Tables[%s].ClusteringColumns[%d]", expected.Name, keyT, i)
}
if et.ClusteringColumns[i].Name != at.ClusteringColumns[i].Name {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Name to be '%v' but was '%v'", expected.Name, keyT, i, et.ClusteringColumns[i].Name, at.ClusteringColumns[i].Name)
}
if expected.Name != at.ClusteringColumns[i].Keyspace {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Keyspace to be '%v' but was '%v'", expected.Name, keyT, i, expected.Name, at.ClusteringColumns[i].Keyspace)
}
if keyT != at.ClusteringColumns[i].Table {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Table to be '%v' but was '%v'", expected.Name, keyT, i, keyT, at.ClusteringColumns[i].Table)
}
if et.ClusteringColumns[i].Type.Type() != at.ClusteringColumns[i].Type.Type() {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Type.Type to be %v but was %v", expected.Name, keyT, i, et.ClusteringColumns[i].Type.Type(), at.ClusteringColumns[i].Type.Type())
}
if i != at.ClusteringColumns[i].ComponentIndex {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].ComponentIndex to be %v but was %v", expected.Name, keyT, i, i, at.ClusteringColumns[i].ComponentIndex)
}
if et.ClusteringColumns[i].Order != at.ClusteringColumns[i].Order {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Order to be %v but was %v", expected.Name, keyT, i, et.ClusteringColumns[i].Order, at.ClusteringColumns[i].Order)
}
if CLUSTERING_KEY != at.ClusteringColumns[i].Kind {
t.Errorf("Expected %s.Tables[%s].ClusteringColumns[%d].Kind to be '%v' but was '%v'", expected.Name, keyT, i, CLUSTERING_KEY, at.ClusteringColumns[i].Kind)
}
}
}
if len(et.Columns) != len(at.Columns) {
eKeys := make([]string, 0, len(et.Columns))
for key := range et.Columns {
eKeys = append(eKeys, key)
}
aKeys := make([]string, 0, len(at.Columns))
for key := range at.Columns {
aKeys = append(aKeys, key)
}
t.Errorf("Expected len(%s.Tables[%s].Columns) to be %v (keys:%v) but was %v (keys:%v)", expected.Name, keyT, len(et.Columns), eKeys, len(at.Columns), aKeys)
} else {
for keyC := range et.Columns {
ec := et.Columns[keyC]
ac, found := at.Columns[keyC]
if !found {
t.Errorf("Expected %s.Tables[%s].Columns[%s] but was not found", expected.Name, keyT, keyC)
} else {
if keyC != ac.Name {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Name to be '%v' but was '%v'", expected.Name, keyT, keyC, keyC, at.Name)
}
if expected.Name != ac.Keyspace {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Keyspace to be '%v' but was '%v'", expected.Name, keyT, keyC, expected.Name, ac.Keyspace)
}
if keyT != ac.Table {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Table to be '%v' but was '%v'", expected.Name, keyT, keyC, keyT, ac.Table)
}
if ec.Type.Type() != ac.Type.Type() {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Type.Type to be %v but was %v", expected.Name, keyT, keyC, ec.Type.Type(), ac.Type.Type())
}
if ec.Order != ac.Order {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Order to be %v but was %v", expected.Name, keyT, keyC, ec.Order, ac.Order)
}
if ec.Kind != ac.Kind {
t.Errorf("Expected %s.Tables[%s].Columns[%s].Kind to be '%v' but was '%v'", expected.Name, keyT, keyC, ec.Kind, ac.Kind)
}
}
}
}
}
}
}
// Tests the cassandra type definition parser
func TestTypeParser(t *testing.T) {
// native type
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.UTF8Type",
assertTypeInfo{Type: TypeVarchar},
)
// reversed
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.UUIDType)",
assertTypeInfo{Type: TypeUUID, Reversed: true},
)
// set
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.SetType(org.apache.cassandra.db.marshal.Int32Type)",
assertTypeInfo{
Type: TypeSet,
Elem: &assertTypeInfo{Type: TypeInt},
},
)
// list
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.ListType(org.apache.cassandra.db.marshal.TimeUUIDType)",
assertTypeInfo{
Type: TypeList,
Elem: &assertTypeInfo{Type: TypeTimeUUID},
},
)
// map
assertParseNonCompositeType(
t,
" org.apache.cassandra.db.marshal.MapType( org.apache.cassandra.db.marshal.UUIDType , org.apache.cassandra.db.marshal.BytesType ) ",
assertTypeInfo{
Type: TypeMap,
Key: &assertTypeInfo{Type: TypeUUID},
Elem: &assertTypeInfo{Type: TypeBlob},
},
)
// custom
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.UserType(sandbox,61646472657373,737472656574:org.apache.cassandra.db.marshal.UTF8Type,63697479:org.apache.cassandra.db.marshal.UTF8Type,7a6970:org.apache.cassandra.db.marshal.Int32Type)",
assertTypeInfo{Type: TypeCustom, Custom: "org.apache.cassandra.db.marshal.UserType(sandbox,61646472657373,737472656574:org.apache.cassandra.db.marshal.UTF8Type,63697479:org.apache.cassandra.db.marshal.UTF8Type,7a6970:org.apache.cassandra.db.marshal.Int32Type)"},
)
assertParseNonCompositeType(
t,
"org.apache.cassandra.db.marshal.DynamicCompositeType(u=>org.apache.cassandra.db.marshal.UUIDType,d=>org.apache.cassandra.db.marshal.DateType,t=>org.apache.cassandra.db.marshal.TimeUUIDType,b=>org.apache.cassandra.db.marshal.BytesType,s=>org.apache.cassandra.db.marshal.UTF8Type,B=>org.apache.cassandra.db.marshal.BooleanType,a=>org.apache.cassandra.db.marshal.AsciiType,l=>org.apache.cassandra.db.marshal.LongType,i=>org.apache.cassandra.db.marshal.IntegerType,x=>org.apache.cassandra.db.marshal.LexicalUUIDType)",
assertTypeInfo{Type: TypeCustom, Custom: "org.apache.cassandra.db.marshal.DynamicCompositeType(u=>org.apache.cassandra.db.marshal.UUIDType,d=>org.apache.cassandra.db.marshal.DateType,t=>org.apache.cassandra.db.marshal.TimeUUIDType,b=>org.apache.cassandra.db.marshal.BytesType,s=>org.apache.cassandra.db.marshal.UTF8Type,B=>org.apache.cassandra.db.marshal.BooleanType,a=>org.apache.cassandra.db.marshal.AsciiType,l=>org.apache.cassandra.db.marshal.LongType,i=>org.apache.cassandra.db.marshal.IntegerType,x=>org.apache.cassandra.db.marshal.LexicalUUIDType)"},
)
// composite defs
assertParseCompositeType(
t,
"org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.UTF8Type)",
[]assertTypeInfo{
assertTypeInfo{Type: TypeVarchar},
},
nil,
)
assertParseCompositeType(
t,
"org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.ReversedType(org.apache.cassandra.db.marshal.DateType),org.apache.cassandra.db.marshal.UTF8Type)",
[]assertTypeInfo{
assertTypeInfo{Type: TypeTimestamp, Reversed: true},
assertTypeInfo{Type: TypeVarchar},
},
nil,
)
assertParseCompositeType(
t,
"org.apache.cassandra.db.marshal.CompositeType(org.apache.cassandra.db.marshal.UTF8Type,org.apache.cassandra.db.marshal.ColumnToCollectionType(726f77735f6d6572676564:org.apache.cassandra.db.marshal.MapType(org.apache.cassandra.db.marshal.Int32Type,org.apache.cassandra.db.marshal.LongType)))",
[]assertTypeInfo{
assertTypeInfo{Type: TypeVarchar},
},
map[string]assertTypeInfo{
"rows_merged": assertTypeInfo{
Type: TypeMap,
Key: &assertTypeInfo{Type: TypeInt},
Elem: &assertTypeInfo{Type: TypeBigInt},
},
},
)
}
// expected data holder
type assertTypeInfo struct {
Type Type
Reversed bool
Elem *assertTypeInfo
Key *assertTypeInfo
Custom string
}
// Helper function for asserting that the type parser returns the expected
// results for the given definition
func assertParseNonCompositeType(
t *testing.T,
def string,
typeExpected assertTypeInfo,
) {
result := parseType(def)
if len(result.reversed) != 1 {
t.Errorf("%s expected %d reversed values but there were %d", def, 1, len(result.reversed))
}
assertParseNonCompositeTypes(
t,
def,
[]assertTypeInfo{typeExpected},
result.types,
)
// expect no composite part of the result
if result.isComposite {
t.Errorf("%s: Expected not composite", def)
}
if result.collections != nil {
t.Errorf("%s: Expected nil collections: %v", def, result.collections)
}
}
// Helper function for asserting that the type parser returns the expected
// results for the given definition
func assertParseCompositeType(
t *testing.T,
def string,
typesExpected []assertTypeInfo,
collectionsExpected map[string]assertTypeInfo,
) {
result := parseType(def)
if len(result.reversed) != len(typesExpected) {
t.Errorf("%s expected %d reversed values but there were %d", def, len(typesExpected), len(result.reversed))
}
assertParseNonCompositeTypes(
t,
def,
typesExpected,
result.types,
)
// expect composite part of the result
if !result.isComposite {
t.Errorf("%s: Expected composite", def)
}
if result.collections == nil {
t.Errorf("%s: Expected non-nil collections: %v", def, result.collections)
}
for name, typeExpected := range collectionsExpected {
// check for an actual type for this name
typeActual, found := result.collections[name]
if !found {
t.Errorf("%s.tcollections: Expected param named %s but there wasn't", def, name)
} else {
// remove the actual from the collection so we can detect extras
delete(result.collections, name)
// check the type
assertParseNonCompositeTypes(
t,
def+"collections["+name+"]",
[]assertTypeInfo{typeExpected},
[]TypeInfo{typeActual},
)
}
}
if len(result.collections) != 0 {
t.Errorf("%s.collections: Expected no more types in collections, but there was %v", def, result.collections)
}
}
// Helper function for asserting that the type parser returns the expected
// results for the given definition
func assertParseNonCompositeTypes(
t *testing.T,
context string,
typesExpected []assertTypeInfo,
typesActual []TypeInfo,
) {
if len(typesActual) != len(typesExpected) {
t.Errorf("%s: Expected %d types, but there were %d", context, len(typesExpected), len(typesActual))
}
for i := range typesExpected {
typeExpected := typesExpected[i]
typeActual := typesActual[i]
// shadow copy the context for local modification
context := context
if len(typesExpected) > 1 {
context = context + "[" + strconv.Itoa(i) + "]"
}
// check the type
if typeActual.Type() != typeExpected.Type {
t.Errorf("%s: Expected to parse Type to %s but was %s", context, typeExpected.Type, typeActual.Type())
}
// check the custom
if typeActual.Custom() != typeExpected.Custom {
t.Errorf("%s: Expected to parse Custom %s but was %s", context, typeExpected.Custom, typeActual.Custom())
}
collection, _ := typeActual.(CollectionType)
// check the elem
if typeExpected.Elem != nil {
if collection.Elem == nil {
t.Errorf("%s: Expected to parse Elem, but was nil ", context)
} else {
assertParseNonCompositeTypes(
t,
context+".Elem",
[]assertTypeInfo{*typeExpected.Elem},
[]TypeInfo{collection.Elem},
)
}
} else if collection.Elem != nil {
t.Errorf("%s: Expected to not parse Elem, but was %+v", context, collection.Elem)
}
// check the key
if typeExpected.Key != nil {
if collection.Key == nil {
t.Errorf("%s: Expected to parse Key, but was nil ", context)
} else {
assertParseNonCompositeTypes(
t,
context+".Key",
[]assertTypeInfo{*typeExpected.Key},
[]TypeInfo{collection.Key},
)
}
} else if collection.Key != nil {
t.Errorf("%s: Expected to not parse Key, but was %+v", context, collection.Key)
}
}
}