This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
query_test.go
264 lines (238 loc) · 5.86 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
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
package main
import (
"io/ioutil"
"math"
"reflect"
"testing"
"time"
"github.com/mschoch/gouchstore"
)
var testInput = []interface{}{}
var nextValue = "29"
var bigInput []byte
func init() {
s := []interface{}{"31", "63", "foo", "17",
"foo", "foo", "foo", "foo",
map[string]interface{}{"key": "value1"},
map[string]interface{}{"key": "value2"},
map[string]interface{}{"key": "value3"}}
for i := range s {
testInput = append(testInput, s[i])
}
var err error
bigInput, err = ioutil.ReadFile("sample.json")
if err != nil {
panic("Couldn't read sample.json")
}
}
func streamCollection(s []interface{}) chan ptrval {
ch := make(chan ptrval)
go func() {
defer close(ch)
t := time.Unix(1347255646, 418514126).UTC()
for _, r := range s {
t = t.Add(time.Second)
ts := t.Format(time.RFC3339Nano)
ch <- ptrval{gouchstore.NewDocumentInfo(ts), r, true}
}
t = t.Add(time.Second)
ts := t.Format(time.RFC3339Nano)
ch <- ptrval{gouchstore.NewDocumentInfo(ts), nextValue, false}
}()
return ch
}
func TestEmptyRateConversion(t *testing.T) {
ch := make(chan ptrval)
rch := convertTofloat64Rate(ch)
close(ch)
val, got := <-rch
if got {
t.Fatalf("Expected empty channel, got %v", val)
}
}
func TestSingleRateConversion(t *testing.T) {
ch := make(chan ptrval, 1)
rch := convertTofloat64Rate(ch)
ch <- ptrval{nil, &nextValue, true}
close(ch)
val, got := <-rch
if got {
t.Fatalf("Expected empty channel, got %v", val)
}
}
func TestPairRateConversion(t *testing.T) {
ch := make(chan ptrval, 2)
rch := convertTofloat64Rate(ch)
tm := time.Now().UTC()
val1 := "20"
ch <- ptrval{gouchstore.NewDocumentInfo(tm.Format(time.RFC3339Nano)),
val1, true}
tm = tm.Add(5 * time.Second)
val2 := "25"
ch <- ptrval{gouchstore.NewDocumentInfo(tm.Format(time.RFC3339Nano)),
val2, false}
close(ch)
exp := 1.0
val, got := <-rch
if !got {
t.Fatalf("Expected value, got empty channel")
}
if val != exp {
t.Fatalf("Expected %v, got %v", exp, val)
}
}
func TestReducers(t *testing.T) {
tests := []struct {
reducer string
exp interface{}
}{
{"any", "31"},
{"count", 11},
{"sum", float64(111)},
{"sumsq", float64(5219)},
{"max", float64(63)},
{"min", float64(17)},
{"avg", float64(37)},
{"c", float64(10.5)},
{"c_min", float64(-23)},
{"c_avg", float64(3.5)},
{"c_max", float64(32)},
{"identity", testInput},
{"obj_keys", []string{"key", "key", "key"}},
{"obj_distinct_keys", []string{"key"}},
// distinct reducer tested separately
}
for _, test := range tests {
got := reducers[test.reducer](streamCollection(testInput))
if !reflect.DeepEqual(got, test.exp) {
t.Errorf("Expected %v for %v, got %v",
test.exp, test.reducer, got)
t.Fail()
}
}
}
// the order of items returned by distinct is not gauaranteed
// which makes using TestReducers problematic as is
// also numeric values become strings do to some interal behavior
// this could change over time, for now its primarily intended
// to work with string values
func TestDistinctReducer(t *testing.T) {
test := struct {
reducer string
exp []interface{}
}{"distinct", []interface{}{"foo", "17", "31", "63"}}
got := reducers[test.reducer](streamCollection(testInput)).([]interface{})
if len(got) != len(test.exp) {
t.Errorf("Expected length of result to be %v for %v, got %v",
len(test.exp), test.reducer, len(got))
}
OUTER:
for _, expval := range test.exp {
for _, gotval := range got {
if reflect.DeepEqual(expval, gotval) {
continue OUTER
}
}
t.Errorf("Expected result to contain %v for %v, got %v",
expval, test.reducer, got)
}
}
func TestEmptyReducers(t *testing.T) {
emptyInput := []interface{}{}
tests := []struct {
reducer string
exp interface{}
}{
{"any", nil},
{"count", 0},
{"sum", 0.0},
{"sumsq", 0.0},
{"max", math.NaN()},
{"min", math.NaN()},
{"avg", math.NaN()},
{"c", 0.0},
{"c_min", math.NaN()},
{"c_avg", math.NaN()},
{"c_max", math.NaN()},
{"identity", emptyInput},
{"obj_keys", []string{}},
{"obj_distinct_keys", []string{}},
{"distinct", emptyInput},
}
eq := func(a, b interface{}) bool {
if !reflect.DeepEqual(a, b) {
af, aok := a.(float64)
bf, bok := b.(float64)
return aok && bok && (math.IsNaN(af) == math.IsNaN(bf))
}
return true
}
for _, test := range tests {
got := reducers[test.reducer](streamCollection(emptyInput))
if !eq(got, test.exp) {
t.Errorf("Expected %v for %v, got %v",
test.exp, test.reducer, got)
t.Fail()
}
}
}
func TestNilReducers(t *testing.T) {
emptyInput := []interface{}{nil}
tests := []struct {
reducer string
exp interface{}
}{
{"any", nil},
{"count", 0},
{"sum", 0.0},
{"sumsq", 0.0},
{"max", math.NaN()},
{"min", math.NaN()},
{"avg", math.NaN()},
{"c", 0.0},
{"c_min", math.NaN()},
{"c_avg", math.NaN()},
{"c_max", math.NaN()},
{"identity", emptyInput},
{"obj_keys", []string{}},
{"obj_distinct_keys", []string{}},
{"distinct", emptyInput},
}
eq := func(a, b interface{}) bool {
if !reflect.DeepEqual(a, b) {
af, aok := a.(float64)
bf, bok := b.(float64)
return aok && bok && (math.IsNaN(af) == math.IsNaN(bf))
}
return true
}
for _, test := range tests {
got := reducers[test.reducer](streamCollection(emptyInput))
if !eq(got, test.exp) {
t.Errorf("Expected %v for %v, got %v",
test.exp, test.reducer, got)
t.Fail()
}
}
}
func TestPointers(t *testing.T) {
docID := "2013-02-22T16:29:19.750264Z"
di := gouchstore.NewDocumentInfo(docID)
tests := []struct {
pointer string
exp interface{}
}{
{"/kind", "Listing"},
{"_id", docID},
}
for _, test := range tests {
chans := make([]chan ptrval, 0, 1)
chans = append(chans, make(chan ptrval))
go processDoc(di, chans, bigInput, []string{test.pointer}, []string{}, []string{}, true)
got := <-chans[0]
if test.exp != got.val {
t.Errorf("Expected %v for %v, got %v",
test.exp, test.pointer, got.val)
}
}
}