-
Notifications
You must be signed in to change notification settings - Fork 0
/
iter_test.go
76 lines (64 loc) · 1.17 KB
/
iter_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
package rumble
import (
"os"
"strconv"
"testing"
"gopkg.in/mgo.v2/bson"
)
type iterTest struct {
predicate func(k, v []byte) bool
result []string
}
var iterTests = []iterTest{
{
nil,
[]string{"0", "1", "2", "3"},
},
{
func(k, v []byte) bool {
if string(k) != "2" {
return true
}
return false
},
[]string{"0", "1", "3"},
},
{
func(k, v []byte) bool {
if string(k) == "foo" {
return true
}
return false
},
[]string{},
},
}
func TestIterator(t *testing.T) {
db := prepareTestDB(t, "TestIter.db")
defer func() {
db.Bolt.Close()
os.Remove("TestIter.db")
}()
bucket := db.Bucket("foo")
for i := 0; i < 4; i++ {
bucket.Put(bson.M{
"_key": []byte(strconv.Itoa(i)),
})
}
for _, it := range iterTests {
i := bucket.Iterate(it.predicate)
var results []string
m := make(map[string]interface{})
for i.Next(&m) {
results = append(results, string(m["_key"].([]byte)))
}
if len(it.result) != len(results) {
t.Fatalf("expected %v results, got %v", len(it.result), len(results))
}
for k, v := range it.result {
if results[k] != v {
t.Fatalf("expected %s, got %s (%#v)", v, results[k], it)
}
}
}
}