-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator_test.go
96 lines (94 loc) · 3.4 KB
/
iterator_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
package biter
import (
"github.com/dgraph-io/badger/v3"
"reflect"
"testing"
)
func TestKeyRangeIterator(t *testing.T) {
bOpts := badger.DefaultOptions("").WithInMemory(true)
aBadger, err := badger.Open(bOpts)
if err != nil {
t.Error("unable to start database!")
}
anIterator := aBadger.NewTransaction(false).NewIterator(badger.DefaultIteratorOptions)
type args struct {
prefix []byte
from []byte
to []byte
}
tests := []struct {
name string
args args
want Iterator
}{
{
name: "none-returns the wrapping iterator",
want: anIterator,
args: args{},
}, {
name: "prefix returns a prefix iterator",
want: &badgerPrefixIterator{anIterator, []byte("prefix")},
args: args{prefix: []byte("prefix")},
}, {
name: "from-to returns a from-to iterator",
want: &badgerFromToIterator{anIterator, []byte("from"), []byte("to")},
args: args{from: []byte("from"), to: []byte("to")},
}, {
name: "from returns a from iterator",
want: &badgerFromIterator{anIterator, []byte("from")},
args: args{from: []byte("from")},
}, {
name: "to returns a to iterator",
want: &badgerToIterator{anIterator, []byte("to")},
args: args{to: []byte("to")},
}, {
name: "to-prefix returns a FromTo iterator",
want: &badgerFromToIterator{anIterator, []byte("d"), []byte("dg")},
args: args{to: []byte("dg"), prefix: []byte("d")},
}, {
name: "to-prefix returns a FromTo iterator, which is invalid",
want: &badgerFromToIterator{anIterator, []byte("d"), []byte("c")},
args: args{to: []byte("c"), prefix: []byte("d")},
}, {
name: "to-prefix returns a Prefix iterator",
want: &badgerPrefixIterator{anIterator, []byte("g")},
args: args{to: []byte("z"), prefix: []byte("g")},
}, {
name: "from-prefix returns a FromTo iterator",
want: &badgerPrefixFromIterator{&badgerPrefixIterator{anIterator, []byte("d")}, []byte("dg")},
args: args{from: []byte("dg"), prefix: []byte("d")},
}, {
name: "from-prefix returns a Prefix iterator",
want: &badgerPrefixIterator{anIterator, []byte("i")},
args: args{from: []byte("g"), prefix: []byte("i")},
}, {
name: "from-prefix returns a PrefixFrom iterator, which will be invalid",
want: &badgerPrefixFromIterator{&badgerPrefixIterator{anIterator, []byte("i")}, []byte("k")},
args: args{from: []byte("k"), prefix: []byte("i")},
}, {
name: "from-to-prefix returns a FromTo Iterator",
want: &badgerFromToIterator{anIterator, []byte("ka"), []byte("kb")},
args: args{from: []byte("ka"), to: []byte("kb"), prefix: []byte("k")},
}, {
name: "from-to-prefix returns a FromTo Iterator a out of bounds",
want: &badgerFromToIterator{anIterator, []byte("k"), []byte("kb")},
args: args{from: []byte("a"), to: []byte("kb"), prefix: []byte("k")},
}, {
name: "from-to-prefix returns a Prefix Iterator",
want: &badgerPrefixIterator{anIterator, []byte("b")},
args: args{from: []byte("a"), to: []byte("c"), prefix: []byte("b")},
}, {
name: "from-to-prefix returns a PrefixFrom Iterator",
want: &badgerPrefixFromIterator{&badgerPrefixIterator{anIterator, []byte("b")}, []byte("bb")},
args: args{from: []byte("bb"), to: []byte("c"), prefix: []byte("b")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := KeyRangeIterator(anIterator, tt.args.prefix, tt.args.from, tt.args.to)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("KeyRangeIterator() = %v, want %v", got, tt.want)
}
})
}
}