-
Notifications
You must be signed in to change notification settings - Fork 7
/
filter_test.go
281 lines (224 loc) · 5.86 KB
/
filter_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
package cete
import (
"errors"
"io/ioutil"
"os"
"sync/atomic"
"testing"
"time"
)
func TestDo(t *testing.T) {
if testing.Short() {
t.Parallel()
}
people := map[string]Person{
"jason": {
Name: "Jason",
City: "Sydney",
Age: 17,
Height: 1.76,
DOB: time.Date(1999, 1, 28, 01, 01, 01, 01, time.UTC),
},
"ben": {
Name: "Ben",
City: "Melbourne",
Age: 19,
Height: 1.83,
DOB: time.Date(1998, 5, 23, 01, 01, 01, 01, time.UTC),
},
"drew": {
Name: "Drew",
City: "London",
Age: 18,
Height: 1.72,
DOB: time.Date(2001, 7, 13, 01, 01, 01, 01, time.UTC),
},
}
dir, err := ioutil.TempDir("", "cete_")
panicNotNil(err)
t.Log("testing directory:", dir)
defer func() {
if !t.Failed() {
os.RemoveAll(dir)
}
}()
db, err := Open(dir + "/data")
panicNotNil(err)
defer func() {
db.Close()
}()
panicNotNil(db.NewTable("do_testing"))
for name, person := range people {
panicNotNil(db.Table("do_testing").Set(name, person))
}
var sum int32
panicNotNil(db.Table("do_testing").All().Do(func(key string, counter uint64, doc Document) error {
atomic.AddInt32(&sum, 1)
return nil
}))
if sum != 3 {
t.Fatal("sum should be 3, but isn't")
}
sum = 0
panicNotNil(db.Table("do_testing").All().Do(func(key string, counter uint64, doc Document) error {
sum++
return nil
}, 1))
if sum != 3 {
t.Fatal("sum should be 3, but isn't")
}
sum = 0
testError := errors.New("cete testing: test do")
err = db.Table("do_testing").All().Do(func(key string, counter uint64, doc Document) error {
if key == "ben" {
time.Sleep(time.Millisecond * 100)
return testError
}
atomic.AddInt32(&sum, 1)
return nil
}, 5)
if err != testError {
t.Fatal("error should be testError, but isn't")
}
if sum != 2 {
t.Fatal("sum should be 2, but isn't")
}
r := newRange(func() (string, []byte, uint64, error) {
return "", nil, 0, testError
}, func() {}, nil)
err = r.Do(func(key string, counter uint64, doc Document) error {
t.Fatal("do should not run, but does")
return nil
})
if err != testError {
t.Fatal("error should be testError, but isn't")
}
}
func TestFilter(t *testing.T) {
if testing.Short() {
t.Parallel()
}
people := map[string]Person{
"jason": {
Name: "Jason",
City: "Sydney",
Age: 17,
Height: 1.76,
DOB: time.Date(1999, 1, 28, 01, 01, 01, 01, time.UTC),
},
"ben": {
Name: "Ben",
City: "Melbourne",
Age: 19,
Height: 1.83,
DOB: time.Date(1998, 5, 23, 01, 01, 01, 01, time.UTC),
},
"drew": {
Name: "Drew",
City: "London",
Age: 18,
Height: 1.72,
DOB: time.Date(2001, 7, 13, 01, 01, 01, 01, time.UTC),
},
}
dir, err := ioutil.TempDir("", "cete_")
panicNotNil(err)
t.Log("testing directory:", dir)
defer func() {
if !t.Failed() {
os.RemoveAll(dir)
}
}()
db, err := Open(dir + "/data")
panicNotNil(err)
defer func() {
db.Close()
}()
err = db.NewTable("filter_testing")
panicNotNil(err)
for name, person := range people {
err = db.Table("filter_testing").Set(name, person)
panicNotNil(err)
}
r := db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryInt("Age") > 17, nil
}, 2)
expectPerson("ben", r, people["ben"])
expectPerson("drew", r, people["drew"])
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
if r.closed != 1 {
t.Fatal("range should have automatically closed, but hasn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 1.75, nil
}, 1)
expectPerson("ben", r, people["ben"])
expectPerson("jason", r, people["jason"])
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryTime("DOB").After(time.Date(2000, 01, 01, 01, 01, 01, 01, time.UTC)), nil
})
expectPerson("drew", r, people["drew"])
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 1.75, nil
})
n, err := r.Count()
panicNotNil(err)
if n != 2 {
t.Fatal("count should be 2, but isn't")
}
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 1.75, nil
}).Skip(2)
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 1.75, nil
}).Skip(1)
expectPerson("jason", r, people["jason"])
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 1.75, nil
}).Skip(3)
if r.Next() || r.Error() != ErrEndOfRange {
t.Fatal("error should be ErrEndOfRange, but isn't")
}
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return doc.QueryFloat64("Height") > 0.5, nil
})
if !r.Next() {
t.Fatal("Next should be successful")
}
if r.Key() != "ben" {
t.Fatal("key should be ben, but isn't")
}
if !r.Next() {
t.Fatal("Next should be successful")
}
if r.Key() != "drew" {
t.Fatal("key should be drew, but isn't")
}
if !r.Next() {
t.Fatal("Next should be successful")
}
if r.Key() != "jason" {
t.Fatal("key should be jason, but isn't")
}
filterError := errors.New("cete testing: filter error")
r = db.Table("filter_testing").All().Filter(func(doc Document) (bool, error) {
return false, filterError
})
if r.Next() || r.Error() != filterError {
t.Fatal("error should be filter error, but isn't")
}
}