-
Notifications
You must be signed in to change notification settings - Fork 174
/
iterator_test.go
68 lines (53 loc) · 1.3 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
package goka
import (
"os"
"testing"
"github.com/lovoo/goka/codec"
"github.com/lovoo/goka/storage"
"github.com/stretchr/testify/require"
"github.com/syndtr/goleveldb/leveldb"
)
func TestIterator(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "goka_storage_TestIterator")
require.NoError(t, err)
db, err := leveldb.OpenFile(tmpdir, nil)
require.NoError(t, err)
st, err := storage.New(db)
require.NoError(t, err)
kv := map[string]string{
"key-1": "val-1",
"key-2": "val-2",
"key-3": "val-3",
}
for k, v := range kv {
require.NoError(t, st.Set(k, []byte(v)))
}
require.NoError(t, st.SetOffset(777))
iter, err := st.Iterator()
require.NoError(t, err)
it := &iterator{
iter: storage.NewMultiIterator([]storage.Iterator{iter}),
codec: new(codec.String),
}
defer it.Release()
count := 0
// accessing iterator before Next should only return nils
val, err := it.Value()
require.True(t, val == nil)
require.NoError(t, err)
for it.Next() {
count++
key := it.Key()
expected, ok := kv[key]
if !ok {
t.Fatalf("unexpected key from iterator: %s", key)
}
val, err := it.Value()
require.NoError(t, err)
require.Equal(t, val.(string), expected)
}
if err := it.Err(); err != nil {
t.Fatalf("unexpected iteration error: %v", err)
}
require.Equal(t, len(kv), count)
}