-
Notifications
You must be signed in to change notification settings - Fork 7
/
query_test.go
210 lines (183 loc) · 4.63 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
201
202
203
204
205
206
207
208
209
210
package riaken_core
import (
"regexp"
"testing"
"github.com/riaken/riaken-core/rpb"
)
// Example from http://docs.basho.com/riak/latest/dev/using/mapreduce/
func TestQueryMapReduce(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Test Data
bucket := session.GetBucket("training")
object1 := bucket.Object("foo")
if _, err := object1.Store([]byte("pizza data goes here")); err != nil {
t.Error(err.Error())
}
object2 := bucket.Object("bar")
if _, err := object2.Store([]byte("pizza pizza pizza pizza")); err != nil {
t.Error(err.Error())
}
object3 := bucket.Object("baz")
if _, err := object3.Store([]byte("nothing to see here")); err != nil {
t.Error(err.Error())
}
object4 := bucket.Object("bam")
if _, err := object4.Store([]byte("pizza pizza pizza")); err != nil {
t.Error(err.Error())
}
request := []byte(`{
"inputs":"training",
"query":[{"map":{"language":"javascript",
"source":"function(riakObject) {
var val = riakObject.values[0].data.match(/pizza/g);
return [[riakObject.key, (val ? val.length : 0 )]];
}"}}]}`)
contentType := []byte("application/json")
// Query
query := session.Query()
var result []byte
// Loop until done is received from Riak.
for out, err := query.MapReduce(request, contentType); !out.GetDone(); out, err = query.MapReduce(request, contentType) {
if err != nil {
t.Error(err.Error())
break
}
result = append(result, out.GetResponse()...)
}
// [["foo",1],["baz",0],["bar",4],["bam",3]]
m, err := regexp.MatchString(`["foo",1]`, string(result))
if err != nil {
t.Error(err.Error())
}
if !m {
t.Error("Mismatched foo result")
}
m, err = regexp.MatchString(`["bar",4]`, string(result))
if err != nil {
t.Error(err.Error())
}
if !m {
t.Error("Mismatched bar result")
}
m, err = regexp.MatchString(`["baz",0]`, string(result))
if err != nil {
t.Error(err.Error())
}
if !m {
t.Error("Mismatched baz result")
}
m, err = regexp.MatchString(`["bam",3]`, string(result))
if err != nil {
t.Error(err.Error())
}
if !m {
t.Error("Mismatched bam result")
}
// Cleanup
if _, err := object1.Delete(); err != nil {
t.Error(err.Error())
}
if _, err := object2.Delete(); err != nil {
t.Error(err.Error())
}
if _, err := object3.Delete(); err != nil {
t.Error(err.Error())
}
if _, err := object4.Delete(); err != nil {
t.Error(err.Error())
}
}
func TestQuerySecondaryIndexes(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Setup
bucket := session.GetBucket("b1")
object := bucket.Object("o1")
opts := &rpb.RpbPutReq{
Content: &rpb.RpbContent{
Indexes: []*rpb.RpbPair{
&rpb.RpbPair{
Key: []byte("animal_bin"),
Value: []byte("chicken"),
},
},
},
}
if _, err := object.Do(opts).Store([]byte("o1-data")); err != nil {
t.Error(err.Error())
}
// Query
query := session.Query()
data, err := query.SecondaryIndexes([]byte("b1"), []byte("animal_bin"), []byte("chicken"), nil, nil, 0, nil)
if err != nil {
t.Error(err.Error())
}
if len(data.GetKeys()) == 0 {
t.Error("expected results")
} else {
if string(data.GetKeys()[0]) != "o1" {
t.Error("expected first key to be o1")
}
}
// Cleanup
if _, err := object.Delete(); err != nil {
t.Error(err.Error())
}
}
func TestQuerySearch(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Setup
bucket := session.GetBucket("b3")
object := bucket.Object("o1")
object.ContentType([]byte("application/json"))
if _, err := object.Store([]byte(`{"food": "pizza"}`)); err != nil {
t.Error(err.Error())
}
// Query
query := session.Query()
data, err := query.Search([]byte("b3"), []byte("food:pizza"))
if err != nil {
t.Error(err.Error())
}
if data.GetNumFound() == 0 {
t.Error("expected results")
}
// Cleanup
if _, err := object.Delete(); err != nil {
t.Error(err.Error())
}
}
func TestQuerySearchCompound(t *testing.T) {
client := dial()
defer client.Close()
session := client.Session()
defer session.Release()
// Setup
bucket := session.GetBucket("b3")
object := bucket.Object("o2")
object.ContentType([]byte("application/json"))
if _, err := object.Store([]byte(`{"food": "pizza", "drink": "beer wine whiskey"}`)); err != nil {
t.Error(err.Error())
}
// Query
query := session.Query()
data, err := query.Search([]byte("b3"), []byte("food:pizza AND drink:beer"))
if err != nil {
t.Error(err.Error())
}
if data.GetNumFound() == 0 {
t.Error("expected results")
}
// Cleanup
if _, err := object.Delete(); err != nil {
t.Error(err.Error())
}
}