@@ -2,6 +2,7 @@ package gpexp
2
2
3
3
import (
4
4
"reflect"
5
+ "sort"
5
6
"strconv"
6
7
"testing"
7
8
"time"
@@ -12,6 +13,139 @@ import (
12
13
13
14
// TODO test against cluster
14
15
16
+ func TestStringSliceCombos (t * testing.T ) {
17
+ client := pilosa .DefaultClient ()
18
+ schema := pilosa .NewSchema ()
19
+ idx := schema .Index ("test-string-slicecombos" )
20
+ fields := make ([]* pilosa.Field , 1 )
21
+ fields [0 ] = idx .Field ("a1" , pilosa .OptFieldKeys (true ), pilosa .OptFieldTypeSet (pilosa .CacheTypeRanked , 100 ))
22
+ err := client .SyncSchema (schema )
23
+ if err != nil {
24
+ t .Fatalf ("syncing schema: %v" , err )
25
+ }
26
+ defer func () {
27
+ err := client .DeleteIndex (idx )
28
+ if err != nil {
29
+ t .Logf ("problem cleaning up from test: %v" , err )
30
+ }
31
+ }()
32
+
33
+ b , err := NewBatch (client , 5 , idx , fields )
34
+ if err != nil {
35
+ t .Fatalf ("creating new batch: %v" , err )
36
+ }
37
+
38
+ records := []Row {
39
+ {ID : uint64 (0 ), Values : []interface {}{[]string {"a" , "b" , "c" }}},
40
+ {ID : uint64 (1 ), Values : []interface {}{[]string {"z" }}},
41
+ {ID : uint64 (2 ), Values : []interface {}{[]string {}}},
42
+ {ID : uint64 (3 ), Values : []interface {}{[]string {"q" , "r" , "s" , "t" , "c" }}},
43
+ {ID : uint64 (4 ), Values : []interface {}{nil }},
44
+ {ID : uint64 (5 ), Values : []interface {}{[]string {"a" , "b" , "c" }}},
45
+ {ID : uint64 (6 ), Values : []interface {}{[]string {"a" , "b" , "c" }}},
46
+ {ID : uint64 (7 ), Values : []interface {}{[]string {"z" }}},
47
+ {ID : uint64 (8 ), Values : []interface {}{[]string {}}},
48
+ {ID : uint64 (9 ), Values : []interface {}{[]string {"q" , "r" , "s" , "t" }}},
49
+ {ID : uint64 (10 ), Values : []interface {}{nil }},
50
+ {ID : uint64 (11 ), Values : []interface {}{[]string {"a" , "b" , "c" }}},
51
+ }
52
+
53
+ err = ingestRecords (records , b )
54
+ if err != nil {
55
+ t .Fatalf ("importing: %v" , err )
56
+ }
57
+
58
+ a1 := fields [0 ]
59
+
60
+ result := tq (t , client , a1 .TopN (10 ))
61
+ rez := sortableCRI (result .CountItems ())
62
+ sort .Sort (rez )
63
+ exp := sortableCRI {
64
+ {Key : "a" , Count : 4 },
65
+ {Key : "b" , Count : 4 },
66
+ {Key : "c" , Count : 5 },
67
+ {Key : "q" , Count : 2 },
68
+ {Key : "r" , Count : 2 },
69
+ {Key : "s" , Count : 2 },
70
+ {Key : "t" , Count : 2 },
71
+ {Key : "z" , Count : 2 },
72
+ }
73
+ sort .Sort (exp )
74
+ errorIfNotEqual (t , exp , rez )
75
+
76
+ result = tq (t , client , a1 .Row ("a" ))
77
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {0 , 5 , 6 , 11 })
78
+ result = tq (t , client , a1 .Row ("b" ))
79
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {0 , 5 , 6 , 11 })
80
+ result = tq (t , client , a1 .Row ("c" ))
81
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {0 , 3 , 5 , 6 , 11 })
82
+ result = tq (t , client , a1 .Row ("z" ))
83
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {1 , 7 })
84
+ result = tq (t , client , a1 .Row ("q" ))
85
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {3 , 9 })
86
+ result = tq (t , client , a1 .Row ("r" ))
87
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {3 , 9 })
88
+ result = tq (t , client , a1 .Row ("s" ))
89
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {3 , 9 })
90
+ result = tq (t , client , a1 .Row ("t" ))
91
+ errorIfNotEqual (t , result .Row ().Columns , []uint64 {3 , 9 })
92
+ }
93
+
94
+ func errorIfNotEqual (t * testing.T , exp , got interface {}) {
95
+ t .Helper ()
96
+ if ! reflect .DeepEqual (exp , got ) {
97
+ t .Errorf ("unequal exp/got:\n %v\n %v" , exp , got )
98
+ }
99
+ }
100
+
101
+ type sortableCRI []pilosa.CountResultItem
102
+
103
+ func (s sortableCRI ) Len () int { return len (s ) }
104
+ func (s sortableCRI ) Less (i , j int ) bool {
105
+ if s [i ].Count != s [j ].Count {
106
+ return s [i ].Count > s [j ].Count
107
+ }
108
+ if s [i ].ID != s [j ].ID {
109
+ return s [i ].ID < s [j ].ID
110
+ }
111
+ if s [i ].Key != s [j ].Key {
112
+ return s [i ].Key < s [j ].Key
113
+ }
114
+ return true
115
+ }
116
+ func (s sortableCRI ) Swap (i , j int ) {
117
+ s [i ], s [j ] = s [j ], s [i ]
118
+ }
119
+
120
+ func tq (t * testing.T , client * pilosa.Client , query pilosa.PQLQuery ) pilosa.QueryResult {
121
+ resp , err := client .Query (query )
122
+ if err != nil {
123
+ t .Fatalf ("querying: %v" , err )
124
+ }
125
+ return resp .Results ()[0 ]
126
+ }
127
+
128
+ func ingestRecords (records []Row , batch * Batch ) error {
129
+ for _ , rec := range records {
130
+ err := batch .Add (rec )
131
+ if err == ErrBatchNowFull {
132
+ err = batch .Import ()
133
+ if err != nil {
134
+ return errors .Wrap (err , "importing batch" )
135
+ }
136
+ } else if err != nil {
137
+ return errors .Wrap (err , "while adding record" )
138
+ }
139
+ }
140
+ if batch .Len () > 0 {
141
+ err := batch .Import ()
142
+ if err != nil {
143
+ return errors .Wrap (err , "importing batch" )
144
+ }
145
+ }
146
+ return nil
147
+ }
148
+
15
149
func TestImportBatchInts (t * testing.T ) {
16
150
client := pilosa .DefaultClient ()
17
151
schema := pilosa .NewSchema ()
0 commit comments